Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    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    (11)

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"

Wellplayed.net
This article has been view 227 times.

Advertisement:

Comments

Bill Beckelman

Avatar

10/2/2008 4:46:22 PM

Kris,

I know a lot of people won't like it, but another option is to use SelectedValue='<%# Eval("whatever") %>' in the markup. Intellisense doesn't list SelectedValue as an option when working with the DropDownList but it works fine.

Bill
Kris

Avatar

10/2/2008 9:11:38 PM

Bill,

Thanks for pointing that out. That certainly is another option for setting the selected value of a dropdown. It's something I tend to avoid personally though, as I find my code more readable when I can see what is being selected in the code behind page and also it just seems easier to debug when it's in the code behind too.

Are there any real advantages to using this method? It almost seems like a personal preference thing to me.

Krish="
Bill

Avatar

10/3/2008 4:49:09 AM

Kris,

Like I said in the first comment, I don't think people will like it and it's for the reason you pointed out.

The place I have found it the most handy is in the EditItemTemplate of the ASP.NET ListView control when quickly developing CRUD pages.

Like you said, just a preference thing, but is always good to have another way. I would think it would have to be cleaner than adding extra hidden fields and such though?

Bill"0"<
Kris

Avatar

10/3/2008 6:52:16 AM

Yeah, probably easier than using hidden fields. Thankfully now though I've discovered that I don't have to do that anymore :)"0"
Micah

Avatar

11/5/2008 10:23:36 AM

I had been wondering about how to use the rowdatabound event... glad i stumbled upon this articleisplay:none"></ifra
Neil Jennings

Avatar

1/13/2009 2:08:49 AM

was looking for something else when I came across this - OH WHAT A FOOL I HAVE BEEN
thanks will use this concept in future"0" s
theAsocialApe

Avatar

4/17/2009 9:55:21 AM

Perfect example. Thanks. Been using text='<%# databinder.eval( ... all along, but just mechanically.

Now I can use it in rowdatabound too.
mobbzbobz

Avatar

6/3/2009 7:48:48 AM

Any idea why I would get a "Object reference not set to an instance of an object" when I try to use

DataBinder.Eval(e.Row.DataItem, "columnname").ToString();

in my onrowdatabound event?
m
mobbzbobz

Avatar

6/3/2009 10:07:30 AM

I figured it out, I was not checking this,

if (e.Row.RowType == DataControlRowType.DataRow)
isplay:none"></ifram
gfh

Avatar

11/9/2009 2:19:22 AM

fghgfhfghfgh
























































































































































isplay:n
Irfan

Avatar

7/30/2010 3:55:52 AM

No matter if anyone likes it or not...I liked the way u implemented it.

Thanks. src=


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.