how can apply hyperlink inside gridview

Posted by Bhuwan87rawat under ASP.NET on 9/8/2012 | Points: 10 | Views : 7998 | Status : [Member] | Replies : 6
Respected Sir,
In my application i take a gridview ,and bind with database,
in the database one column is url(in url take different different companies url)
so with the help of grid view i want to click any url in url column then open the url .



Regard
Bhuwan Rawat




Responses

Posted by: Shameer.Ali87 on: 9/8/2012 [Member] Starter | Points: 25

Up
0
Down
that URL'S in the URL column are predefined in the database right..


shameer ali shaik

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 9/8/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this,

<asp:TemplateField HeaderText="URL">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Column_Name") %>'
Text='<%# Eval("Column_Name") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>




Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 9/8/2012 [Member] Starter | Points: 25

Up
0
Down
hi bhuwan,
After you bind the data source to the gridview.
You can re-assign the column value as a link.
(OR)
You loop through the URL column values in the gridview and assign the text to a LinkButton. So that your column value becomes link button.

Here i am presenting you an example:
I have a table "WebSiteLinksTable" with "SiteName" & "NavigateSiteUrl" as columns with values as Eg: "Google" , "http://www.google.com".

.aspx Page:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="SiteName" HeaderText="WebSiteName" />
</Columns>
</asp:GridView>

.aspx.cs:

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}

private void GetData()
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Select * from WebSiteLinksTable";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(table);
}
}
}
grid.DataSource = table;
grid.DataBind();
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Cells[0].Text = "<a href=\"" + table.Rows[i].Field<string>("NavigateSiteUrl") + "\">" + grid.Rows[i].Cells[0].Text + "</a>";
}
}

Onclick of the row goes to the corresponding link.

Regards,

Awesome Coding !! :)

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jimmyhelu on: 9/8/2012 [Member] Starter | Points: 25

Up
0
Down
In my application I have a GridView that works smoothly. ... then you can use a HyperLink instead of a HyperLinkField, which will have an Id.

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudeesh.Govind on: 9/13/2012 [Member] Starter | Points: 25

Up
0
Down
Hi in the item template of gridview add a anchor tag and bind the url to the href of a tag using <%Eval("")%>

learn and earn

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/13/2012 [Member] Starter | Points: 25

Up
0
Down
Nice explanation vasanth

Thanks & Regards
Hari

Bhuwan87rawat, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response