Dvirski
Dvirski

Reputation: 321

button event function won't fire (postback issue)

I have a aspx page which gets updated dynamically on button click. a table with buttons is being added to a placeholder. each button should redirect to another page sending it's id in a query string.

the problem I'm having is that when each button is clicked a postback occurs and the buttons are not added into the page again (because the method is being run by button click), and so the function fired by the event isn't running

I tried to put everything inside an updatepanel except the placeholder, but then the table is not added in the first place. how can I call the btn_click function again from the pageload? what should I pass in its parameters? or maybe I can add buttons that won't casue postback?

this is the aspx:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
   <h1 style="text-align:center;">חיפוש חברים</h1>
    <br />
        <table>
            <tr><td>אזור עבודה מועדף</td>
                <td>סיווג משני</td>
                <td>סיווג ראשי</td>
                <td>שם משפחה</td>
                <td>שם</td>
                <td>תז</td> 
            </tr>

            <tr>

            <td>
                <asp:DropDownList ID="working_area" CssClass="wid" runat="server">
                   <%--some items--%>

                </asp:DropDownList>
            </td>

            <td>

                <asp:DropDownList ID="d_d_second" CssClass="wid" runat="server">

                </asp:DropDownList>            
            </td>            

            <td>
                <asp:DropDownList ID="d_d_main" CssClass="wid" runat="server">

                    <asp:ListItem></asp:ListItem>


                </asp:DropDownList>
            </td>

            <td><asp:TextBox ID="m_l_name" CssClass="wid" runat="server"></asp:TextBox></td>
            <td><asp:TextBox ID="m_name" CssClass="wid" runat="server"></asp:TextBox></td>
            <td><asp:TextBox ID="m_id" CssClass="wid" runat="server"></asp:TextBox></td>        
            </tr>

            <tr>               
                <td style="text-align:left"><asp:Button ID="search" CssClass="btn btn-primary" 
                        runat="server" Text="חפש" onclick="search_Click" /></td>
            </tr>


        </table>

    <asp:PlaceHolder ID="search_tbl_ph" runat="server"></asp:PlaceHolder>
     </ContentTemplate>
     </asp:UpdatePanel>

this is the code behind:

protected void Page_Load(object sender, EventArgs e)
{


}



protected void search_Click(object sender, EventArgs e)
{
    search_category s1 = new search_category();

    s1.id = m_id.Text;
    s1.name = m_name.Text;
    s1.l_name = m_l_name.Text; 
    s1.main_cat = d_d_main.SelectedValue;
    s1.second_cat = d_d_second.SelectedValue;
    s1.working_area = working_area.SelectedValue;


    List<member> m_list = db.return_search_member(s1);

    Table m_tbl = new Table();
    TableRow r2 = new TableRow();

    TableCell c7 = new TableCell();
    TableCell c8 = new TableCell();
    TableCell c9 = new TableCell();
    TableCell c10 = new TableCell();
    TableCell c11 = new TableCell();
    TableCell c12 = new TableCell();

    c7.Text = "תז";
    c8.Text="שם פרטי";
    c9.Text="שם משפחה";
    c10.Text="סיווג ראשי";
    c11.Text="סיווג משני";

    r2.Controls.Add(c12);
    r2.Controls.Add(c11); 
    r2.Controls.Add(c10);
    r2.Controls.Add(c9);
    r2.Controls.Add(c8);
    r2.Controls.Add(c7);
    r2.CssClass = " head_line";


    m_tbl.Controls.Add(r2);


    foreach (member m1 in m_list)
    {

        TableRow r1 = new TableRow();
        TableCell c1 = new TableCell();
        TableCell c2 = new TableCell();
        TableCell c3 = new TableCell();
        TableCell c4 = new TableCell();
        TableCell c5 = new TableCell();
        TableCell c6 = new TableCell();

        Button btn1 = new Button { Text = "עבור לכרטיס חבר", CommandArgument = "argument", ID = m1.id };
        btn1.Click += new EventHandler(btn_click);
        btn1.CssClass = "btn btn-primary";
        c1.Controls.Add(btn1);

        c2.Text = m1.prof.secondary;
        c3.Text = m1.prof.primary;
        c4.Text = m1.l_name;
        c5.Text = m1.f_name;
        c6.Text = m1.id;

        r1.Controls.Add(c1);
        r1.Controls.Add(c2);
        r1.Controls.Add(c3);
        r1.Controls.Add(c4);
        r1.Controls.Add(c5);
        r1.Controls.Add(c6);

        m_tbl.Controls.Add(r1);

    }
 search_tbl_ph.Controls.Add(m_tbl);
}

protected void btn_click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    String member_id = btn.ID;
    string qstring = "?id=" + member_id;

    Response.Redirect("member_page.aspx" + qstring);
    //Session["id"] = qstring;

}

Upvotes: 0

Views: 4671

Answers (4)

S Nash
S Nash

Reputation: 2509

Button OnClick Event Only gets fired if the button is inside a Form element.

Just put your HTML inside a Form.

I tried it with your code and was able to see search_Click gets called when user click on the button.

Upvotes: 0

Mayur Borad
Mayur Borad

Reputation: 1295

        private void MakeButton()
        {
         search_category s1 = new search_category();

            s1.id = m_id.Text;
            s1.name = m_name.Text;
            s1.l_name = m_l_name.Text; 
            s1.main_cat = d_d_main.SelectedValue;
            s1.second_cat = d_d_second.SelectedValue;
            s1.working_area = working_area.SelectedValue;


            List<member> m_list = db.return_search_member(s1);

            Table m_tbl = new Table();
            TableRow r2 = new TableRow();

            TableCell c7 = new TableCell();
            TableCell c8 = new TableCell();
            TableCell c9 = new TableCell();
            TableCell c10 = new TableCell();
            TableCell c11 = new TableCell();
            TableCell c12 = new TableCell();

            c7.Text = "תז";
            c8.Text="שם פרטי";
            c9.Text="שם משפחה";
            c10.Text="סיווג ראשי";
            c11.Text="סיווג משני";

            r2.Controls.Add(c12);
            r2.Controls.Add(c11); 
            r2.Controls.Add(c10);
            r2.Controls.Add(c9);
            r2.Controls.Add(c8);
            r2.Controls.Add(c7);
            r2.CssClass = " head_line";


            m_tbl.Controls.Add(r2);


            foreach (member m1 in m_list)
            {

                TableRow r1 = new TableRow();
                TableCell c1 = new TableCell();
                TableCell c2 = new TableCell();
                TableCell c3 = new TableCell();
                TableCell c4 = new TableCell();
                TableCell c5 = new TableCell();
                TableCell c6 = new TableCell();

                Button btn1 = new Button { Text = "עבור לכרטיס חבר", CommandArgument = "argument", ID = m1.id };
                btn1.Click += new EventHandler(btn_click);
                btn1.CssClass = "btn btn-primary";
                c1.Controls.Add(btn1);

                c2.Text = m1.prof.secondary;
                c3.Text = m1.prof.primary;
                c4.Text = m1.l_name;
                c5.Text = m1.f_name;
                c6.Text = m1.id;

                r1.Controls.Add(c1);
                r1.Controls.Add(c2);
                r1.Controls.Add(c3);
                r1.Controls.Add(c4);
                r1.Controls.Add(c5);
                r1.Controls.Add(c6);

                m_tbl.Controls.Add(r1);

            }
         search_tbl_ph.Controls.Add(m_tbl);
        }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(ViewState["ClickEventFired"]!=null && ViewState["ClickEventFired"]==true)
        {
           MakeButton();
        }
    }

    protected void search_Click(object sender, EventArgs e)
    {
        MakeButton();
        ViewState["ClickEventFired"]=true;
    }

    protected void btn_click(object sender, EventArgs e)
    {
        // your code
    }

You have to reassign click event after Postback. let me know if you help this.

Upvotes: 2

Sain Pradeep
Sain Pradeep

Reputation: 3125

Its recommended to load the dynamic controls during the Page_Init instead, because we may want to hook up our events with proper handler at an early stage.

protected void Page_Init(object sender, EventArgs e)
{ 
   ///code to create dynamic controls

}

Upvotes: 0

bcolin
bcolin

Reputation: 438

Maybe you could use a simple <a href=""> link instead of a button ?

Otherwise, you can use the OnClientClick attribute of the button, not the OnClick event because it will cause a postback.

Upvotes: 0

Related Questions