0

我有一个 aspx 页面,它会在按钮单击时动态更新。正在将带有按钮的表格添加到占位符中。每个按钮都应该重定向到另一个页面,在查询字符串中发送它的 id。

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

我试图将除占位符之外的所有内容都放在更新面板中,但是首先没有添加表格。如何从页面加载中再次调用 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

4 回答 4

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
    }

您必须在回发后重新分配点击事件。让我知道你是否有帮助。

于 2013-05-30T12:54:09.703 回答
0

Button OnClick 事件仅在按钮位于 Form 元素内时触发。

只需将您的 HTML 放入表单中。

我用您的代码进行了尝试,并且能够看到用户单击按钮时调用了 search_Click 。

于 2013-05-30T12:37:37.177 回答
0

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

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

于 2013-05-30T12:20:48.350 回答
0

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

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

}
于 2013-05-30T12:29:09.593 回答