Insede of ItemDataBound

Posted on 2006-03-29
Last Modified: 2008-01-09
Hello I have a datagrid with template items - it's fairely simple, one column of checkboxes, and three columns of the labels (one label in one column). It's implemented like that (that's for one column):

<asp:TemplateColumn HeaderText="Item Description">
            <asp:Label Runat="server" ID="Label16">Item Description</asp:Label>
            <asp:Label ID="lblDescription" Runat="server" Width="250">
                  <%# DataBinder.Eval( Container.DataItem, "description" ) %>

and everything works fine.

I need to do some changes depending on to what my labels are bound - enabling or disable checkboxes in the first column - and I'm trying to check it like that:

if( (( Label )e.Item.Cells[ 3 ].Controls[ 1 ]).Text != someTextContro.Text ) { ... }

inside ItemDataBound and it is always empty... I checked MSDN and it's said this event is called AFTER the item is bound, so presumable if the item IS bound, controls inside this item should be bound as well and should contain some data... Yet it's not working this way.

I can suggest that controls inside the item are bound later, but what do I do in this case?? What is the event I can use to enable/disable my controls depending on what my controls inside items were bound to.


Question by:Yurich
    LVL 10

    Accepted Solution

    u can simply do so

    1. go to design view of your datagrid, in "Properties" ,click "Events" tab, double click "ItemDataBound"
    2. this will bring you to code behind
    3. then u do sth. like this-->

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        DataRowView dv = (DataRowView)e.Item.DataItem;
        Label lblDescription= (Label)e.Item.FindControl("lblDescription");
        lblDescription.Text = dv["description"];
        //then do your modification
        if (lblDescription.Text == "")
    LVL 21

    Author Comment


    I have exception "specified cast is not valid" on this line:

    DataRowView dv = ( DataRowView )e.Item.DataItem;

    any ideas?

    LVL 21

    Author Comment

    found how to do it:

    System.Data.Common.DbDataRecord ddr = ( System.Data.Common.DbDataRecord )e.Item.DataItem;
    string ecert = ddr[ "description" ].ToString();

    All credit goes to you as you pointed to the right direction and I was completely stuck :)

    Thank you
    LVL 10

    Expert Comment

    great that u've solved this!
    thanks a lot :-)

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now