0

I have a gridview, which gets info by parametrized sqldatasource. I want to fire up a function by pressing a button, sending one of the fields (id). But function won't even fire up..

here's my aspx part:

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

here's my code behind:

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

EDIT:

after reading Suhani Mody's answer I changed it to fire on the gridview's rowcommand like that: (but still doesn't fire up)

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

        }
    }
Dvirski
  • 321
  • 4
  • 16
  • 36

3 Answers3

0

Put the All code of page load in 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]));

        }
    }


}
Raghubar
  • 2,768
  • 1
  • 21
  • 31
0

If your control (button) is inside a row of a gridview,its event will not fire like how it does for normal buttons. This is called event bubbling. If your controls are inside a container, they become a child of that container. e.g. in your case, button is a child for gridview and in that case, child cannot fire their events directly. They will send their event to their container/parent i.e. gridview in your case and you need to deal with an event of that parent i.e. gridview.

Try to use OnRowCommand event of your gridview. That should help. You can use "FindControl" method to find your button control on that row.

Hope this helps! Let me know if you need further help.

Suhani Mody
  • 691
  • 6
  • 10
0

I think this is your button

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

Change it to :

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

Now in gridviews rowcomand event

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

See Here Your old code.

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

            }
        }

New Code:

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

            }
        }

Problem:

Binding must be inside !Ispostback

  • Check the viewstate of the grid and age –  Jul 23 '13 at 10:56
  • how do I do that and how will it help me? – Dvirski Jul 23 '13 at 16:02
  • There is property name EnableViewstate in evry server control. Check whether its true of false. Also search for the <%@ Page EnableViewState=”false” EnableSessionState=”ReadOnly” ...%> –  Jul 24 '13 at 09:47