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
Related
I have page where switch toggle is used and when toggle is switched to on and off update value in database.
<div class="card rounded" style="margin-bottom: 20px">
<asp:Image ID="coverpic" runat="server" CssClass="card-img" Height="500" AlternateText="Cover Picture" />
<div class="card-img-overlay card-inverse social-profile d-flex " style="top: auto">
<div class="align-self-center rounded" style="margin: auto">
<asp:Image ID="profilepic" runat="server" CssClass="img-circle" Height="100" Width="100" AlternateText="Profile Picture" />
<h4 class="card-title">
<asp:Label ID="fullname" runat="server"></asp:Label></h4>
<h6 class="card-subtitle">
<asp:Label ID="unametop" Font-Size="Large" runat="server"></asp:Label></h6>
<div class="switch">
<label>
User
<input id="userrole" type="checkbox" runat="server"><span class="lever switch-col-red"></span>Admin</label>
</div>
</div>
</div>
</div>
As you can see input checkbox with switch class is used, when i turn switch on i want to update user role in database to admin and when switch is turned off i want to update user role to normal user. Please help if you can.
By design the input control doesn't have a click event, so in my opinion the most elegant way to do it is with jQuery, if you're using it. See this answer.
Alternatively you could do something like this:
<script>
function triggerpostback(obj) {
//check if textbox is readyonly
if (obj.readOnly) {
__doPostBack('Button', "");
}
}
</script>
<asp:Button ID="Button" runat="server" Text="Button" OnClick="Button_Click" style="display:none" />
<input id="userrole" type="checkbox" runat="Server" onclick="triggerpostback(this)" />
Code-behind:
protected void Button_Click(object sender, EventArgs e)
{
// Add the code to update user role in database
}
So, I need to get a string from a textbox to use in a backend program, but I'm unable to get it when im one the site, however it works if i set Text="Test1" prior, even though i change the text in the textbox, "Test1" always gets sent to to my function and works! (Talking about Box3)
Code to the text boxes below
<div class="row">
<form action="#" method="post" class="contact-form">
<div class="col-md-4 col-sm-6">
<asp:TextBox ID="Box3" Text="Test1" runat="server" placeholder="Gamepin..."></asp:TextBox>
</div>
<div class="col-md-4 col-sm-6">
<asp:TextBox ID="Box2" runat="server" placeholder="Name ..."></asp:TextBox>
</div>
<div class="col-md-4 col-sm-12">
<asp:TextBox ID="Box1" runat="server" placeholder="Amount ..."></asp:TextBox>
</div>
<div >
<asp:CheckBox ID="CheckBoxREPLY" runat="server" style="vertical-align: central" Text="YesNo" TextAlign="Left" />
</div>
<div class="col-md-12 col-sm-12">
<input type="button" runat="server" onserverclick="Funcactivate" class="button big green" value="Launch!"/>
</div>
</form>
</div>
And my code to get textbox.text
protected void Funcactivate(object sender, EventArgs e)
{
//Get textbox(s)
string Game = Box1.Text;
string Namepref = Box2.Text;
string Amount = Box3.Text;
}
Changing your button could work.
<asp:Button ID="btnLaunch" runat="server" Text="Launch!" class="button big green" OnClick="Funcactivate" CausesValidation="False"/>
If you are using web forms, you should be able to capture the text box entry in the "if(!IsPostBack){...}" event and set it to a variable and use it in your "Funcactivate" method.
Hello Sleepwalker, here is a code sample that works for me, I am not using the isPostBack. I am also using an asp:button Instead of using an HTML input control.
<div>
<asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
</div>
<div>
<asp:TextBox ID="txtBox2" runat="server"></asp:TextBox>
</div>
<div>
<asp:TextBox ID="txtBox3" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click" />
</div>
protected void btnSubmit_Click(object sender, EventArgs e)
{
string game = this.txtBox1.Text.ToString();
string Namepref = this.txtBox2.Text.ToString();
string Amount = this.txtBox3.Text.ToString(); //box3;
}
I fixed it by moving my form to only get the < asp:X, because i discovered that if the < asp:x was nested too deep within a ton of divs, it didnt work properly.
It may have been due to something else, but it worked for me.
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 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?
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