1

I have a textbox and submit button created using the design mode.

When the submit button is pressed, it will retrieve the user input from the textbox and then make a query to my database. It will then display a list of dynamic buttons according to the information retrieved from my database.

However, the event handler for the buttons does not fire when clicked. I guess my problem is the postback but I cannot create those buttons in page_load etc. because I need to get the user input (from the textbox when submit button is pressed) before i can load the buttons.

How can i solve this problem?

Thank you.

Edit (codes):

protected void subBtn_Click(object sender, EventArgs e)
{
       //database setup codes
        .......


       while (reader.Read())
       {
            Button detailsBtn = new Button();
            detailsBtn.Text = reader["fName"].ToString();
            //doesn't fire
            detailsBtn.Click += new EventHandler(detailsBtn_Click);
            memPanel.Controls.Add(detailsBtn);
       }
}
Grace
  • 91
  • 3
  • 11
  • Show us how the buttons are created. It should be easily achievable to wire them all up to a single eventhandler. – Euric Aug 22 '12 at 08:32
  • possible duplicate of [ASP.NET dynamically created controls and Postback](http://stackoverflow.com/questions/4216329/asp-net-dynamically-created-controls-and-postback) – podiluska Aug 22 '12 at 08:33
  • Hi, edited my questions with the codes. thanks – Grace Aug 22 '12 at 08:40
  • If you need to react on user's decisions like this, it's worthwhile to consider using ajax to provide the functionality you want. It can increase performance of your app and you won't have to solve problems like this. – walther Aug 22 '12 at 08:33

2 Answers2

4

Main problem is Postback regenerate dynamic controls on each postback if those controls does not exists. For quick demo see this code

ASPX CODE

<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</form>

ASPX.CS CODE

protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack)
            {
                generate();                
            }
        }
        public void generate()
        {
            if (!pnl.HasControls())
            {
                for (int i = 0; i < 4; i++)
                {
                    Button detailsBtn = new Button();
                    detailsBtn.Text = "fName" + i.ToString();
                    detailsBtn.ID = i.ToString();
                    detailsBtn.Click += new EventHandler(detailsBtn_Click);
                    pnl.Controls.Add(detailsBtn);
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            generate();
        }

        protected void detailsBtn_Click(object sender, EventArgs e)
        {
        }
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
  • Thanks for the reply. But i have other button which has other functions, if i put the generate() in ispostback wouldn't it call the method everytime the page postback. – Grace Aug 23 '12 at 02:41
0

Sound to me like you could easily refactor your page to use a simple <asp:Repeater runat="server" ..></asp:Repeater> instead of dynamically adding controls to a Panel.

Here is a very simple complete sample:

RepeaterTest.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:TextBox ID="theTextBox" runat="server"></asp:TextBox>
    <asp:Button ID="theButton" runat="server" OnClick="theButton_Click" Text="Click me" />
    <asp:Repeater ID="test" runat="server">
        <ItemTemplate>
            <asp:Button ID="theRepeaterButton" runat="server" Text='<%# Eval("fName") %>' OnClick="theRepeaterButton_Click" />
        </ItemTemplate>
    </asp:Repeater>
</asp:Content>

RepeaterTest.aspx.cs

public partial class RepeaterTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void theButton_Click(object sender, EventArgs e)
    {
        string filter = theTextBox.Text;
        // below row simulates fetching data using the filter text in the text box
        var data = Enumerable.Range(0, 20).Select(i => new { fName = filter + " " + i });
        test.DataSource = data;
        test.DataBind();
    }
    protected void theRepeaterButton_Click(object sender, EventArgs e)
    {
        var button = (Button)sender;

        // do something here based on text/commandname/commandargument etc of the button

    }
}
user1429080
  • 9,086
  • 4
  • 31
  • 54