开发者

Sharepoint 2010 - Problem with event handlers for dynamically created controls

开发者 https://www.devze.com 2023-03-30 15:27 出处:网络
In a WebPart using Sharepoint 2010, I´ve got a problem when creating dynamically a LinkButton because his event isn´t fired.

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号