我有一个日历控件,我点击一天给我一个javascript弹出窗口来添加一个培训课程。每天可能会有多个培训课程。添加类是没有问题的。然后在另一个页面上,我已经获得了这个精确的日历控件,但每个日期都使用动态链接按钮填充了培训课程。在我希望检索培训班的ID LinkButton的的点击将被用来填充标签和使用这个号码作为参考,当他/她的雇员(插入SQL时)的管理者请求训练
我尝试过添加一个EventHandler,但这并没有解决。我尝试使用Click属性到这个linkbutton(但不能从属性调用方法)...我不确定我是否遗漏了事件处理程序未触发的内容,有人可以帮我解决这个问题吗?
这就是我所做的: DayRendering日历控件:
protected void calSchedule_DayRender(object sender, DayRenderEventArgs e)
{
bool result = false;
string filterstring = "SELECT tos.[ID], sc.ServiceCenter, tos.Description " +
"FROM [SOS].[dbo].[TrainingOfficerSchedule] tos " +
"INNER JOIN [dbo].[TrainingOfficerServiceCenters] sc " +
"on tos.ServiceCenterRef = sc.ID " +
"WHERE TrainingDate = @TrainingDate";
cn = new SqlConnection(GetConnectionString());
SqlCommand myCmd = new SqlCommand(filterstring, cn);
myCmd.Parameters.AddWithValue("@TrainingDate", e.Day.Date.ToShortDateString());
cn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
if (myReader.HasRows)
{
while (myReader.Read())
{
string training = myReader["ServiceCenter"].ToString() + " - " + myReader["Description"].ToString();
LinkButton lnk = new LinkButton();
lnk.ID = myReader["ID"].ToString();
lnk.ForeColor = Color.FromArgb(64, 64, 64);
lnk.BackColor = Color.FromArgb(192, 192, 192);
lnk.Attributes.Add("onclick", "return getItems();");
lnk.Click += new EventHandler(lnk_Click);
//lnk.Attributes.Add("onClick", "return getItems();");
//lnk.Attributes.Add("OnClientClick", "return getItems();");
//lnk.Attributes.Add("onclick", jsMethod(Convert.ToInt32(myReader["ID"])));
//lnk.Click += new EventHandler(this.lnk_Click);
//lnk.Atributes.Add("runat", "server");
//lnk.Attributes.Add("onclick", jsMethod(myReader["ID"].ToString()));
lnk.Text = training + "<br/><br/>";
lnk.Style.Add("cursor", "pointer");
e.Cell.Controls.Add(lnk);
}
}
cn.Close();
}
单击Linkbutton时要触发的事件处理程序:
public void lnk_Click(object sender, EventArgs e)
{
string ctrlId = ((Control)sender).ID;
txtMManagerName.Text = ctrlId;
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "popSchedular", "showSchedular();", true);
}
我可能做错了吗?
答案 0 :(得分:0)
将PlaceHolder放在要显示链接按钮的位置,然后尝试。
<asp:PlaceHolder ID="ph" runat="server" />
LinkButton lnk = new LinkButton();
lnk.ID = myReader["ID"].ToString();
lnk.ForeColor = Color.FromArgb(64, 64, 64);
lnk.BackColor = Color.FromArgb(192, 192, 192);
lnk.Attributes.Add("onclick", "return getItems();");
lnk.Click += new EventHandler(lnk_Click);
lnk.Text = training + "<br/><br/>";
lnk.Style.Add("cursor", "pointer");
ph.Controls.Add(lnk);
protected void lnk_Click(object sender, EventArgs e)
{
string ctrlId = ((Control)sender).ID;
txtMManagerName.Text = ctrlId;
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "popSchedular", "showSchedular();", true);
}
答案 1 :(得分:0)
我以前使用过动态控件。使用相同的对象引用创建多个项目似乎搞得一团糟。尝试创建一个继承自LinkButton的类,并在读取循环中使用它。
public class myButton : LinkButton
{
public myButton(SqlCommand sqlCmd)
{
var myReader = (SqlDataReader) sqlCmd.ExecuteReader();
var training = myReader["ServiceCenter"].ToString() + " - " + myReader["Description"].ToString();
ID =myReader["ID"].ToString();
ForeColor = Color.FromArgb(64, 64, 64);
BackColor = Color.FromArgb(192, 192, 192);
Attributes.Add("onclick", "return getItems();");
Click += new EventHandler(lnk_Click);
Text = training + "<br/><br/>";
Style.Add("cursor", "pointer");
}
}