Pass Session Variable with hyperlinkfield in gridview - asp.net

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.

Related

click event to update the gridview in ASP

It convenient to do the query by :
<asp: AccessDataSource >
I wish to update the query string and update the result by a click event.
<asp:Button id="Button1"
Text="To Inquiry"
OnClick="doInquiry"
runat="server"/>
How I can achieve to update the query string and do the query in in the "doInquiry" function; BUT, still using the following useful code.
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.MDB" SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
Whenever you generate the HTML via the markup and the behavior is different from what you expect, check the generated HTML in your browser. You intended to set the onclick attribute of an HTML tag, however, OnClick is a property pointing to the server-side method, which manages the server-side click event. You intended to use the OnClientClick property.

asp.net hyperlinkfield set navigateurl "http=blabla/Id=" value from database

I am new to asp.net. Please help.
I have 2 files. One default.aspx, one default.aspx.cs.
In the default.aspx, I have:
<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3366FF" HeaderStyle-ForeColor="White">
<rowstyle backcolor="LightCyan" forecolor="Black" font-italic="true"/>
<alternatingrowstyle backcolor="PaleTurquoise" forecolor="Black" font-italic="true"/>
<Columns>
<asp:BoundField ItemStyle-Width="15%" DataField="ID" HeaderText="ID" />
<asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
<asp:BoundField ItemStyle-Width="15%" HeaderText="Delayed Delivery" />
</Columns>
</asp:GridView>
My questions:
Column: hyperlink Log:
I want to bind it to a column in database called "Base_Id", but when I use DataField="Base_Id" in the hyperlink definition, it gives error;
I want to append the base_Id to the end of the url so that it gives navigateurl="http://somelink&RecordId=baseId_value" ; I tried TemplateField, but Visual Studio doesnot recognize it.
for the delayed delivery, I need to compare the data and do some math. Is there a way for me to add a function in default.aspx.cs? if there's, could anyone give an example please?
Any ideas are appreciated. Thanks a lot. =)
Convert the column
<asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
to TemplateColumn and do this
<asp:TemplateField HeaderText="Log" ItemStyle-Width="15%">
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%# GetUrl(Eval("Base_Id"))%>'
text="Log"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
and in code-behind
protected string GetUrl(object id)
{
return "http://somelink&RecordId=" + id;
}
Hope this helps!
Regards,
Uroš

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

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

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