我尝试了多种解决方案来解决这个问题,但都没有奏效.基本上,我有一张员工表,用户可以选择通过更新面板动态添加员工.每个员工都被添加为 LinkButton,此按钮将通过 ajaxToolkit:modalpopupextender 窗口>OnClick 事件,此窗口将显示员工详细信息.问题是当我点击员工姓名时,弹出窗口会显示但细节不会.
I’ve tried several solutions for this problem but none of them worked. Basically, I have a table of employees and the user have the choice of adding an employee dynamically thru an update panel. Each employee is being added as LinkButton and this button will fire ajaxToolkit:modalpopupextender window through OnClick event, and this window will show the employee details. The problem is when I click on the employee name the popup window will show up BUT the details wont.
这是我创建按钮并将其放入表格的代码:
Here is the code in which I’m creating the buttons and putting it in the table:
LinkButton lbtn = new LinkButton();
lbtn.ID = employee_arry[i] + "_lbtn" + i;
lbtn.Text = employee_arry[i];
lbtn.Click += new EventHandler(this.employee_info);
lbtn.CausesValidation = false;
lbtn.Attributes.Add("runat", "server");
cell.Controls.Add(lbtn);
这里是employee_info方法:
and here is the employee_info method:
//the info will be pulled from the database…
public void employee_info(object sender, EventArgs e)
{
name.Text = "employee name";
dept.Text = "employee department";
jobt.Text = "employee job title";
email.Text = "employee email";
tel.Text = "employee telephone";
ModalPopupExtender1.Show();
}
查看这个答案
https://stackoverflow.com/a/11127064/1268570
这解释了动态控件的行为
This explains the behavior of dynamic controls
你需要考虑:
如果您还没有找到解决方案,这是一种方法(完整的工作示例):
In case you have not found a solution, this is a way to do it (full working example):
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled">
<ContentTemplate>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel><br />
<asp:Button ID="Button1" Text="add control" runat="server" OnClick="addControl_Click" /><br />
<asp:Label ID="lblMessage" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
protected int NumberOfControls
{
get
{
if (ViewState["c"] == null)
{
return 0;
}
return int.Parse(ViewState["c"].ToString());
}
set
{
ViewState["c"] = value;
}
}
protected void addControl_Click(object sender, EventArgs e)
{
this.NumberOfControls++;
this.myPanel.Controls.Add(new Literal { Text = "<br />" });
this.myPanel.Controls.Add(this.CreateLinkButton(this.NumberOfControls));
}
protected void Page_PreLoad(object sender, EventArgs e)
{
this.CreateDynamicLinkButtons();
}
private void CreateDynamicLinkButtons()
{
for (int i = 0; i < this.NumberOfControls; i++)
{
this.myPanel.Controls.Add(new Literal { Text = "<br />" });
this.myPanel.Controls.Add(this.CreateLinkButton(i + 1));
}
}
private LinkButton CreateLinkButton(int index)
{
var l = new LinkButton { Text = "MyLink" + index.ToString(), ID = "myLinkID" + index.ToString() };
l.Click += (x, y) =>
{
this.lblMessage.Text += "<br/>ID: " + (x as LinkButton).ID;
};
return l;
}
这篇关于动态创建的 LinkButtons 的 OnClick 事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!