問題描述
我嘗試了多種解決方案來解決這個(gè)問題,但都沒有奏效.基本上,我有一張員工表,用戶可以選擇通過更新面板動(dòng)態(tài)添加員工.每個(gè)員工都被添加為 LinkBut??ton,此按鈕將通過 ajaxToolkit:modalpopupextender 窗口>OnClick 事件,此窗口將顯示員工詳細(xì)信息.問題是當(dāng)我點(diǎn)擊員工姓名時(shí),彈出窗口會(huì)顯示但細(xì)節(jié)不會(huì).
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.
這是我創(chuàng)建按鈕并將其放入表格的代碼:
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();
}
推薦答案
查看這個(gè)答案
https://stackoverflow.com/a/11127064/1268570
這解釋了動(dòng)態(tài)控件的行為
This explains the behavior of dynamic controls
你需要考慮:
- 當(dāng)您不使用母版頁(yè)時(shí),應(yīng)在 PreInit 事件中創(chuàng)建動(dòng)態(tài)控件,如果是,則在 Init 事件中創(chuàng)建控件
- 避免在這些事件中設(shè)置可以在每個(gè)帖子中更改的屬性,因?yàn)楫?dāng)應(yīng)用視圖狀態(tài)時(shí)(在帖子事件中),這些屬性將被覆蓋
- 每次發(fā)布??頁(yè)面時(shí)都必須創(chuàng)建動(dòng)態(tài)控件,避免這種情況 if(!this.IsPostBack) this.CreatemyDynamicControls();
- 當(dāng)您在 PreInit 或 Init 事件中創(chuàng)建控件時(shí),它們的狀態(tài)將在 post 事件中自動(dòng)設(shè)置,這意味著在 LoadComplete 事件中,即使您在每個(gè) post 中再次創(chuàng)建控件,您的控件也會(huì)包含它們的狀態(tài),甚至當(dāng)您沒有明確設(shè)置它們的狀態(tài)時(shí).請(qǐng)注意,當(dāng)您處理在設(shè)計(jì)時(shí)創(chuàng)建的控件時(shí),此行為是不同的,在這種情況下,設(shè)置狀態(tài)的事件是 Load 事件
- 事件訂閱應(yīng)該在 PageLoadComplete 之前發(fā)生,否則它們將不會(huì)被引發(fā)
如果您還沒有找到解決方案,這是一種方法(完整的工作示例):
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;
}
輸出
這篇關(guān)于動(dòng)態(tài)創(chuàng)建的 LinkBut??tons 的 OnClick 事件不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!