In a WebPart using Sharepoint 2010, I´ve got a problem when creating dynamically a LinkButton because his event isn´t fired.
My code is:
Default.aspx
<asp:TextBox ID="formAccountCode" runat="server" MaxLength="8" Columns="8"></asp:TextBox>
<asp:Table ID="idTabela" runat="server" Width="100%" BorderWidth="1px" GridLines="Both"></asp:Table>
<asp:Button ID="btPesquisar" runat="server" onclick="btPesquisar_Click" Text="Pesquisar" />
Default.aspx.cs
protected void btPesquisar_Cli开发者_开发知识库ck(object sender, EventArgs e)
{
LinkButton lkButton = new LinkButton();
lkButton.Text = "Teste Tabela";
lkButton.ID = "link1";
lkButton.Attributes.Add("runat", "server");
lkButton.CommandArgument = "Codigo 1";
lkButton.Command += test;
TableRow tr;
TableCell td1;
td1 = new TableCell();
td1.Controls.Add(lkButton);
tr = new TableRow();
tr.Cells.Add(td1);
idTabela.Rows.Add(tr);
idTabela.DataBind();
}
protected void test(object sender, EventArgs e)
{
formAccountCode.Text = "HI"; // just for test
}
The idea is return a select from DB and create a result with linkbutton to each record returned.
These LinkButtons will be created after an action from the user, because this, it can´t be created in OnInit.
They will connect with another web part.
please move the code for dynamically created button and event handler in CreateChildControls()
and call the EnsureChildControls()
in Onload
For example:
protected override void CreateChildControls()
{
base.CreateChildControls();
// Create our drop down list, but don't populate it yet
_dropDownList = new DropDownList();
this.Controls.Add(_dropDownList);
}
精彩评论