模板字段中的按钮赢得';t启动单击功能

本文关键字:启动 功能 单击 字段 按钮 | 更新日期: 2023-09-27 18:24:07

我有一个网格视图,它通过参数化的sqldatasource获取信息。我想通过按下一个按钮来启动一个函数,发送其中一个字段(id)。但功能甚至不会启动。。

这是我的aspx部分:

 <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:igroup20_test2ConnectionString %>" 
        SelectCommand="select mie.e_num, mie.id, m.f_name, m.l_name from memberInEvent mie, member m where e_num=@num and mie.id=m.id" >
        <SelectParameters>
            <asp:Parameter DefaultValue="066643776" Name="num" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:PlaceHolder ID="head_line_ph" runat="server"></asp:PlaceHolder>
    <br /><br />
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
            AutoGenerateColumns="false" CssClass="tableStatic">
        <Columns>
            <asp:TemplateField HeaderText="הסר מאירוע">
                <ItemTemplate>
                    <asp:Button ID="delete_mem" CommandArgument='<%# Bind("id") %>' runat="server" Text="הסר מאירוע" OnClick="remove_member" CssClass="btn btn-primary" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ReadOnly="True" HeaderText="ת.ז" 
                  InsertVisible="False" DataField="id"
                    SortExpression="ת.ז">
                </asp:BoundField>
                <asp:BoundField ReadOnly="True" HeaderText="שם פרטי" 
                  InsertVisible="False" DataField="f_name"
                    SortExpression="שם פרטי">
                </asp:BoundField>
                <asp:BoundField ReadOnly="True" HeaderText="שם משפחה" 
                  InsertVisible="False" DataField="l_name"
                    SortExpression="שם משפחה">
                </asp:BoundField>

        </Columns>
    </asp:GridView>

下面是我的代码:

protected void Page_Load(object sender, EventArgs e)
    {

        string e_num = Request.QueryString["enum"];
        Label headline_lbl = new Label();
        headline_lbl.Text = db.return_event_name(e_num);
        headline_lbl.CssClass = "head_line";
        head_line_ph.Controls.Add(headline_lbl);
        SqlDataSource2.SelectParameters["num"].DefaultValue = e_num;
        GridView1.DataSourceID = "SqlDataSource2";
        GridView1.DataBind();
        if (!IsPostBack)
        {
            List<string[]> ids_list = db.return_ids_for_event(Convert.ToInt32(e_num));
            foreach (string[] s in ids_list)
            {
                DropDownList1.Items.Add(new ListItem(s[0], s[1]));
            }
        }

    }
protected void remove_member(object sender, EventArgs e)
    {
        string mem_id = ((Button)sender).CommandArgument;
        db.remove_member(mem_id, num);
        Response.Redirect("memberInevents.aspx?enum=" + num);
    }

编辑:

在阅读了苏哈尼·莫迪的答案后,我把它改为对gridview的rowcommand开火:(但仍然没有开火)

<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server" DataSourceID="SqlDataSource1" 
            AutoGenerateColumns="false" CssClass="tableStatic">
        <Columns>
            <asp:TemplateField HeaderText="הסר מאירוע">
                <ItemTemplate>
                    <asp:Button ID="delete_mem" CommandArgument='<%# Bind("id") %>' CommandName="MyRowButton" runat="server" Text="הסר מאירוע"  CssClass="btn btn-primary" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ReadOnly="True" HeaderText="ת.ז" 
                  InsertVisible="False" DataField="id"
                    SortExpression="ת.ז">
                </asp:BoundField>
                <asp:BoundField ReadOnly="True" HeaderText="שם פרטי" 
                  InsertVisible="False" DataField="f_name"
                    SortExpression="שם פרטי">
                </asp:BoundField>
                <asp:BoundField ReadOnly="True" HeaderText="שם משפחה" 
                  InsertVisible="False" DataField="l_name"
                    SortExpression="שם משפחה">
                </asp:BoundField>

        </Columns>
    </asp:GridView>

cs:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "MyRowButton")
        {
            string mem_id = e.CommandArgument.ToString();
            db.remove_member(mem_id, num);
            Response.Redirect("memberInevents.aspx?enum=" + num);
        }
    }

模板字段中的按钮赢得';t启动单击功能

将页面加载的所有代码放在IsPostBack块中。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      string e_num = Request.QueryString["enum"];
      Label headline_lbl = new Label();
      headline_lbl.Text = db.return_event_name(e_num);
      headline_lbl.CssClass = "head_line";
      head_line_ph.Controls.Add(headline_lbl);
      SqlDataSource2.SelectParameters["num"].DefaultValue = e_num;
      GridView1.DataSourceID = "SqlDataSource2";
      GridView1.DataBind();
        List<string[]> ids_list = db.return_ids_for_event(Convert.ToInt32(e_num));
        foreach (string[] s in ids_list)
        {
            DropDownList1.Items.Add(new ListItem(s[0], s[1]));
        }
    }

}

如果控件(按钮)位于网格视图的一行中,则其事件将不会像普通按钮那样触发。这称为事件冒泡。如果控件位于容器内,则它们将成为该容器的子级。例如,在您的情况下,button是gridview的子项,在这种情况下,子项不能直接启动他们的事件。他们会将事件发送到他们的容器/父级,即在您的情况下的gridview,您需要处理该父级的事件,即gridview。

尝试使用网格视图的OnRowCommand事件。这应该会有所帮助。您可以使用"FindControl"方法在该行中查找按钮控件。

希望这能有所帮助!如果你需要进一步的帮助,请告诉我。

我想这是你的按钮

<ItemTemplate>
  <asp:Button ID="delete_mem" CommandArgument='<%# Bind("id") %>' runat="server" Text="הסר מאירוע" OnClick="remove_member" CssClass="btn btn-primary" />
</ItemTemplate>

更改为:

 <ItemTemplate>
    <asp:Button ID="delete_mem" CommandArgument='<%# Eval("id") %>' runat="server" Text="הסר מאירוע" CommandName="remove_member" CssClass="btn btn-primary" />
 </ItemTemplate>

现在处于网格视图中的rowcomand事件

protectected void Gv_RowCommand(object sender, GridRowCommandEventArgs e)
{
   if(e.CommandName.Equals("remove_member"))
   {
        string mem_id = e.CommandArgument.ToString();
        db.remove_member(mem_id, num);

   }
   System.Thread.Sleep(500); // To hold the current thread for few second to complete the operation and then redirect to your desired page
    Response.Redirect("memberInevents.aspx?enum=" + num);
}

请参阅此处你的旧密码。

SqlDataSource2.SelectParameters["num"].DefaultValue = e_num;
        GridView1.DataSourceID = "SqlDataSource2";
        GridView1.DataBind();
        if (!IsPostBack)
        {
            List<string[]> ids_list = db.return_ids_for_event(Convert.ToInt32(e_num));
            foreach (string[] s in ids_list)
            {
                DropDownList1.Items.Add(new ListItem(s[0], s[1]));
            }
        }

新代码:

        SqlDataSource2.SelectParameters["num"].DefaultValue = e_num;
        if (!IsPostBack)
        {
        GridView1.DataSourceID = "SqlDataSource2";
        GridView1.DataBind();
            List<string[]> ids_list = db.return_ids_for_event(Convert.ToInt32(e_num));
            foreach (string[] s in ids_list)
            {
                DropDownList1.Items.Add(new ListItem(s[0], s[1]));
            }
        }

问题:

绑定必须在内部!Ispostback