Programmatically Change Text Property of Label in ItemTemplate in Datagrid

Discussion in 'ASP .Net Datagrid Control' started by Craig, Oct 9, 2004.

  1. Craig

    Craig Guest

    How do I programmatically change the text property of a label in an
    ItemTemplate in a datagrid?
    Specifically the Text property. I want to change the databinding to another
    column at runtime.

    <asp:TemplateColumn HeaderText="Rate1">
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# fRate(DataBinder.Eval(Container,
    "DataItem.RateAgentID")) %>' ID="Label3" NAME="Label3">
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>

    Thanks
    Craig, Oct 9, 2004
    #1
    1. Advertising

  2. Craig

    Karl Guest

    Hook into the ItemDataBound event of the datagrid, then:

    Label lbl = (Label)e.Item.FindControl("Label3")
    if (lbl != null){
    lbl.Text = ((DataRowView)e.Item.DataItem)["OtherColumn"]
    }

    DataItem actually returns an object which will be a DataRowView when binding
    to a dataset/datatable/datarow...looks like you are binding to a custom
    collection in which case it'll return your collection item type:

    lbl.Text = ((YourClassEntity)e.Item.DataItem).YourProperty

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Craig" <> wrote in message
    news:eUJz$...
    > How do I programmatically change the text property of a label in an
    > ItemTemplate in a datagrid?
    > Specifically the Text property. I want to change the databinding to

    another
    > column at runtime.
    >
    > <asp:TemplateColumn HeaderText="Rate1">
    > <ItemTemplate>
    > <asp:Label runat="server" Text='<%# fRate(DataBinder.Eval(Container,
    > "DataItem.RateAgentID")) %>' ID="Label3" NAME="Label3">
    > </asp:Label>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    > Thanks
    >
    >
    Karl, Oct 9, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    5,305
    Joe Fallon
    Sep 24, 2004
  2. Craig
    Replies:
    1
    Views:
    3,514
  3. Craig
    Replies:
    1
    Views:
    168
  4. RenatoGuarilha
    Replies:
    1
    Views:
    115
    RenatoGuarilha
    Jan 19, 2006
  5. Knut Krueger
    Replies:
    8
    Views:
    236
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page