how to get the text of label in Datalist?

Discussion in 'ASP .Net Web Controls' started by Arvan, May 28, 2006.

  1. Arvan

    Arvan Guest

    hi,all.

    i placed a datalist named DataList1 and placed a Label named Label1 in item
    template.

    how do i get the text of control Label?

    code:

    <asp:DataList ID="DataList1" RepeatColumns="4" runat="server"
    RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
    <ItemTemplate>
    <asp:Label runat="server" ID="img" Text="test"
    ></asp:HyperLink>

    </ItemTemplate>
    </asp:DataList>
    Arvan, May 28, 2006
    #1
    1. Advertising

  2. Normally when you want to do that you are doing it from within an event
    raised by the DataList. I will assume that is where you are doing this from.
    Here is a line of code that will get the Text property of the current Item:

    CType(e.Item.FindControl("lblMyLabel"), Label).Text

    If you needed to access this data from a method/function other than one of
    the events raised by the DataList, you will need to know the index of the
    item the desired Label is in. In this case, use the following:

    CType(DataList1.Items(index).FindControl("lblMyLabel"), Label).Text

    Please remember that when accessing data this way you must do it before
    calling the databind() method in the case of databound properties (it
    doesn't look like your ItemTemplate has any databound properties, but I
    assume it will), otherwise it will return a value of "". Good Luck!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Arvan" <> wrote in message
    news:...
    > hi,all.
    >
    > i placed a datalist named DataList1 and placed a Label named Label1 in
    > item template.
    >
    > how do i get the text of control Label?
    >
    > code:
    >
    > <asp:DataList ID="DataList1" RepeatColumns="4" runat="server"
    > RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
    > <ItemTemplate>
    > <asp:Label runat="server" ID="img" Text="test"
    > ></asp:HyperLink>

    > </ItemTemplate>
    > </asp:DataList>
    >
    Nathan Sokalski, May 28, 2006
    #2
    1. Advertising

  3. Arvan

    Arvan Guest

    Thank you very much. :)
    "Nathan Sokalski" <> дÈëÏûÏ¢ÐÂÎÅ:%2365F%...
    > Normally when you want to do that you are doing it from within an event
    > raised by the DataList. I will assume that is where you are doing this
    > from. Here is a line of code that will get the Text property of the
    > current Item:
    >
    > CType(e.Item.FindControl("lblMyLabel"), Label).Text
    >
    > If you needed to access this data from a method/function other than one of
    > the events raised by the DataList, you will need to know the index of the
    > item the desired Label is in. In this case, use the following:
    >
    > CType(DataList1.Items(index).FindControl("lblMyLabel"), Label).Text
    >
    > Please remember that when accessing data this way you must do it before
    > calling the databind() method in the case of databound properties (it
    > doesn't look like your ItemTemplate has any databound properties, but I
    > assume it will), otherwise it will return a value of "". Good Luck!
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Arvan" <> wrote in message
    > news:...
    >> hi,all.
    >>
    >> i placed a datalist named DataList1 and placed a Label named Label1 in
    >> item template.
    >>
    >> how do i get the text of control Label?
    >>
    >> code:
    >>
    >> <asp:DataList ID="DataList1" RepeatColumns="4" runat="server"
    >> RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
    >> <ItemTemplate>
    >> <asp:Label runat="server" ID="img" Text="test"
    >> ></asp:HyperLink>

    >> </ItemTemplate>
    >> </asp:DataList>
    >>

    >
    >
    Arvan, May 28, 2006
    #3
    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. Onur Bozkurt

    Getting text of a label in a datalist.

    Onur Bozkurt, Aug 12, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    326
    Onur Bozkurt
    Aug 13, 2003
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,833
  3. Praveen
    Replies:
    1
    Views:
    923
    Rob Williscroft
    Nov 5, 2006
  4. CSharpner
    Replies:
    0
    Views:
    975
    CSharpner
    Apr 9, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    237
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page