按钮事件函数没有触发(回发问题)
本文关键字:问题 事件 函数 按钮 | 更新日期: 2023-09-27 18:16:17
我有一个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;
}
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
}
你必须在Postback之后重新分配click事件。如果你有帮助,请告诉我
也许你可以使用一个简单的<a href="">
链接而不是一个按钮?
否则,你可以使用按钮的OnClientClick属性,而不是OnClick事件,因为它会导致回发。
建议在Page_Init期间加载动态控件,因为我们可能希望在早期阶段将事件与适当的处理程序相关联。
protected void Page_Init(object sender, EventArgs e)
{
///code to create dynamic controls
}
Button OnClick事件只在按钮位于表单元素中时触发。
把你的HTML放到一个表单里。
我用你的代码尝试了一下,并且能够看到当用户点击按钮时search_Click被调用。