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

T

trint

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, rowscode 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>
<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>
<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
 
M

Muhammad Naveed Yaseen

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

trint

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top