Hyperlink in datagrid view - asp.net

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>

Related

how to get textbox value from a gridview control in a webpage using C#

I tried the below code for fetching values from text box present in gridview but the text value shows blank "".
what's the issue with this code??
TextBox box1 = (TextBox)grdCountry.Rows[rowIndex].Cells[0].FindControl("TextBox1");
design code :
<asp:gridview ID="grdCountry" runat="server" ShowFooter="true"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CountryName" HeaderText="Country" ItemStyle-Width="200px" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Try define a name for textbox, and in c# use "nameTextBox.Text();"
The issue was resolved.The above code works properly.I was refreshing the whole page hence the data stored in textbox was showing blank data.

passing a querystring from one of the griditems bound columns

I have a gridview, one of the item in the gridview is
<asp:GridBoundColumn DataField="Id" UniqueName="Id" DataType="System.Int32" Visible="false"></asp:GridBoundColumn>
and another item is
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg" PostBackUrl="~/Viewer/ViewerSummary.aspx?QueryID=" runat="server" />
want to pass Id from the asp:GridBoundColumn as a querystring to the postbackurl of the asp:imageButton.
How can I achieve this?
You can try with this code
PostBackUrl='<%# "~/Viewer/ViewerSummary.aspx?QueryID=" + DataBinder.Eval(Container.DataItem,"ID") %>'
Since you are using an ImageButton then that means you are using an ItemTemplate. You can do something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg"
PostBackUrl='<%#string.Format("~/Viewer/ViewerSummary.aspx?QueryID={0}",Eval("Id")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>

Displaying an image icon in a gridview column with a href value in asp.net

I have a gridview column in which I have one column that point to a pdf file on the file server. I need to have another column right beside this, displaying the pdf icon. The user should be able to click the icon and launch the file from the file server.
My code is:
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I add a "NavugateURL" or href="" to the img ?
Rather than using an HTML img control, try using an ASP.Net Server control.
A good choice would be the Hyperlink Control:
Instead of:
<img runat="server" src="Images/img_Pdf.gif" id="imgFileType" />
Use:
<asp:HyperLink ID="imgFileType" ImageUrl="Images/img_Pdf.gif" NavigateUrl="" runat="server">HyperLink</asp:HyperLink>
Just set your NavigateUrl property.
you need to wrap your icon around an anchor tag, and set the href of the anchor tag using DataBinding expression Eval. This assumes your Datasource field "PDFPath" is an absolute path.
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><a href='<%#Eval("PDFPath")%>'> <img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></a></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
inside the RowDataBind event look for your image control and set its NavigateUrl property
protected void gvInvoices_RowDataBound(object Sender , GridViewRowEventArgs e)
{
if(e.Row.RowType==DataRow)
{
HtmlControl icon = e.Row.FindControl("imgFileType") as HtmlControl;
icon.NavigateUrl = ((MyDataType)e.Row.DataRow).PDFPath;
}
}
Note it is free hand writing so you may find some syntax errors that you should fix

I cannot make the headers of my gridview click able for sorting

I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField

loading gridview with hyperlink column

I have a gridview and added a column "Hyperlink" to all records by enabling autogeneratefields.
When this gridview is loaded and when I click the hyperlink across any record I want to redirect to some other page with entire record passed as query string to that page?
can anybody help me on this?
These links should clarify how to do it:
How to pass variables thru a DataGrid hyperlink column
How To: Use a HyperLink control inside a GridView
Sample code (Look at the NavigateUrl property of HyperLink):
<asp:GridView ID="urlGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl='<%# "RedirectPage.aspx?xxxx=" &
DataBinder.Eval(Container, "DataItem.xxxx") &
"&yyyy=" & DataBinder.Eval(Container, "DataItem.yyyy")%>'
Text="Go!">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SiteName" HeaderText="Site Name" />
</Columns>
</asp:GridView>

Resources