0

我有一个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);

        }
    }
4

3 回答 3

0

将页面加载的所有代码放在 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]));

        }
    }


}
于 2013-07-22T09:32:58.643 回答
0

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

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

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

于 2013-07-22T09:40:49.947 回答
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 内

于 2013-07-22T10:25:20.440 回答