Morgan
Morgan

Reputation: 71

c# .net change label text

Hello for I trying to use this code but for some reason it doesn't work. Really need help with this. The problem is that the label doesn't change name from "label" when I enter the site.

<asp:Label ID="Label1" runat="server" Text="label"></asp:Label>


<% 
    Label1.Text = "test";
    if (Request.QueryString["ID"] != null)
    {

        string test = Request.QueryString["ID"];
        Label1.Text = "Du har nu lånat filmen:" + test;
    }

     %>

Upvotes: 7

Views: 175538

Answers (6)

kirchhoff
kirchhoff

Reputation: 11

  Label label1 = new System.Windows.Forms.Label
//label1.Text = "test";
    if (Request.QueryString["ID"] != null)
    {

        string test = Request.QueryString["ID"];
        label1.Text = "Du har nu lånat filmen:" + test;
    }

   else
    {

        string test = Request.QueryString["ID"];
        label1.Text = "test";
    }

This should make it

Upvotes: 1

Nonno
Nonno

Reputation: 193

When I had this problem I could see only a part of my text and this is the solution for that:

Be sure to set the AutoSize property to true.

output.AutoSize = true;

Upvotes: 0

Hovestar
Hovestar

Reputation: 1627

Old question, but I had this issue as well, so after assigning the Text property, calling Refresh() will update the text.

Label1.Text = "Du har nu lånat filmen:" + test;
Refresh();

Upvotes: 4

piranha
piranha

Reputation: 141

If I understand correctly you may be experiencing the problem because in order to be able to set the labels "text" property you actually have to use the "content" property.

so instead of:

  Label output = null;
        output = Label1;
        output.Text = "hello";

try:

Label output = null;
            output = Label1;
            output.Content = "hello";

Upvotes: 1

ok.baby
ok.baby

Reputation: 91

you should convert test type >>>> test.tostring();

change the last line to this :

Label1.Text = "Du har nu lånat filmen:" + test.tostring();

Upvotes: 9

Dalbir Singh
Dalbir Singh

Reputation: 2638

Have you tried running the code in the Page_Load() method?

protected void Page_Load(object sender, EventArgs e) 
{

         Label1.Text = "test";
        if (Request.QueryString["ID"] != null)
        {

            string test = Request.QueryString["ID"];
            Label1.Text = "Du har nu lånat filmen:" + test;
        }
}

Upvotes: 3

Related Questions