Adding values from a control into hyperlinkfield querystring - asp.net

<asp:HyperLinkField
DataTextField="USERNAME" HeaderText="USERNAME"
NavigateUrl="~/openOrdersDetails.aspx?lookup_id=" SortExpression="USERNAME"
Target="_blank" />
How can I write the code above so the NavigateUrl contains a value grabbed from a control on the page?
Eg,
NavigateUrl="~/openOrdersDetails.aspx?lookup_id=ValueFromControl"

Related

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.

asp.net gridview DataNavigateUrlFormatString from DataSource

I have a gridview that is being populated from a datasouce.
The Stored procedure that is populating the datasource, has a field "Client" and a field "Client WebSite".
I want to populate the field "Client" in the gridview column called "Client" which would be a hyperlink field and the hyperlink field would be the "Client WebSite" value from the dataset. The client website is an external site (not within my asp project)
Below is my html code. How can I get the "Client WebSite" appear as the DataNavigatrURL value?
<asp:HyperLinkField DataTextField="Client" HeaderText="Client" DataNavigateUrlFields="Client"
DataNavigateUrlFormatString="Client WebSite">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
Use databinding on the NavigateUrl attribute, like this:
NavigateUrl = '<%# Bind("ClientWebSite") %>'
Or more fully:
<asp:HyperLinkField DataTextField='<%# Bind("Client" %>' HeaderText="Client" NavigateUrl='<%# Bind("ClientWebSite") %>'>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizantalAlign="Left" />
</asp:HyperLinkField>
DataNavigateUrlFields is used to gets or set the names of the fields from the data source used to construct the URLs for the hyperlinks in the HyperLinkField object.
'DataNavigateUrlFormatString` is used to gets or sets the string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField object are rendered.

Hyperlink in datagrid view

I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>

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

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