Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dynamic button not fire event in the page

problem : save button not fired ... how i could solve the problem.

Here's the behind code
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            btnCreate_Click(sender, e);
            
        }
    }
    
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        DivTextBoxCreator.Controls.Clear();
        HtmlTable htable = new HtmlTable();
        htable.BgColor = "Yellow";
        for (int i = 1; i <= int.Parse(TextBox1.Text); i++)
        {
            HtmlTableRow hRow = new HtmlTableRow();
            HtmlTableCell hCell = new HtmlTableCell();
            TextBox txtGenerator = new TextBox();

            txtGenerator.Text = "TextBox : " + i;
            hCell.Controls.Add(txtGenerator);
            hRow.Controls.Add(hCell);
            htable.Controls.Add(hRow);
        }
        DivTextBoxCreator.Controls.Add(htable);

        // create a Save Button
        Button buttonSave = new Button();
        buttonSave.ID = "buttonSave";
        buttonSave.Text = "Save";
        buttonSave. önClientClick = "return validate()";
        buttonSave.Click += new EventHandler(buttonSave_Click);
        buttonSave_Click(sender, e);
        DivTextBoxCreator.Controls.Add(buttonSave);
    }

    protected void buttonSave_Click(object sender, EventArgs e)
    {
        Response.Write("Test");
    }
}



Here's the tags
C#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    
        <table>
            <tr>
                <td>
                    Enter Number of TextBox</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td style="margin-removed 40px">
                    <asp:Button ID="btnCreate" runat="server" Text="Create" Width="139px" 
                        onclick="btnCreate_Click" />
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td style="margin-removed 40px">
                     </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td style="margin-removed 40px">
           
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                      <div id="DivTextBoxCreator"  runat="server">

                    </div>
                    </ContentTemplate>
                    
                    </asp:UpdatePanel>
                 </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td style="margin-removed 40px">
                   <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                      <div id="DivButtonCreator"  runat="server">

                          

                    </div>
                    </ContentTemplate>
                   
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td style="margin-removed 40px">
                     </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>
Posted

Hi,

when you create the button in event the asp does not load your new button then you must refresh your asp page or you create your button in the load.

see this example Dynamically Created Controls in ASP.NET[^]

good luck
 
Share this answer
 
Comments
Honeyboy_20 23-May-13 8:08am    
Could you correct my code ... coz i have some conflicts in your code.
JeremH 24-May-13 5:36am    
you can not to that your button is not create and you rise event it is impossible you can rise event in function the page lifecycle.
may be the problem is your trying to hook a handler to a button in prerender(before rendering of page)
try to hook the events under post render events.

may be this sorts out ur problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900