3
votes

I have just started creating a new Web application using ASP.NET WebForms (VS 2012). In the project I have a Login.aspx page with a TextBox (created automatically by VS):

<asp:TextBox ID="Username" runat="server"></asp:TextBox>

Now I need to access the value of that text box, and I am using the following:

String username = Html.Encode(Username.Text);

I get an error, stating that Username does not exist. In Login.Designer.cs the TextBox is not declared.

On the designer, when I select the text box it is identified in the Properties dropdown as:

Unnamed1.LayoutTemplate.Username

This is my first project in VS2012; I have not had this issue in VS2008 or earlier. Is there anything different here?

5
I think you should remove and drag-drop the textbox again or declare it in the designer.csAdnanQ
Try restarting VS, that normally works for me when the designer file fails to build correctlyfreefaller
Is Username TextBox placed in asp:Login control?Yuriy Rozhovetskiy
@AdnanQ I tried with drag and drop, it did not work. Nor declaring it. I tried to restart VS many times as freefaller suggested but nothing changes.user10901
@YuriyRozhovetskiy yes it is placed under asp.Loginuser10901

5 Answers

1
votes

You can't access children controls of templated control till template property not marked with TemplateInstance(TemplateInstance.Single) attribute. And this is how ASP.NET templated controls work.

In this particular case you can use UserName property of Login control. Behind the scene, Login control will search own LayoutTemplate for instance of IEditableTextControl control with UserName ID.

0
votes

Try this,

string username=Username.Text;
0
votes

Sometimes a Build -> Rebuild will fix errors like this.

0
votes

Make it this

String username = Server.Html.Encode(Username.Text);
0
votes

First convert your asp:login control to a template using the designer. This works for me where login has a <div runat="server"> added in template:

HtmlGenericControl ChooseDiv = Login1.FindControl("ChooseFireDepartment") as HtmlGenericControl;
ChooseDiv.Visible = false;

This works with a dropdown list added to the same template:

var ddlList = Login1.FindControl("myDdlList") as DropDownList;
...
ddlList.DataBind();