I keep getting the error "The name "User Name" does not exist in the current context. As you can see "UserName" is clearly defined on my code behind page and I am referencing my page. So why does it say that it does not exist?
Here is my C# -
public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void btnLogOn_Click(object sender, EventArgs e)
{
Session["SubName"] = UserName.Text;
}
}
Here is my HTML -
<p>
Please enter your username and password.
</p>
<form action="/Account/LogOn" method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
<label for="UserName">User name</label>
</div>
<div class="editor-field">
<input id="UserName" name="UserName" type="text" value="" />
</div>
<div class="editor-label">
<label for="Password">Password</label>
</div>
<div class="editor-field">
<input id="Password" name="Password" type="password" />
</div>
<p>
<input id="btnLogOn" type="submit" value="Log On" />
</p>
</fieldset>
</div>
</form>
</div>
Your input box needs to have runat=server if you want to access it from code behind.
<input id="UserName" name="UserName" type="text" value="" runat=server />
Otherwise you will receive an error that UserName does not exist, as you have already mentioned.
You should have 'runat="server"' in UserName
<input id="UserName" name="UserName" type="text" value="" runat="server" />
"UserName" is clearly defined on my code behind
No, it's not - you have a client side control called UserName but nothing in the code-behind. You can either make it a server control by adding 'runat="server"' to the form and control, or reference the form variable in the code-behind:
protected void btnLogOn_Click(object sender, EventArgs e)
{
Session["SubName"] = Request["UserName"]; // pull value from form variable
}
UserName object corresponding to the control needs to be present in the designer.cs file of the page you are working on.
This will be auto generated on page save, but only if you create your control as a server side control.
So marking the input control with runat="server" should do the trick.
Add runat="server" to the label control?
Related
Currently I’m developing MAUI application with blazor template.I use the navigation manager to navigate to another page in order to move to the next page when a user successfully enters their credentials.
Navigation manager moves me to the next page. But in a millisecond, it will be redirected to the index page.
Here is my index code below:
#page "/"
#inject NavigationManager Navigation;
<div class="center_div" >
<form class="center_content">
<center>
<h3 class="login_title">Sign In</h3>
<p class="login_text">Login with your Credentials</p>
<div class="field_cont">
<input type="text" class="field_input" placeholder=" " />
<label class="field_label">Username</label>
</div>
<div class="field_cont">
<input type="password" class="field_input" placeholder=" " />
<label class="field_label">Password</label>
</div>
<button #onclick="()=>NavigateToCompany()" class="form_btn">Sign In</button>
</center>
</form>
</div>
#code{
private string UserName = "";
private string Password = "";
private void NavigateToCompany()
{
Navigation.NavigateTo("/company-select");
}
}
Here is my second page code below:
#page "/company-select"
<h3>CompanySelect</h3>
#code {
}
How can I stay on second page rather than be redirected to the index page?
I use .NET 7 and Visual studio 2022
You need to put the Button element outside the form element like below:
#page "/"
#inject NavigationManager Navigation;
<div class="center_div">
<form class="center_content">
<center>
<h3 class="login_title">Sign In</h3>
<p class="login_text">Login with your Credentials</p>
<div class="field_cont">
<input type="text" class="field_input" placeholder=" " />
<label class="field_label">Username</label>
</div>
<div class="field_cont">
<input type="password" class="field_input" placeholder=" " />
<label class="field_label">Password</label>
</div>
</center>
</form>
<button #onclick="()=>NavigateToCompany()" class="form_btn">Sign In</button>
</div>
#code{
private string UserName = "";
private string Password = "";
private void NavigateToCompany()
{
Navigation.NavigateTo("/company-select");
}
}
The reason why you need place the button outside of the form is that when putting a button element inside a form element, the attribute of the button element defaults to submit leading to that it will be redirected to the index.
I'm trying to add a new seller into database but all the values i get from the input text are null
this is the form
<div class="mb-3">
<label for="SNameTb" class="form-label">Seller FirstName</label>
<input type="text" class="form-control" id="Snametb" runat="server">
</div>
And this how i'm trying to get the values
string Sname = Snametb.Value;
You missing the closing "/> in that input box.
So, this:
<div class="mb-3">
<label for="SNameTb" class="form-label">Seller FirstName</label>
<input type="text" class="form-control" id="Snametb" runat="server" value="" />
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
and button click code:
protected void Button1_Click(object sender, EventArgs e)
{
Debug.Print("Text box value = " + Snametb.Value);
}
So, add the "/>" to your markup (maybe a cut+paste issue).
And not required, but good idea to add the value="" attribute also.
add runat= 'server' to the text input
I am trying to auto-fill a form on a webpage using .NET's web browser int he C# language. I need to fire the submit button but it doesn't have a name or ID, all it has is a type and value.
The type is equal to "submit" and the value is equal to "Sign In"
Heres how I did it for Sign Up:
webBrowser1.Document.GetElementById("email").InnerText = email;
webBrowser1.Document.GetElementById("password").InnerText = password;
webBrowser1.Document.GetElementsByTagName("input")["register"].InvokeMember("click");
But this time, the submit button doesn't have a name, can someone guide me in the right direction to getting an element by its type or even better, value?
HTML:
<form accept-charset="utf-8" method="post">
<div class="field">
<label for="email">Email or Username</label> <span class="required">*</span> <input class="email" id="email" name="email" size="32" type="text">
</div>
<div class="field">
<label for="password">Password</label> <span class="required">*</span> <input id="password" name="password" size="16" type="password">
<p class="tip">I don't know my password</p>
</div>
<div class="field">
<input id="persistent_hidden" name="persistent" type="hidden" value="f"> <input checked="checked" id="persistent" name="persistent" type="checkbox" value="t"> <label for="persistent">Keep me logged in</label>
</div>
<div class="submit">
<input id="redirect_path" name="redirect_path" type="hidden"> <input type="submit" value="Sign In"> or <a class="cancel" href="/">Cancel</a>
</div>
</form>
try :
foreach(HtmlElement elem in webBrowser.Document.getElementByTagName("input")) {
if (elem.GetAttribute("value") == "Sign In") {
elem.InvokeMember("Click");
}
}
Help!
I have a login and and a signup html form, each toggle on clicking respective links on the form itself and there is no postback or page refresh event.
the problem is asp.net does not allow me to have two runat=server forms.
i can access input fields of the forms.
i have added asp:Button in place of input type="submit" in the forms in order to acces onclick methods, but then again asp.net does not allow me to add asp button (server side control) when i remove runat="server" from any of the forms which is having this asp button!
The question:
How can i access submit button of the forms so that i can perform necessary code behind operations for signup and login
Is there a way to achieve my goal? (may be some way to hide one of the forms in starting and show it when i click toggle link )
Here's the login register form's
Code:
<div>
<header>
</header>
<section>
<div id="container_demo" >
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<form id="form1" action="#" runat="server" >
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" > Your email </label>
<input id="username" name="username" runat="server" required="required" type="text" placeholder="myusername or mymail#mail.com"/>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<input id="password" name="password" runat="server" required="required" type="password" placeholder="eg. X8df!90EO" />
</p>
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
</p>
<p class="login button">
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
<!--<input type="submit" value="Login" />-->
</p>
<p class="change_link">
Not a member yet ?
Join us //toggle link
</p>
</form>
</div>
<div id="register" class="animate form">
<form id="form2" action="#" runat="server" >
<h1> Sign up </h1>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<input id="usernamesignup" name="usernamesignup" runat="server" required="required" type="text" placeholder="mysuperusername690" />
</p>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<input id="emailsignup" name="emailsignup" runat="server" required="required" type="email" placeholder="mysupermail#mail.com"/>
</p>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<input id="passwordsignup" name="passwordsignup" runat="server" required="required" type="password" placeholder="eg. X8df!90EO"/>
</p>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="mob1" class="uname" data-icon="u">Your mob no.</label>
<input id="mob" name="mob" runat="server" required="required" type="text" placeholder="9450.." />
</p>
<p class="signin button">
<asp:Button Text="Submit" runat="server" OnClick="Submitr" />
<!--<input type="submit" value="Sign up"/> -->
</p>
<p class="change_link">
Already a member ?
Go and log in //toggle link
</p>
</form>
</div>
</div>
</div>
</section>
</div>
Ok finally i got it after applying my brains!
this is what i did:
applied asp buttons on both server side forms
on signup form:
<asp:Button Text="tologin" runat="server" OnClick="changetologin" ForeColor="#1DA2C1" BackColor="#F7F8F1" />
on login form:
<asp:Button Text="Join us" runat="server" OnClick="changetosignup" ForeColor="#1DA2C1" BackColor="#F7F8F1" />
on page load:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["form2"] == null && Session["form1"] == null) //show login hide signup
{
form1.Visible = true;
form2.Visible = false;
}
if (Session["form2"] != null && Session["form1"]==null ) //show signup hide login
{
form1.Visible = false;
form2.Visible = true;
Session["form2"] = null;
}
if (Session["form1"] != null && Session["form2"] == null) //show login hide signup
{
form1.Visible = true;
form2.Visible = false;
Session["form1"] = null;
}
}
on signup form on click of toggle button:
protected void changetologin(object sender, EventArgs e)
{
Session["form1"] = "clicked";
Response.Redirect("#tologin");
}
on login form on click of toggle button:
protected void changetosignup(object sender, EventArgs e)
{
Session["form2"] = "clicked";
Response.Redirect("#toregister");
}
In short:
combo of form visible property and session variable did the trick !!
I'm trying to follow this example on how to validate credentials. However, it uses asp: controls for the login form.
If I were to use html controls instead so CSS styles can be applied, eg
<div id="login">
<form action="#" runat="server">
<fieldset>
<div class="frame">
<h4>Login</h4>
<small>Sign in to your account.</small>
<div class="clear"></div>
<input type="text" value="Username" class="input-text autoclear" />
<input type="password" value="Password" class="input-text autoclear"/>
</div>
<div class="separator"></div>
<div>
<input type="submit" value="Sign in" class="input-submit float-right" runat="server" onserverclick="LoginButton_Click"/>
Forgot your password?
</div>
</fieldset>
</form>
</div>
How do I access the Username & Password in code behind similar to?
protected void LoginButton_Click(object sender, EventArgs e)
{
// Validate the user against the Membership framework user store
if (Membership.ValidateUser(UserName.Text, Password.Text))
{
// Log the user into the site
FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked);
}
// If we reach here, the user's credentials were invalid
InvalidCredentialsMessage.Visible = true;
}
What is the correct syntax instead of UserName.Text, Password.Text?
Add id and runat server attributes to the input tag (see below)
<input type="text" value="Username" class="input-text autoclear" id="Username" runat="server"/>
<input type="password" value="Password" class="input-text autoclear" id="Password" runat="server"/>
You also need to change Text to Value in your code:
protected void LoginButton_Click(object sender, EventArgs e)
{
// Validate the user against the Membership framework user store
if (Membership.ValidateUser(Username.Value, Password.Value))
{
// Log the user into the site
FormsAuthentication.RedirectFromLoginPage(UserName.Value, RememberMe.Checked);
}
// If we reach here, the user's credentials were invalid
InvalidCredentialsMessage.Visible = true;
}
You can also add a html checkbox for RememberMe
<input id="RememberMe" type="checkbox" runat="server" value ="RememberMe"/>
Now you can check the checked states by calling RememberMe.Checked
Add runat="server" and id="your_id" and you should have access to them. For example:
<input type="text" value="Username" class="input-text autoclear"
runat="server" id="UserName" />
<input type="password" value="Password" class="input-text autoclear"
runat="server" id="Password"/>
Then you can access the values like this:
Membership.ValidateUser(UserName.Value, Password.Value)
You can access them from code behind by adding runat="server" on the html elements.
http://www.w3schools.com/aspnet/aspnet_refhtmlcontrols.asp
The below link has an example of how you can do this
http://msdn.microsoft.com/en-us/library/aa478973.aspx