I have a gridview with several columns of data. The gridview is populated by a SQL stored proc. I need to add a hyperlinked column that gets the value of one of the columns and adds it to a link to a different page - example
Gridview1:
Employee ID EmployeeName Department HyperlinkField
213 John Doe Accounting detailpage.aspx?employee.id=213
The problem is, I am very new to development and I do not know how to write a RowDataBound Event for my gridview.
Can anyone tell me how I would go about creating the event on the hyperlink field?
Jus paste this code below u r 'Department' column of GridView in .aspx page and do the needful changes such as the Text of HyperLink and the NavigatingUrl. NavigateingUrl is the Url wher u wann to redirect so give a appropriate one or else u may get error.
------------------------
Donn forget to mark as answer if it solves u r trouble
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "goto") //we could get the data which will be transfered from e.CommandArgument
{
Response.Redirect("~/data.aspx?cust_no=" + e.CommandArgument.ToString());
}
}
I look forward to receiving your test results.
Sincerely,
Zong-Qing Li
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
I went with steppppo's answer which worked with a bit of syntax tweaking. The NavigateUrl binding expression on my gridview template field was: Eval("ID", "DownloadFile.aspx?ID={0}") What I really wanted, however, was to figure out how to do this in the
code behind. My answer on that front was:
This is excellent. I needed to do the same thing. Only one thing... Where did you come up with "Hyperlink1" as the name of the control to find? I've tried using the header text and the display text. No good.. and, there's no ID tag for asp:HyperLinkField. I'll
keep searching, but if you can reply, it would be appreciated very much. Thanks again!
I just figured it out using the debugger, though it wasn't very straightforward. I had to poke around the object chain to find it. It seems like you should be able to put an ID value directly on the HyperLink tag.
JPO
Member
4 Points
19 Posts
How to Write GridView RowDataBound Event - C#
Oct 13, 2008 05:57 PM|LINK
I have a gridview with several columns of data. The gridview is populated by a SQL stored proc. I need to add a hyperlinked column that gets the value of one of the columns and adds it to a link to a different page - example
Gridview1:
Employee ID EmployeeName Department HyperlinkField
213 John Doe Accounting detailpage.aspx?employee.id=213
The problem is, I am very new to development and I do not know how to write a RowDataBound Event for my gridview.
Can anyone tell me how I would go about creating the event on the hyperlink field?
Thanks,
PJ
steppppo
Participant
780 Points
158 Posts
Re: How to Write GridView RowDataBound Event - C#
Oct 13, 2008 06:18 PM|LINK
hello friend,
you could write the data directly into the gridview's item template:
<a href="detailpage.aspx?employee.id=<%# Eval("EmployeeIDColumnNameHere", "{0}")%>Hyperlink Text Goes Here</a>
hope this helps.
JPO
Member
4 Points
19 Posts
Re: How to Write GridView RowDataBound Event - C#
Oct 13, 2008 06:45 PM|LINK
Thanks! Can you tell me how to write that into the item template?
steppppo
Participant
780 Points
158 Posts
Re: How to Write GridView RowDataBound Event - C#
Oct 13, 2008 07:06 PM|LINK
2. Select Item Template from the Display drop-down list
3. Add the above code to the field where you want the hyperlink to appear.
victor0282
Member
42 Points
47 Posts
Re: How to Write GridView RowDataBound Event - C#
Oct 13, 2008 07:44 PM|LINK
<asp:TemplateField HeaderText="HyperlinkField">
<ItemTemplate>
<asp:HyperLink ID="hlnk" runat="server"
NavigateUrl='<%# "~/detailpage.aspx?employee.id=" + Eval("EmployeeIdColumn") %>'
Text=" - - Hyperlink Text - - ">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Jus paste this code below u r 'Department' column of GridView in .aspx page and do the needful changes such as the Text of HyperLink and the NavigatingUrl. NavigateingUrl is the Url wher u wann to redirect so give a appropriate one or else u may get error.
------------------------
Donn forget to mark as answer if it solves u r trouble
Happy Codding
Zong-Qing Li...
All-Star
26878 Points
2165 Posts
Re: How to Write GridView RowDataBound Event - C#
Oct 15, 2008 04:02 AM|LINK
Hi,
As far as I can see, I suggest to use GridView_RowCommand instead of RowDataBound Event.
The code is shown below.
.aspx file
The code snippet of the GridView.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName="goto" CommandArgument='<%#Eval("cust_no") %>' runat="server">goto</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
.cs file
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "goto") //we could get the data which will be transfered from e.CommandArgument
{
Response.Redirect("~/data.aspx?cust_no=" + e.CommandArgument.ToString());
}
}
I look forward to receiving your test results.
Zong-Qing Li
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
JPO
Member
4 Points
19 Posts
Re: How to Write GridView RowDataBound Event - C#
Oct 15, 2008 01:19 PM|LINK
Thanks Everyone,
I went with steppppo's answer which worked with a bit of syntax tweaking. The NavigateUrl binding expression on my gridview template field was: Eval("ID", "DownloadFile.aspx?ID={0}") What I really wanted, however, was to figure out how to do this in the code behind. My answer on that front was:
protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink download = ((HyperLink)e.Row.FindControl("HyperLink1"));
DataRowView row = (DataRowView)e.Row.DataItem;
download.NavigateUrl = String.Format("DownloadFile.aspx?ID={0}", row["ID"]);
}
}
Thanks for all the responses!
PJ
johngalt
Member
4 Points
2 Posts
Re: How to Write GridView RowDataBound Event - C#
Nov 08, 2008 11:43 AM|LINK
johngalt
Member
4 Points
2 Posts
Re: How to Write GridView RowDataBound Event - C#
Nov 08, 2008 11:58 AM|LINK