Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Using DataBinder.Eval in the RowDataBound Event
Date 9/29/2008    Tags ASP.net    (1)

Often times in the RowDataBound event, I have a need to perform some logic based on a field in my data source that I don't need to display on the screen. When this happened, I was using things like HiddenFields or Labels to get at that data. It seems there is a much better way, using DataBinder in your code behind just like you do in the ASPX page, that I was simply unaware of.

Let's say I have a DropDownList named ddlMyList that I need to populate dynamically based on values in my database. My GridView looks like so:
<asp:GridView ID="MyGridView" 
    runat="server" 
    AutoGenerateColumns="False" 
    OnRowDataBound="MyGridView_OnRowDataBound"
    >
<Columns>
    <asp:TemplateField HeaderText="License">
    <ItemTemplate>
        <asp:DropDownList ID="ddlMyList" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
I then need to set the selected value of ddlMyList to the ID field in my data source. Previously what I would have done is create a HiddenField control or Label (which I would then hide), set the value of it to the ID, and then used this control in the RowDataBound event to get that ID value.

We really don't need to do that though... we can use DataBinder in our code behind and get that value directly.
protected void MyGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlMyList = (DropDownList)e.Row.FindControl("ddlMyList");

        // Do some logic to populate my ddlMyList with data

        ddlMyList.SelectedValue = DataBinder.Eval(e.Row.DataItem, "ID").ToString();

    }
}
I'm really amazed that I've been doing this for so long using HiddenFields and Labels and never realized that there was a so much simpler way to do this. I've looked at numerous tutorials online and until recently never seen it done any other way (I wish I had a link to where I first saw this, but I failed to keep a bookmark). I hope by mentioning it here, I'll save a few others the pain and suffering that I endured.

And maybe someone will tell me if this was just something obvious I completely missed or if you missed it too.

kick it on DotNetKicks.com
0"


This article has been view 14685 times.


Comments

Jakob

Avatar

10/12/2015 7:45:20 AM

The picture caption says "NAME the animal..."
How can it be wrong if I decide his name is Rufus??
Maybe word your own little version of "captcha" a bit better...?


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)