Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
trint - 05 Sep 2007 02:04 GMT
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
Muhammad Naveed Yaseen - 05 Sep 2007 02:48 GMT
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.
trint - 05 Sep 2007 03:36 GMT
> 1) Since both grids have EnableViewState="false" the state would be
> lost at each post back.
[quoted text clipped - 4 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.