asp grid view check box to presrver on neigation(paging) - asp.net

In a grid view i have used paging for that i have used the view state to store datatale to bind it on GridView1_PageIndexChanging event every thing works fine but the problem happens with the first column which is having the checkbox placed in each row .
On navigation all checked check box becomes unchecked how to maintain the state of check box as well.
this is the aaspx code
<Columns>
<asp:TemplateField HeaderText="Select Student">
<ItemTemplate>
<asp:CheckBox id="Chek" runat="server" Text="select" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Enrollment No." DataField="enrollment_no" />
<asp:BoundField HeaderText="Course Name" DataField="course_name"/>
<asp:BoundField HeaderText="Branch Name" DataField="branch_name"/>
<asp:BoundField HeaderText="Email Id" DataField="email" />
<asp:BoundField HeaderText="Mobile" DataField="mobile"/>
<asp:BoundField HeaderText="Name" DataField="first_name"/>
<asp:BoundField HeaderText="Surname" DataField="last_name" />
</Columns>
</asp:GridView>

Viewstate is intended for postbacks to the same page.
To preserve state when navigating to other pages here are 3 options:
Put your checkbox (or simply true/false) in the Session
Use the PreviousPage property
Or use cookies
Summary of option #2
If you have ot post accross pages,
cookies can be used, and also Cross
Page Posting by setting the
PostBackURL property of a button, then
the POST request is directed at the
specified page, and you can get the
values from the PreviousPage property
of the next page.
Example of using option #3, the Session:
//Set
Session["mySessionVariableName"] = myCheckBox;
//Get
CheckBox myCheckBox = (CheckBox)Session["mySessionVariableName"];
I summarized in more detail here and here

Related

Dropdownlist autopostback property is getting triggered even when I click any other options in gridview or on any other control on the same page

The dropdownlist and the gridview are on the same page.
Code for dropdownlist :-
<asp:DropDownList OnSelectedIndexChanged="drplist_SelectedIndexChanged"
AutoPostBack="true" ViewStateMode="Disabled" AppendDataBoundItems="true"
ID="drplist" runat="server" DataSourceID="datasource1"
DataTextField="User_ID" DataValueField="User_ID">
<asp:ListItem Value="" Text="Make a selection"/>
</asp:DropDownList>
Code for gridview :-
<asp:GridView ID="gview" runat="server" Width="100%" AutoGenerateColumns="false" DataKeyNames="User_ID" DataSourceID="datasource">
<Columns>
<asp:BoundField DataField="User_ID" HeaderText="User ID" />
<asp:BoundField DataField="User_Name" HeaderText="User Name" />
<asp:BoundField DataField="User_Email" HeaderText="User e-mail" />
<asp:BoundField DataField="User_Mob" HeaderText="User mobile number" />
<asp:BoundField DataField="User_Created" HeaderText="Created Date" />
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
</Columns>
</asp:GridView>
The dropdown has User IDs from the database and when a selection is made from the dropdown it fires it selectedIndexChanged event , code for the same:-
protected void drplist_SelectedIndexChanged(object sender, EventArgs e)
{
var index = drplist.SelectedValue;
drplist.SelectedIndex = 0;
Response.Redirect("~/Demo/TableDemo.aspx?User_ID="+index);
}
Now when I click browser's back button , the dropdown still retains its selected value, which I do not want, I want it to reset to index 0. As I return to the dropdownlist from the TableDemo.aspx page and click on the edit button or delete button in gridview , the postback for the dropdown is triggered again taking me to the TableDemo.aspx page. Only a manual page refresh sets things straight. I tried to search and implement JS code for forcing page refresh on pressing browser back button but it didn't solve my issue and created more. I have tried using the update panel to my frustration and it happens to do nothing to solve my issue here. Sorry for the long post I couldn't do better in making the scenario shorter in writing here.
You probably have your page cached, thats the reason it doesn't behave the way you want when you click the back button. Try to add this code to your page_load on the page you don't want to be cahched.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Hope this helps!

Datalist.Visible always false?

I've done this a million times without a problem, and I'm stumped at this one.
I have a DataGrid:
<asp:DataGrid ID="gridDeptEmployees" runat="server" AutoGenerateColumns="False"
ItemStyle-CssClass="hovertable" ViewStateMode="Enabled">
<ItemStyle Font-Size="Smaller" />
<Columns>
<asp:TemplateColumn HeaderStyle-CssClass="hidden">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="firstname" HeaderStyle-CssClass="hidden" />
<asp:BoundColumn DataField="lastname" HeaderStyle-CssClass="hidden" />
<asp:BoundColumn DataField="employeeNumber" HeaderStyle-CssClass="hidden" />
</Columns>
</asp:DataGrid>
This datagrid is bound to a list of objects:
Dim userList as New List(Of User)
userList = getList()
gridDeptEmployees.DataSource = ulst
gridDeptEmployees.DataBind()
I can see users in the list when I set a breakpoint and step through it, but the grid is never visible. The visible property is always false!
I've tried explicitly setting the visible property, but it doesn't honor my changes. I even tried adding my DataKeyNames, but that didn't work.
What am I missing here? Should I switch to a different control?
When you can not see a control (not rendered on page), and debug it and see that the visible is false, then is probably because is inside some other hidden control, like a PlaceHolder, or a Panel, or a UserControl.
Check to see if a parent control is hidden to find your reason.

Pass Session Variable with hyperlinkfield in gridview

here is my gridivew:
<asp:GridView ID="gvBatters" runat="server" DataKeyNames="playerID"
AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkField DataTextField="nameFull" HeaderText="Player"
NavigateUrl="~/Pages/individualPlayer.aspx" />
<asp:BoundField DataField="g" HeaderText="G" />
<asp:BoundField DataField="avg" HeaderText="AVG" />
<asp:BoundField DataField="obp" HeaderText="OBP" />
<asp:BoundField DataField="slg" HeaderText="SLG" />
<asp:BoundField DataField="ops" HeaderText="OPS" />
<asp:BoundField DataField="rc" HeaderText="RC" />
<asp:BoundField DataField="h" HeaderText="H" />
<asp:BoundField DataField="doub" HeaderText="2B" />
<asp:BoundField DataField="trip" HeaderText="3B" />
<asp:BoundField DataField="hr" HeaderText="HR" />
<asp:BoundField DataField="rbi" HeaderText="RBI" />
</Columns>
</asp:GridView>
I want to pass the datakeyname "playerID" with the hyperlinkfield to the individualPlayer page. Id prefer to not use query strings, so i was thinking there must be a way to make the datakeyname a session variable. i can then use that variable for what i need, dont need query string. i just cant think of how to go about doing this
Instead of doing this you can also bind anchor tag from code behind as well. as you know that Hyper link is nothing but a anchor tag. so in this case you can bind anchor tag from code behind as
<a href='~/Pages/individualPlayer.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "Id") %>'>Player</a>
try this and if you want to put link name while binding you can also write like this
<a href='~/Pages/individualPlayer.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "Id") %>'><%# DataBinder.Eval(Container.DataItem, "PageName") %>
try this.
HyperLinkField will not work for this. What you want is a command field with a button type of link. On clicking the command, the RowCommand event fires on postback to the server, you'll write the session value, then use Response.Redirect. That is how this would have to be done.

How to display data from gridview

I simply added grid view and added columns and gave headertext
But when i run the application i am not able to see any grid,,atleast i should see grid column names
Do i need to do any thing more
Verify that you have everything wired up properly and are assigning a DataSource and doing a DataBind(). Once you have verified that these two things are happening then make sure that your DataSource is returning some type of result set with at least one item.
A GridView will not display anything at all unless there is at least 1 item in the result set. If you bind to a DataSet or some type of object list and there are not items in it then the grid will no display at all. Not even the headers. In this case you should setup the EmptyDataText property to display something.
If not if this helps, please post your GridView markup and the code where you bind your grid and I'll see if I can figure out what the issue is.
check aspx page code
<asp:MyGridView runat="server" DataKeyNames="pkey" AutoUpdateAfterCallBack="true"
Width="100%"
ID="grduser" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Sr No." DataField="rownumber" ReadOnly="true" HeaderStyle-Width="10px"
ItemStyle-Width="10px" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" SortExpression="FirstName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="LoginName" DataField="LoginName" SortExpression="LoginName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Email" DataField="Email" SortExpression="Email" ReadOnly="true"
HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Role" DataField="Role" SortExpression="Role" ReadOnly="true"
HeaderStyle-Width="30px" ItemStyle-Width="30px" />
<asp:BoundField HeaderText="Reportingto" DataField="Reportingto" SortExpression="Reportingto"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="MobileNo" DataField="MobileNo" SortExpression="Mobile_no"
ReadOnly="true" HeaderStyle-Width="30px" ItemStyle-Width="30px" />
</Columns>
</asp:MyGridView>
Cs file code to bind grid
DataSet ds = new DataSet();
ds = //get dataset form the database
DataView dv = new DataView(ds.Tables[0]);
this.grduser.DataSource = dv;
this.grdusers.DataBind();
have a look on http://msdn.microsoft.com/en-us/library/ms972948.aspx
Easiest way is as Kelsey says:
<emptydatatemplate>
No Data Found.
</emptydatatemplate>
Other techniques:
1) Override the CreateChildControls (example: http://forums.asp.net/t/1003306.aspx)
2) Manually insert a row (example: http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx)

Adding a validator to the gridview textbox, created in edit-mode of a bound field

take a look at this sample code: (question bellow)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated"
DataKeyNames="Product_Id">
<Columns>
<asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name"
ReadOnly="True" >
<ItemStyle Width="50px" Height="50px" Wrap="true"/>
</asp:ImageField>
<asp:BoundField DataField="Product_Id" HeaderText="Product_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
</asp:BoundField>
<asp:BoundField DataField="Product_Name" HeaderText="Product_Name"
SortExpression="Product_Name" />
<asp:BoundField DataField="Category_Name" HeaderText="Category_Name"
SortExpression="Category_Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size"
SortExpression="Size" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Assume I initialize an SqlDataSource, add a parameter and so on.
The thing is, that when a user clicks edit we get a textbox to edit the colnumn value.
I want to validate the data enter by the user before the update is performed and the new
data is propagated back to the server.How?
10x a lot!
You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.
Option 1:
But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.
ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2
Option 2:
But, we can do.
You need to add the validation either the javascript validation or server side validation
control, when the GridView's DataBound event is happening at runtime on the particular
TableCell of the Gridview rows.
Hence, when you click the update button that custom generated javascript or the validation
control will check for the validation on editing the values.
This process is more harder than the converting boundfield to templatefield
refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Option 3:
And you can go for server side validation on the values instead of client side validation:
refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx

Resources