templatefield中的按钮不会启动Click功能

时间:2013-07-22 09:29:53

标签: c# asp.net gridview asp.net-controls

我有一个gridview,它通过参数化的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);
    }

修改

在阅读了Suhani Mody的回答后,我将其更改为触发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);

        }
    }

3 个答案:

答案 0 :(得分:0)

将所有页面加载代码放入IsPostBack Block。

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]));

        }
    }


}

答案 1 :(得分:0)

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

尝试使用gridview的OnRowCommand事件。这应该有所帮助。您可以使用“FindControl”方法在该行上找到您的按钮控件。

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

答案 2 :(得分:0)

我认为这是你的按钮

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

现在在gridviews 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