GridView databinding question
In one of my table I have three following fields: id, title, content
If I databind the table data to a GridView
I want to have a link to the title using the format of displaycontent.aspx?id='id'.
My question is if I don't show the id field in the gridview, which I don't bind id field to it. How can I ge开发者_如何学Ct id value in datarowbind event?
Juse make a template field and localize all the customization of the binding to the control's OnDataBinding
event. For some reason most people do this on the RowDataBound
event which I do not recommend. There is no reason to have to search for controls using the controls DataBinding and it also allows for the customizations to be localized and easy to swap out without having to affect anything else. Imagine if you had 20 controls in your grid that required DataBinding
customizations of some sort. Your RowDataBound
event would be a mess and have to know about everything in the grid which could be easily buggy.
Example:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" ID="lnkYourLink"
OnDataBinding="lnkYourLink_DataBinding" />
</ItemTemplate>
</asp:TemplateField>
In codebind:
protected void lnkYourLink_DataBinding(object sender, System.EventArgs e)
{
HyperLink lnk = (HyperLink)(sender);
lnk.Text = Eval("Title").ToString();
lnk.NavigateUrl = string.Format("displaycontent.aspx?id={0}",
Eval("ID").ToString())
}
I prefer this method as well to inline code as it doesn't clutter your markup with any logic as well. If the next day you need it to be a LinkButton
you can easily swap it out without touching any other code that is unrelated to the HyperLink
.
I think you can do something like:
NavigationUrl='displaycontent.aspx?id=<%#Eval("Id")%>'
And you don't need to bind the Id column
I didn't test but I'm sure that's the idea.
There are several ways to do this. You actually don't need to subscribe to the RowDataBound
Event for this.
You need a TemplateField
column, which contains a UserControl that suffices your needs (HyperLink
, LinkButton
, or whatever)
Let's assume your TemplateColumn looks like this:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" ID="hlContent" Text="Details" />
</ItemTemplate>
</asp:TemplateField>
You can have the databinding in the markup, as Icarus suggested. Means, that your HyperLink would look like this (untested):
<asp:HyperLink runat="server" ID="hlContent" Text="Details" />
The other option would be to use the RowDataBound
Event, as you have asked for it in your question:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow) // neccessary to differentiate between content, header and footer
{
HyperLink hlContent = e.Row.FindControl("hlContent") as HyperLink;
YourObject dataItem = e.Row.DataItem as YourObject; // Depending on what datatype your e.Row.DataItem is. Take a look at it which the QuickWatch
hlContent.NavigateUrl = String.Format("displaycontent.aspx?id={0}", dataitem.id);
}
}
Edit: After posting several answers about how to use the RowDataBound event, I decided to write an article elaborating it: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
精彩评论