I created a new project in VS2010. It automaticly creates masterpages and other stuff.
In the login page i create a connection with my local database. To test my login-routine i want to change te text of a label.
I wanna do this with behind code (C#).
But i continue get errors that the refered label doesn't exist: Error 1 The name 'Labeltest1' does not exist in the current context l:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 46 13 l:\...\WebSite1\
I tryed different solutions, but nothing helps.
Can someone help me with this please.
This is my *.aspx and *.aspx.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
}
public void LoginCode(object sender, EventArgs e)
{
string Naam = "h.v.neerven@home.nl";
string Wachtwoord = "hallo";
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connectionwebbankbeheer"].ConnectionString);
SqlCommand cmd = new SqlCommand("SelecteerLogins", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Login", Naam);
cmd.Parameters.AddWithValue("@Wachtwoord", Wachtwoord);
//prm1 pakt de return value van de stored procedure.
SqlParameter prm1 = cmd.Parameters.AddWithValue("@OK", SqlDbType.Int);
prm1.Direction = ParameterDirection.ReturnValue;
//Execute the command.
connection.Open();
cmd.ExecuteNonQuery();
//Accept the return value:
//1 if valid
//0 otherwise
Byte count = Convert.ToByte(cmd.Parameters["@OK"].Value);
if (count == 1)
{
Labeltest1.text = "excellent!";
}
else
{
}
connection.Close();
}
}
<%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Login.aspx.cs" Inherits="Account_Login" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Log In
</h2>
<p>
Please enter your username and password.
<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> if you don't have an account.
</p>
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
<LayoutTemplate>
<span class="failureNotification">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="LoginUserValidationGroup"/>
<div class="accountInfo">
<fieldset class="login">
<legend>Account Information</legend>
<p>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:CheckBox ID="RememberMe" runat="server"/>
<asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
</p>
<p>
<asp:Label ID="Labeltest1" runat="server">.........</asp:Label>
</p>
</fieldset>
<p class="submitButton">
<asp:Button ID="LoginButton" runat="server" Oncommand="LoginCode" CommandName="LoginCode" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
</p>
</div>
</LayoutTemplate>
</asp:Login>
</asp:Content>
You have it inside of your login control. You can place it outside of your login control or use a .FindControl method on your <asp:Login control and you will be able to reference your control.
I'm afraid that didn't do the job. it now gives me the error:
Error 1 The name 'Login1' does not exist in the current context L:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 45 32 L:\...\WebSite1\
When i change Login1 to LoginCode it says:
Error 1 'Account_Login.LoginCode(object, System.EventArgs)' is a 'method', which is not valid in the given context L:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 45 32 L:\...\WebSite1\
I'm afraid that didn't do the job. it now gives me the error:
Error 1 The name 'Login1' does not exist in the current context L:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 45 32 L:\...\WebSite1\
When i change Login1 to LoginCode it says:
Error 1 'Account_Login.LoginCode(object, System.EventArgs)' is a 'method', which is not valid in the given context L:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 45 32 L:\...\WebSite1\
Any help? Thnx
Hmm... please read the code carefully. By just copy/paste of course it might not work in your environment ;).
Emm... my Login control id was Login1 while your Login control id is
LoginUser - so just change that... i.e.
Your label is inside of the Login control's template. You must reference the control by using findcontrol of the parent control. Once you reference the parent control you can then find all child elements of the parent. Either you reference using findcontrol
or you move the control outside of the parent. Once you do this, your parent control is the form. You can then do this.Label or Me.Label in order to find your label. Until you do so, you must do LoginControl.FindControl("labelid") in order to instantiate the
object.
Check the call to LoginCode If it's missing somekey word. I am not understanding ur method is a event or plain method if it's event from where is it firing also I guess It should be syntaxed as:
LoginUser_Click(...)...blhablah
markup:
to me the code sample is missing the event OnClick. Am I correct? not then pls excuse for any oversight mistake.
Hanzie
Member
8 Points
31 Posts
Change label text with C# behind code
Dec 29, 2010 10:11 PM|LINK
Hello,
I created a new project in VS2010. It automaticly creates masterpages and other stuff.
In the login page i create a connection with my local database. To test my login-routine i want to change te text of a label.
I wanna do this with behind code (C#).
But i continue get errors that the refered label doesn't exist: Error 1 The name 'Labeltest1' does not exist in the current context l:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 46 13 l:\...\WebSite1\
I tryed different solutions, but nothing helps.
Can someone help me with this please.
This is my *.aspx and *.aspx.cs file:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; public partial class Account_Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); } public void LoginCode(object sender, EventArgs e) { string Naam = "h.v.neerven@home.nl"; string Wachtwoord = "hallo"; SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connectionwebbankbeheer"].ConnectionString); SqlCommand cmd = new SqlCommand("SelecteerLogins", connection); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Login", Naam); cmd.Parameters.AddWithValue("@Wachtwoord", Wachtwoord); //prm1 pakt de return value van de stored procedure. SqlParameter prm1 = cmd.Parameters.AddWithValue("@OK", SqlDbType.Int); prm1.Direction = ParameterDirection.ReturnValue; //Execute the command. connection.Open(); cmd.ExecuteNonQuery(); //Accept the return value: //1 if valid //0 otherwise Byte count = Convert.ToByte(cmd.Parameters["@OK"].Value); if (count == 1) { Labeltest1.text = "excellent!"; } else { } connection.Close(); } }<%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Account_Login" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <h2> Log In </h2> <p> Please enter your username and password. <asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> if you don't have an account. </p> <asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false"> <LayoutTemplate> <span class="failureNotification"> <asp:Literal ID="FailureText" runat="server"></asp:Literal> </span> <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" ValidationGroup="LoginUserValidationGroup"/> <div class="accountInfo"> <fieldset class="login"> <legend>Account Information</legend> <p> <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label> <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> </p> <p> <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> </p> <p> <asp:CheckBox ID="RememberMe" runat="server"/> <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label> </p> <p> <asp:Label ID="Labeltest1" runat="server">.........</asp:Label> </p> </fieldset> <p class="submitButton"> <asp:Button ID="LoginButton" runat="server" Oncommand="LoginCode" CommandName="LoginCode" Text="Log In" ValidationGroup="LoginUserValidationGroup"/> </p> </div> </LayoutTemplate> </asp:Login> </asp:Content>label behind code c#
Srini Oleti
Member
32 Points
7 Posts
Re: Change label text with C# behind code
Dec 29, 2010 11:22 PM|LINK
Hi Hanzie,
I don't see Labeltest1 in your aspx file.
In your code behind file Labeltest1.text = "excellent!"; it should be Labeltest1.Text = "excellent!" (Upper case T);
According to your posted code there is no Labeltest1 control. Check for that.
Thank you,
Srini Oleti.
Xequence
Contributor
4313 Points
1528 Posts
Re: Change label text with C# behind code
Dec 29, 2010 11:29 PM|LINK
You have it inside of your login control. You can place it outside of your login control or use a .FindControl method on your <asp:Login control and you will be able to reference your control.
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Credentials
hajan
Star
10655 Points
1782 Posts
Re: Change label text with C# behind code
Dec 29, 2010 11:42 PM|LINK
Just add this:
Label lbl = (Label)Login1.FindControl("Labeltest1"); if (lbl != null) { lbl.Text = "Excellent!"; }Simple as that ;).
Hajan
Dont forget to Mark as Answer the answer that solved your problem!
My ASP.NET Weblog
Hanzie
Member
8 Points
31 Posts
Re: Change label text with C# behind code
Dec 30, 2010 08:01 AM|LINK
Hello Hajan,
I'm afraid that didn't do the job. it now gives me the error:
Error 1 The name 'Login1' does not exist in the current context L:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 45 32 L:\...\WebSite1\
When i change Login1 to LoginCode it says:
Error 1 'Account_Login.LoginCode(object, System.EventArgs)' is a 'method', which is not valid in the given context L:\Documenten\Visual Studio 2010\WebSites\WebSite1\Account\Login.aspx.cs 45 32 L:\...\WebSite1\
Any help? Thnx
Hanzie
Member
8 Points
31 Posts
Re: Change label text with C# behind code
Dec 30, 2010 08:03 AM|LINK
Hello Xequence,
In my opinion it is not in a control. The code for changing Labeltest1 is in a method.
Am i right?!
THNX
Hanzie
Member
8 Points
31 Posts
Re: Change label text with C# behind code
Dec 30, 2010 08:05 AM|LINK
Hello Srini Oleti,
In the *.aspx there is a Labeltest1 definition. See somewhere downstairs in the code !
THNX
hajan
Star
10655 Points
1782 Posts
Re: Change label text with C# behind code
Dec 30, 2010 11:10 AM|LINK
Hmm... please read the code carefully. By just copy/paste of course it might not work in your environment ;).
Emm... my Login control id was Login1 while your Login control id is LoginUser - so just change that... i.e.
Label lbl = (Label)LoginUser.FindControl("Labeltest1"); if (lbl != null) { lbl.Text = "Excellent!"; }I hope you see the difference and hope it will work now.
Try it and reply back ;)
Cheers...
Hajan
Dont forget to Mark as Answer the answer that solved your problem!
My ASP.NET Weblog
Xequence
Contributor
4313 Points
1528 Posts
Re: Change label text with C# behind code
Dec 30, 2010 01:45 PM|LINK
Your label is inside of the Login control's template. You must reference the control by using findcontrol of the parent control. Once you reference the parent control you can then find all child elements of the parent. Either you reference using findcontrol or you move the control outside of the parent. Once you do this, your parent control is the form. You can then do this.Label or Me.Label in order to find your label. Until you do so, you must do LoginControl.FindControl("labelid") in order to instantiate the object.
Credentials
aspnet2sams
Participant
1772 Points
551 Posts
Re: Change label text with C# behind code
Dec 30, 2010 02:27 PM|LINK
Check the call to LoginCode If it's missing somekey word. I am not understanding ur method is a event or plain method if it's event from where is it firing also I guess It should be syntaxed as:
LoginUser_Click(...)...blhablah
markup:
to me the code sample is missing the event OnClick. Am I correct? not then pls excuse for any oversight mistake.