I am trying to make a user register for my project and I want to take user's password with input(password). I used textboxes for name and surname and I can save them to database but I can't get user's input which has entered in input(password). Here is my code which I am using to save users in database.
TermProjectEntities entity = new TermProjectEntities();
protected void Button1_Click(object sender, EventArgs e)
{
Customer cust = new Customer
{
Name = txtName.Text,
Surname = txtSurname.Text,
Email = txtEmail.Text,
};
}
I can take textboxes variables with "textboxe's id.Text" but it's not working for input(password). Thanks for the help and sorry if it's an easy question and took your time.
Here is the HTML:
<form id="form1" runat="server">
<div>
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
Surname:
<asp:TextBox ID="txtSurname" runat="server"></asp:TextBox><br />
Password:
<input id="txtPassword" type="password" /><br />
E-Mail:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" /> <br />
</div>
</form>
I have copied your HTML from your comment onto the question to make it easier for people to answer. But while I was there I saw the problem is your password is not set to run on the server (therefore will not be sent back when you click the button).
So you currently have this:
<input id="txtPassword" type="password" /><br />
But it should be this:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br />
So, there are two main points:
Use asp:TextBox and not input to leverage ASP .NET Framework
Ensure every control you want to use contains runat="server"
This is because ASP .NET is Server-Side so you need to get your controls back to the server before they can be accessed and this is what the runat="server" attribute achieves.
You can then use attributes like TextMode="Password" to control how items are shown/act.
Related
I'm new to programming in asp.net, I'm still learning and right now I'm building a login form.
This is my aspx (HTML file):
<div class="login">
<input type="text" placeholder="User" name="user" id="user" runat="server"><br>
<input type="password" placeholder="Password" name="password" id="password" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click">Login</button>
</div>
I need to create a button click event in aspx.cs to store the data in SQL Server, in my registration form I did this:
cmd.Parameters.AddWithValue("user", user.Text);
cmd.Parameters.AddWithValue("password", password.Text);
The difference was that the TAG in the .aspx file for registration was like this:
<td class="auto-style8">
<asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>
</td>
How can I do the same that I did in the registration form without using these asp tags?
You are trying to get rid of ASP.NET tag controls which effectively are syntactic sugar around HTML equivalents. You could re-write you ASP Textbox tag as below:
<input type="text" ID="nome" runat="server" style="width:159px"></input>
The runat="server" attribute gives you the flexibility to access this control at code behind (aspx.cs) and if you do not intend to do that, it can be removed as well.
from code behind access the value of text control like this -
string userName = user.Value;
you can directly access value of controls by using name of that control
In your case
<td class="auto-style8">
<asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>
</td>
you can access this value in .cs file like
cmd.Parameters.AddWithValue("user", nome.Text);
I have a form and I have a validation in JavaScript. How to prevent the submit button go the the server side if the form is not valid?
<button id="LoginButton" onclick="Login.initReuiredValidation();"
onserverclick="LoginButton_Click" runat="server" type="submit"
class="submit btn btn-primary pull-right">
<asp:Literal runat="server" meta:resourcekey="LoginButton" />
<i class="icon-angle-right"></i>
</button>
in jquery you can do as
$(yourform).submit(function(event){
event.preventDefault();
//do somehting
})
This isn't classic asp, it's asp.net webforms and I've edited the tags accordingly
Adding input validation in webforms is quite easy, you can assign a validation control to each form control, eg
Contact Name<br />
<asp:TextBox ID="Contact_name" runat="server" Width="246 pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactNameValidator" runat="server"
ErrorMessage="Please enter your name" ControlToValidate="Contact_name"></asp:RequiredFieldValidator><br />
<br />
Contact Telephone<br />
<asp:TextBox ID="Contact_phone" runat="server" Width="246pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactPhoneValidator" runat="server" ControlToValidate="Contact_phone"
ErrorMessage="Please enter your telephone number"></asp:RequiredFieldValidator>
If you view the output, you will see that the validation is actually done with (client side) JavaScript. Asp.net generates that for you, you don't have to write it yourself.
Further information here
https://msdn.microsoft.com/library/a0z2h4sw%28v=vs.100%29.aspx
In the code behind add...
LoginButton.Attributes.Add("onclick", "return false;");
I'm creating a login system in asp.net and C# programming language. The code behind to handle the user and password is done. But in view layer, I'm troubling to get the values from username textbox and password textbox and passing it to codebehind.
Both textboxes are ID identified and in my few skills of programming, an ID should be enough to access the elements.
This is my aspx login page:
<asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
<LayoutTemplate>
<p class="validation-summary-errors">
<asp:Literal runat="server" ID="FailureText" />
</p>
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
<asp:Label ID="Label1" runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
</li>
<li>
<asp:Label ID="Label2" runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
</li>
<li>
<asp:CheckBox runat="server" ID="RememberMe" />
<asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
</li>
</ol>
<asp:Button ID="Button1" runat="server" CommandName="Login" Text="Log in" OnClick="Button1_Click"/>
</fieldset>
</LayoutTemplate>
</asp:Login>
This I did do get values from UserName and Password Textboxes:
Using the code:
string user = this.UserName.Text;
string pass = this.Password.Text;
Using the code:
Textbox UserName = this.FindControl("UserName");
Deleted the aspx.design.cs and right click on the form and Convert it to application;
In the designer, add the following lines of code:
protected global::System.Web.UI.WebControls.TextBox UserName;
protected global::System.Web.UI.WebControls.TextBox Password;
Nothing worked so far, and when I reach this line:
string user = this.UserName.Text;
It throws me an error:
Object Reference not set an instance of an object.
Can you suggest any solution to my problem?
This is because these controls are parts of a template. They are not directly on the page, they are added there dynamically when Login control is initialized. To access them you need FindControl:
string user = ((TextBox)Login1.FindControl("UserName")).Text;
I'm trying to create a ASP .NET website that masks the password in a registration page as the user types. I found sites that use windows form but it doesn't work for a ASP .NET website.
So if the user types in a password its masked like this
*******
Any website or suggestion on how I can get it to work would be great.
To do it the ASP.NET way:
<asp:TextBox ID="txtBox1" TextMode="Password" runat="server" />
//in aspx page
<asp:TextBox ID="password" runat="server" TextMode="Password" />
//in MVC cshtml
#Html.Password("password", "", new { id = "password", Textmode = "Password" })
Use the password input type.
<input type="password" name="password" />
Here is a simple demo http://jsfiddle.net/cPaEN/
#JohnHartsock: You can also write type="password". It's acceptable in aspx.
<asp:TextBox ID="txtBox" type="password" runat="server"/>
I think this is what you are looking for
<asp:TextBox ID="txbPass" runat="server" TextMode="Password"></asp:TextBox>
Simply select texbox property 'TextMode' and select password...
<asp:TextBox ID="TextBox1" TextMode="Password" runat="server" />
I am working on adding a modal popup to ASP.NET pages. The popup will give the user some textboxes to fill in, with a Cancel and Submit button.
The issue I'm running into is that the textboxes are being created dynamically, as the number of textboxes needed and what they are asking for will change depending on what is clicked on the page. When attempting to retrieve the values that have been entered after clicking Submit on the modal window (which is not tied to the modal window so that it will do a postback), the textboxes are gone by that point and the data cannot be retrieved.
Here's the code for the modal popup:
<div id="divModalContainer">
<div id="PromptContentHeader">
<asp:Label ID="lblHeader1" runat="server">
</asp:Label>
<br />
<asp:Label ID="lblHeader2" runat="server">
</asp:Label>
<asp:Label ID="lblPassFileName" runat="server">
</asp:Label>
</div>
<!--<ul id="ulTabModalPrompt" class="tabnav" runat="server">
</ul>-->
<div id="divModalPrompts" runat="server">
<table id="PromptTable" runat="server">
</table>
</div>
<div id="divModalButtons" style="width:230px;">
<div style="float:left">
<asp:Button ID="btnCancelDocPrompts" runat="server" Text="Cancel" OnClick="btnCancelDocPrompts_Click" />
</div>
<div style="float:right">
<asp:Button ID="btnSubmitDocPrompts" runat="server" Text="Submit" OnClick="btnSubmitDocPrompts_Click" />
</div>
</div>
</div>
</asp:Panel>
<ajaxtoolkit:ModalPopupExtender ID="modalDocPrompt" runat="server"
TargetControlID="btnOpenPromptWindow"
PopupControlID="panelPrompts"
OkControlID="btnHiddenOkButton"
CancelControlID="btnCancelDocPrompts"
BackgroundCssClass="ModalPromptBackground"
DropShadow="true" />
<asp:Button ID="btnOpenPromptWindow" runat="server" Text="Test Modal" Style="display: none;" />
<asp:Button ID="btnHiddenOkButton" runat="server" Text="Test Modal" Style="display: none;" />
Before the modal popup is shown, rows will be added to PromptTable, with a label and textbox in each row.
The btnSubmitDocPrompts_Click method will attempt to go through each row in PromptTable and retrieve the values entered, but once Submit is clicked, there are no rows anymore.
Thanks for the help!
You could try using JavaScript to write the values of the textboxes to a HiddenField (which will get posted back if it exists when the page loads).
You'll have to encode the values and comma-separate them (or similar), then parse the values out server-side.
Edit: Example
OK, I'd personally use jQuery for the JavaScript part, but I'll assume you're doing it 'raw'.
Say your markup (with a few added dynamic textboxes) looks like this:
<input type="hidden" id="hdnFormValues" />
<div id="dynamicForm">
<div id="textBoxArea">
<input type="text" id="newField1" /><br />
<input type="text" id="newField2" /><br />
<input type="text" id="newField3" /><br />
<input type="text" id="newField4" /><br />
</div>
<input type="submit" onclick="saveValues()" value="Save Values" />
</div>
and you have a JavaScript function that looks like this:
function saveValues()
{
theBoxes = document.getElementById('textBoxArea').getElementsByTagName('input');
hdnValues = document.getElementById('hdnFormValues');
hdnValues.value = "";
for(var i = 0; i < theBoxes.length; i++)
{
hdnValues.value += escape(theBoxes[i].value) + '|';
}
}
Then when the submit button gets pressed, the value of the HiddenField will become a pipe-delimited string of encoded values.
For example, if text boxes 1 through 4 had the values:
I'm
encoding
dynamic
values!
then the HiddenField's value would become I%27m|encoding|dynamic|values%21|
Remember, you'll need to output the function above from ASP.NET server side. Look at the ScriptManager documentation for how to do this. The reason is that the HiddenField's ID will be dynamic, so you can't (reliably) predict it before runtime.
Finally, in your server-side code that recieves the postback, you split out the delimited string and unencode it, then do what you want with the values.
The biggest caveat here is security and validation - although I've encoded the string, you need to do your own validation and security checks!