按钮事件功能不会触发(回发问题)

时间:2013-05-30 12:17:12

标签: c# asp.net events postback

我有一个aspx页面,可以通过按钮点击动态更新。带有按钮的表被添加到占位符。每个按钮应该重定向到另一个页面,在查询字符串中发送它的id。

我遇到的问题是,当单击每个按钮时会发生回发并且按钮不会再次添加到页面中(因为该方法是通过按钮单击运行的),因此事件触发的功能是没跑了

我尝试将所有内容都放在除占位符之外的updatepanel中,但之后不会添加该表。如何从页面加载再次调用btn_click函数?我应该传递什么参数? 或者我可以添加不会回发的按钮?

这是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>

这是背后的代码:

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;

}

4 个答案:

答案 0 :(得分:2)

        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
    }

您必须在回发后重新分配点击事件。 如果你帮助我,请告诉我。

答案 1 :(得分:0)

也许您可以使用简单的<a href="">链接而不是按钮?

否则,您可以使用按钮的OnClientClick属性,而不是OnClick事件,因为它会导致回发。

答案 2 :(得分:0)

建议在Page_Init期间加载动态控件,因为我们可能希望在早期阶段将事件与适当的处理程序连接起来。

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

}

答案 3 :(得分:0)

按钮OnClick事件仅当按钮位于Form元素内时才会被触发。

只需将HTML放入表单中即可。

我用你的代码尝试了它,并且当用户点击按钮时能够看到search_Click被调用。