Trying to figure out why I'm having a problem getting GridView1 to databind
HTML:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
CellPadding="10" RowStyle-VerticalAlign="Top"
BackColor="White" BorderColor="Black" BorderWidth="1px" OnRowCreated="GridView1_RowDataBound"
Width="100%" AllowPaging="true" BorderStyle="Solid" PagerSettings-Position="TopAndBottom">
<Columns>
<asp:TemplateField HeaderText="Last" >
<ItemStyle VerticalAlign="Top" />
<ItemTemplate>
<%#Eval("Last")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First" >
<ItemStyle VerticalAlign="Top" />
<ItemTemplate>
<%#Eval("First")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supervisor ID" >
<ItemStyle VerticalAlign="Top" />
<ItemTemplate>
<%#Eval("supervisorId")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// EdT: if there is no value in session["userId"], redirect user to login page
if ((HttpContext.Current.Session["userId"]) == null)
{
Response.Redirect("Default.aspx");
}
else
{
userName = SqlHelperClass.GetUserName((int)HttpContext.Current.Session["userId"]);
timeCard = new TimeCard((int)HttpContext.Current.Session["userId"], GetCurrentPayPeriod());
// EdT: Set value of literal
Literal1.Text = userName;
// EdT: Set default values for SelectParameters
string userId = HttpContext.Current.Session["userId"].ToString();
string pped = Convert.ToString(GetCurrentPayPeriod());
SqlDataSource1.SelectParameters["user"].DefaultValue = userId;
SqlDataSource1.SelectParameters["pped"].DefaultValue = pped;
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// set value of dropdown list in GridView Row to the value contained in the timecard DataTable
if (e.Row.RowType == DataControlRowType.DataRow)
{
string dropDownListValue = CurrentTimeCard.TimeCardDataTable.Columns["projectName"].ToString();
DropDownList dropDownList = (DropDownList)e.Row.FindControl("DropDownList1");
dropDownList.DataSource = SqlDataSource2;
dropDownList.SelectedValue = dropDownListValue;
}
}
SqlDataSource1 markup:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MinnsTimeDatabase %>"
SelectCommand="spGetTimeCard" SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:Parameter Name="user" Type="Int32" />
<asp:Parameter DbType="Date" Name="pped" />
</SelectParameters>
</asp:SqlDataSource>
Any thoughts appreciated
You haven't mentioned what your actual problem is, but one issue I see is that you do not need to call DataBind() on a GridView when you are using the DataSourceID property (and thus a datasource control).
So I would definitely remove the call to GridView1.DataBind(); that is the else block of your Page_Load function. Since you are doing processing in the RowDataBound event, it's quite possible that the GridView databinding multiple times during the page life cycle is causing an issue.
Related
I am using gridview in a page of website that I am builing with asp.net so I am using sqldatasource component to fill gridview.now I insert new data into the table and page is refreshed but new data is not displayed in the gridview how to solve this problem
Bind your gridview again after inserting new data. It will take time but you have to fetch the data from database and bind it again to gridview.
Gridview.DataSource = yourDataSource;
Gridview.DataBind();
Bind the Grid in page_Init()
It bind the data from your database Every page init
Documentation on inserting using SqlDataSource - SqlDataSource.Insert Method()
And here's a complete, wroking example:
Code behind:
public partial class SqlDataSourceExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
}
}
.ASPX:
<form id="form1" runat="server">
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Surname] FROM [Person]"
InsertCommand="INSERT INTO [Person](Name,Surname)VALUES(#name,#surname)">
<InsertParameters>
<asp:ControlParameter ControlID="txtName" Name="name" />
<asp:ControlParameter ControlID="txtSurname" Name="surname" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
</Columns>
</asp:GridView>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Label ID="lblSurname" runat="server" Text="Surname"></asp:Label>
<asp:TextBox ID="txtSurname" runat="server"></asp:TextBox><br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</form>
I have a gridview.I have a hyperlink field and I want that when I click on hyperlink field, then row value is store in session and page redirect to other page. How can I do this?
Here is my aspx markup:
<asp:GridView ID="GridView1" runat="server" Height="36px"
style="margin-left: 270px; margin-top: 92px" Width="232px" CellPadding="3"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated"
onrowupdating="GridView1_RowUpdating" AutoGenerateColumns="False"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Table Name">
<EditItemTemplate>
<asp:TextBox ID="txtTBL" runat="server" Text='<%# Eval("Table_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Table_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Operation" ShowEditButton="True"
ShowDeleteButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink Text="Select" ID="lnkSelect" runat="server" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
And here is my code behind :
public partial class DisplayTable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Retrieve database gridview
gettable();
}
}
public void gettable()
{
// here is code
}
//here all code included regarding to edit,delete etc
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = ((HyperLink)GridView1.SelectedRow.Cells[0].Controls[0]).Text;
Session["destype"] = s;
Page.Response.Redirect("home.aspx");
}
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
}
Hope following will work for you, Not sure as not tested by me.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gr = ((sender as HyperLink).NamingContainer as GridViewRow);
Session["destype"] = gr.Cells[0].Text.Trim(); /*For first cell value of Row */
//Session["abc"] = gr.Cells[2].Text.Trim(); /*Repeat for other cell values of Row by increasing cell index */
Response.Redirect("~/home.aspx");
}
Also have look on Send (Pass) GridView Row Values to next page using Session variable in ASP.Net
I've never been a fan of accessing information by cell reference when it comes from a backing store like a database.
There are a number of ways to accomplish this. I prefer making the information available to the link button.
In your markup you can do this:
<ItemTemplate>
<asp:LinkButton Text="Select" ID="lnkSelect" runat="server"
data-tablename='<%# Eval("Table_Name") %>'
CommandName="Select"
// And consider doing this as often as needed
// It should be self evident :)
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
/>
</ItemTemplate>
or equivalently, in the RowDatabound Event:
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.DataRow ) {
DataRowView drv = (DataRowView) e.Row.DataItem ;
LinkButton lnkSelect = (LinkButton) e.Row.FindControl("lnkSelect");
lnkSelect.Attributes["data-tablename"] = drv["Table_Name"];
}
}
then in SelectedIndexChanged
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gv = (GridView) sender;
LinkButton lnkSelect = ( LinkButton ) gv.Rows[ gv.SelectedIndex ].FindControl( "lnkSelect" );
Session[ "destype" ] = lnkSelect.Attributes[ "data-tablename" ];
Page.Response.Redirect("home.aspx");
}
or in RowCommand
protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e ) {
if ( e.CommandName == "Select" ) {
LinkButton lnkSelect = ( LinkButton ) e.CommandSource;
Session[ "destype" ] = lnkSelect.Attributes[ "data-tablename" ];
Page.Response.Redirect("home.aspx");
}
}
Addendum
NOTE: Hyperlinks are not reliable if you do not provide an href. Since you need to postback first before redirecting to home.aspx, change the HyperLink to a LinkButton. I've revised my entire answer accordingly.
In the aspx markup, modify the hyperlink with this:
<ItemTemplate>
<asp:LinkButton ID="lnkSelect" runat="server"
Text="Select"
CommandName="Select"
data-tablename='<%# Eval("Table_Name") %>' />
</ItemTemplate>
And in SelectedIndexChanged do this
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gv = (GridView) sender;
LinkButton lnkSelect = ( LinkButton ) gv.Rows[ gv.SelectedIndex ].FindControl( "lnkSelect" );
Session[ "destype" ] = (string) lnkSelect.Attributes[ "data-tablename" ];
Page.Response.Redirect("home.aspx");
}
I'm making a tour and travel website. But I'm stuck with a problem. I have a DropDownList and below it a GridView. As soon as I select a value in the DropDownList, the respective values in the GridView shows up. I have converted a column of a GridView into HyperLink in the code behind file. Each HyperLink navigate to a different url.
Markup page:
Select destination:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Make a selection</asp:ListItem>
<asp:ListItem Value="1">jaipur</asp:ListItem>
<asp:ListItem Value="2">manali</asp:ListItem>
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="package_id" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" AutoGenerateSelectButton="false">
<Columns>
<asp:BoundField DataField="Title_of_package" HeaderText="Title_of_package" SortExpression="Title_of_package" />
<asp:BoundField DataField="No_of_Days" HeaderText="No_of_Days" SortExpression="No_of_Days" />
<asp:BoundField DataField="Details" HeaderText="Details" SortExpression="Details" />
<asp:BoundField DataField="package_id" HeaderText="package_id" ReadOnly="True" SortExpression="package_id" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:registrationConnectionString %>" SelectCommand="SELECT [Title_of_package], [No_of_Days], [Details], [package_id] FROM [User_choice] WHERE ([Id_of_place] = #Id_of_place)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Id_of_place" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Code behind file in C#:
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the value in the hyperlink column.
string HyperLinkValue = e.Row.Cells[2].Text;
if (e.Row.Cells[3].Text == "100")
{
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "~/afterlogin/ChokiDhaniVisit.aspx";
myLink.Text = HyperLinkValue;
// then add the control to the cell.
e.Row.Cells[2].Controls.Add(myLink);
}
if (e.Row.Cells[3].Text == "101")
{
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "~/Manali.aspx";
myLink.Text = HyperLinkValue;
// then add the control to the cell.
e.Row.Cells[2].Controls.Add(myLink);
}
}
}
Now I want as soon as user click on the HyperLink column of a row its Title_of_package to get stored in a label. I have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label11.Text = ((HyperLink)GridView1.SelectedRow.Cells[0].Controls[0]).Text;
Label11.Text = Session["destype"].ToString();
}
But it is showing up error. I am a beginner.
I would suggest the following adding this to GridView1_SelectedIndexChanged
GridViewRow row = GridView1.SelectedRow;
HyperLink hyperLink = ((HyperLink)row.FindControl["hyperlinkname"]);
Label1.Text = hyperLink.Text;
you`r nearly there but the session is used as follows
Session["desType"] = ((HyperLink)GridView1.SelectedRow.Cells[0].Controls[0]).Text;
and you get it like
Label1.Text = Session["desType"].toString();
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);
}
}
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]));
}
}
}
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.
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
I have used the paging in the gridview.
I need the code for check/uncheck in gridview either in server side or client side.
I have tried without paging, it's working. I need it with paging.
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHGrid" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkGrid" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
This technique is called maintaining the state of GridView CheckBox in Paging. One method to achieve this is as follows.
Create a Genric List of int or string to store the DataKeys related to Checked rows in it.
At PageIndexChanging before you set the PageIndex to new index. For each rows in GridView,
If checkbox is checked, store the id to the generic list.
If checkbox is unchecked & the ID exist in generic list, remove it from the list.
At RowDataBound event, for each DataRow, check if the DataKeyName is present in the Generic List. If so, find the CheckBox and make it checked.
A simple example with Products Table, Northwind Database is given below
The Markup
<asp:GridView ID="gvProducts" runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="ProductID"
OnPageIndexChanging="gvProducts_PageIndexChanging"
OnRowDataBound="gvProducts_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsProducts" runat="server"
ConnectionString='<%$ ConnectionStrings:NorthwindConnectionString %>'
SelectCommand="SELECT * FROM Products">
</asp:SqlDataSource>
The Code Behind
private List<int> ProductIDs
{
get
{
if (this.ViewState["ProductIDs"] == null)
{
this.ViewState["ProductIDs"] = new List<int>();
}
return this.ViewState["ProductIDs"] as List<int>;
}
}
protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
foreach (GridViewRow gvr in gvProducts.Rows)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect != null)
{
int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
if (chkSelect.Checked && !this.ProductIDs.Contains(productID))
{
this.ProductIDs.Add(productID);
}
else if (!chkSelect.Checked && this.ProductIDs.Contains(productID))
{
this.ProductIDs.Remove(productID);
}
}
}
}
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect != null)
{
int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
chkSelect.Checked = this.ProductIDs.Contains(productID);
}
}
}