asp:Label - how can I get the Text property?

Discussion in 'ASP .Net' started by Steve Hershoff, Nov 3, 2006.

  1. I'm using VS 2003, and need to extract the text from an asp label in my
    code-behind file. The label is part of a DataList's ItemTemplate.

    I've noticed that in my ascx file (we're using User Controls here) I can't
    set the Text property in the opening tag when writing out the
    <asp:Label....</asp:Label> tags. I have to put my text between the opening
    and closing tags, like you would with a regular HTML tag.

    So this might be the cause of my trouble. What happens in my code-behind
    file is I'm trapping the DataBound event on the label's "parent" datalist,
    and doing the following:

    //from DL_List_DataBound(object sender, DataListItemEventArgs e)

    /.....

    Label myLabel = (Label) e.Item.FindControl("labelFromASCX_page");

    myLabel.Visible = true;
    String myString = myLabel.Text;

    //........


    My problem is, the value of myLabel.Text is always equal to "" even though I
    set it in the ascx file. How can I extract the label's text in my
    code-behind?
    Steve Hershoff, Nov 3, 2006
    #1
    1. Advertising

  2. Hi,

    Steve Hershoff wrote:
    > I'm using VS 2003, and need to extract the text from an asp label in my
    > code-behind file. The label is part of a DataList's ItemTemplate.
    >
    > I've noticed that in my ascx file (we're using User Controls here) I can't
    > set the Text property in the opening tag when writing out the
    > <asp:Label....</asp:Label> tags. I have to put my text between the opening
    > and closing tags, like you would with a regular HTML tag.
    >
    > So this might be the cause of my trouble. What happens in my code-behind
    > file is I'm trapping the DataBound event on the label's "parent" datalist,
    > and doing the following:
    >
    > //from DL_List_DataBound(object sender, DataListItemEventArgs e)
    >
    > /.....
    >
    > Label myLabel = (Label) e.Item.FindControl("labelFromASCX_page");
    >
    > myLabel.Visible = true;
    > String myString = myLabel.Text;
    >
    > //........
    >
    >
    > My problem is, the value of myLabel.Text is always equal to "" even though I
    > set it in the ascx file. How can I extract the label's text in my
    > code-behind?


    If you don't set the Text property of the label, but you write the
    content between the closing and opening tag of the Label, I think it
    doesn't set the text property, but rather it adds a literal control to
    the Label. That would explain why the Text property is always empty.

    Why can't you just set the Text property in the asp:Label tag?

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Nov 6, 2006
    #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. Replies:
    1
    Views:
    9,929
    Brock Allen
    May 24, 2005
  2. Praveen
    Replies:
    1
    Views:
    960
    Rob Williscroft
    Nov 5, 2006
  3. CSharpner
    Replies:
    0
    Views:
    1,013
    CSharpner
    Apr 9, 2007
  4. Steve Hershoff

    asp:Label - how can I get the Text property?

    Steve Hershoff, Nov 3, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    1,200
    Laurent Bugnion
    Nov 6, 2006
  5. Knut Krueger
    Replies:
    8
    Views:
    263
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page