gridview with checkboxes problem (loosing rows count)...

Discussion in 'ASP .Net' started by trint, Sep 5, 2007.

  1. trint

    trint Guest

    I have a gridview that has checkboxes that works fine on a single page
    where it is created and loaded in the "protected void Page_Load(object
    sender, EventArgs e)".

    However, in a separate file where I want the gridview with checkboxes
    to be created by a button (not page_load), it works fine also UNTIL, I
    click the "remove selected items" button...as soon as I do that, rows
    >>count is 0, not 11 or whatever. The funny thing is, it is the same

    code as the other page that works fine and the "remove selected items"
    button, when clicked, still has 11 rows>>count.

    Any ideas on why this happens? Can I only have this gridview created
    in the page_load in order for it to work? Here is some of the code
    that may show what I'm just not seeing:

    This one works:

    <asp:GridView ID="GridView2" runat="server"
    AutoGenerateColumns="False" CssClass="basix"
    DataKeyNames="id"
    OnSelectedIndexChanged="getSelected" BorderColor="Gainsboro" Font-
    Bold="True" ForeColor="MidnightBlue" Width="431px"
    EnableViewState="False" >
    <HeaderStyle BackColor="Gainsboro"
    ForeColor="MidnightBlue" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton
    ID="Linkbutton1" runat="server" CommandName="select" Text='<%# Eval
    ( "id" ) %>'></asp:LinkButton>
    </ItemTemplate>
    <ItemStyle
    Wrap="False" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    <HeaderTemplate>
    OrderID
    </HeaderTemplate>
    <ControlStyle
    ForeColor="Black" />
    </asp:TemplateField>
    <asp:boundfield
    headertext="Order Date"
    datafield="order_date"
    dataformatstring="{0}" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:boundfield
    headertext="First Name"

    datafield="shipping_firstname"
    dataformatstring="{0}" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:boundfield
    headertext="Last Name"

    datafield="shipping_lastname"
    dataformatstring="{0}" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:boundfield
    headertext="Total Charge"
    datafield="total_charge"
    dataformatstring="{0:c}"
    HtmlEncode="False" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:TemplateField
    HeaderText ="Remove?">
    <ItemTemplate>
    &nbsp;<asp:CheckBox
    ID="ProductSelector" runat="server" />
    </ItemTemplate>
    <HeaderTemplate>
    Remove?
    </HeaderTemplate>
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="WhiteSmoke" />
    <RowStyle BackColor="White" ForeColor="Black" /
    >

    <SelectedRowStyle BackColor="Gainsboro"
    ForeColor="Black" />
    </asp:GridView>


    protected void Page_Load(object sender, EventArgs e)
    {
    if (Class1.loginUserFirstName == "")
    {
    Label1.Visible = false;
    GridView2.Visible = false;
    GridView1.Visible = false;
    Button5.Visible = false;
    Label2.Visible = false;
    Button1.Visible = false;
    Button2.Visible = false;
    Button3.Visible = false;
    Button4.Visible = false;

    }
    else
    {
    //this.Button1.Click += new
    System.EventHandler(this.Button1_Click1);
    //Choose order
    SqlCommand cmdChooseOrder = new SqlCommand(@"SELECT id,
    shipping_firstname, shipping_lastname, order_date, total_charge FROM
    orders where user_id = '" + Class1.loginUserid.Trim() + "' ORDER BY
    order_date DESC", new SqlConnection(@"Password=;Persist Security
    Info=True;User ID=;Initial Catalog=;Data Source="));

    try
    {
    cmdChooseOrder.Connection.Open();
    GridView2.DataSource = cmdChooseOrder.ExecuteReader();
    GridView2.DataBind();
    cmdChooseOrder.Connection.Close();
    cmdChooseOrder.Connection.Dispose();

    }
    catch (SqlException sqlExc)
    {
    //TextBox1.Text = sqlExc.ToString();
    }

    }
    }

    This one works until the "remove selected items" button is clicked,
    then rows>>count is at 0:

    <asp:GridView ID="GridView8" runat="server"
    AutoGenerateColumns="False" CssClass="basix"
    DataKeyNames="id"
    OnSelectedIndexChanged="getSelected" BorderColor="Gainsboro"
    Font-Bold="True" ForeColor="MidnightBlue" Width="431px"
    EnableViewState="False" >
    <HeaderStyle BackColor="Gainsboro"
    ForeColor="MidnightBlue" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton
    ID="Linkbutton9" runat="server" CommandName="select"
    Text='<%# Eval ( "id" ) %>'></asp:LinkButton>
    </ItemTemplate>
    <ItemStyle
    Wrap="False" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    <HeaderTemplate>
    OrderID
    </HeaderTemplate>
    <ControlStyle
    ForeColor="Black" />
    </asp:TemplateField>
    <asp:boundfield
    headertext="Order Date"
    datafield="order_date"
    dataformatstring="{0}" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:boundfield
    headertext="First Name"

    datafield="shipping_firstname"
    dataformatstring="{0}" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:boundfield
    headertext="Last Name"

    datafield="shipping_lastname"
    dataformatstring="{0}" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:boundfield
    headertext="Total Charge"
    datafield="total_charge"
    dataformatstring="{0:c}"
    HtmlEncode="False" >
    <ItemStyle
    HorizontalAlign="Right" />
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:boundfield>
    <asp:TemplateField
    HeaderText ="Remove?">
    <ItemTemplate>
    &nbsp;<asp:CheckBox
    ID="ProductSelector" runat="server" />
    </ItemTemplate>
    <HeaderTemplate>
    Remove?
    </HeaderTemplate>
    <HeaderStyle
    ForeColor="MidnightBlue" />
    </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="WhiteSmoke" />
    <RowStyle BackColor="White" ForeColor="Black" /
    >

    <SelectedRowStyle BackColor="Gainsboro"
    ForeColor="Black" />
    </asp:GridView>

    else
    {
    //this.Button5.Click += new
    System.EventHandler(this.Button5_Click5);
    //Choose order
    SqlCommand cmdChooseOrder = new
    SqlCommand(@"SELECT id, shipping_firstname, shipping_lastname,
    order_date, total_charge FROM orders where user_id = '" +
    Class1.loginUserid.Trim() + "' ORDER BY order_date DESC", new
    SqlConnection(@"Password=;Persist Security Info=True;User ID=;Initial
    Catalog=;Data Source="));
    //SqlCommand cmdChooseOrder = new
    SqlCommand(@"Select product_id, quantity, prod_name, price, Total =
    (price * quantity) from order_items where order_id = '" +
    Class1.getSelectedOrderID.Trim() + "' ", new
    SqlConnection(@"Password=;Persist Security Info=True;User ID=;Initial
    Catalog=;Data Source="));

    try
    {
    cmdChooseOrder.Connection.Open();
    GridView8.DataSource =
    cmdChooseOrder.ExecuteReader();
    GridView8.DataBind();
    cmdChooseOrder.Connection.Close();
    cmdChooseOrder.Connection.Dispose();
    loginIfNot1.Visible = false;
    Label1.Visible = false;
    //SetFocus(Label15);
    }
    catch (SqlException sqlExc)
    {
    //TextBox1.Text = sqlExc.ToString();
    }

    }

    Now when this button is clicked, there are no rows>>count (0):

    protected void Button5_Click5(object sender, EventArgs e)
    {
    formViewCell1.Visible = true;
    table01.Visible = true;
    GridView8.Visible = true;
    // Iterate through the Products.Rows property
    foreach (GridViewRow row in GridView8.Rows)
    {
    // Access the LinkButton
    string linkButtonText =
    ((LinkButton)row.FindControl("Linkbutton9")).Text;
    Response.Write(linkButtonText);
    // Access the CheckBox
    CheckBox cb =
    (CheckBox)row.FindControl("ProductSelector");
    if (cb != null && cb.Checked)
    {

    Any help is very much appreciated.
    Thanks,
    Trint
     
    trint, Sep 5, 2007
    #1
    1. Advertising

  2. 1) Since both grids have EnableViewState="false" the state would be
    lost at each post back.

    2) Difference is because when you are binding grid in Page_Load, the
    rows are being bound again and again at each postback, regardless of
    how the postback occured. But when you are binding it in click
    handler, it is not being bound since that click handler is not being
    called at next postback.
     
    Muhammad Naveed Yaseen, Sep 5, 2007
    #2
    1. Advertising

  3. trint

    trint Guest

    On Sep 4, 9:48 pm, Muhammad Naveed Yaseen <> wrote:
    > 1) Since both grids have EnableViewState="false" the state would be
    > lost at each post back.
    >
    > 2) Difference is because when you are binding grid in Page_Load, the
    > rows are being bound again and again at each postback, regardless of
    > how the postback occured. But when you are binding it in click
    > handler, it is not being bound since that click handler is not being
    > called at next postback.


    Muhammad,
    You solved my problem...that worked perfectly!
    Thanks,
    Trint
     
    trint, Sep 5, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MC
    Replies:
    2
    Views:
    8,323
  2. Mr. SweatyFinger

    count dynamic checkboxes

    Mr. SweatyFinger, Dec 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    537
    Masudur
    Dec 1, 2006
  3. Dan
    Replies:
    5
    Views:
    3,313
    shawpnendu
    May 26, 2009
  4. VijayRama
    Replies:
    2
    Views:
    2,385
    Gregory A. Beamer
    Oct 12, 2009
  5. MC

    Gridview returns old value of gridview.rows.count!

    MC, Apr 26, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    342
Loading...

Share This Page