I'm trying to build some simple user controls for a standard ASP.NET membership system. I have a CreateUserWizard control which, when submitted needs to add the user to a particular role.
The ascx file looks like this:
<asp:CreateUserWizard ID="CreateUserWizard2" runat="server" oncreateduser="CreateUserWizard2_CreatedUser">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizard2Step1" runat="server">
<ContentTemplate>
<div class="form-error">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</div>
<fieldset class="member-control">
<legend>Registration details</legend>
<div class="standard">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username*:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="text-input"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="Username is required." ToolTip="Username is required." ValidationGroup="CreateUserWizard1">Username is required.</asp:RequiredFieldValidator>
</div> ...etc
The C# code-behind looks like this:
namespace DocumentMembership
{
public partial class ClientRegister : System.Web.UI.UserControl
{
protected void CreateUserWizard2_CreatedUser(object sender, EventArgs e)
{
// add member to role
Roles.AddUserToRole(this.CreateUserWizard2.UserName, "Client");
However, I'm getting a build error saying:
'DocumentMembership.ClientRegister' does not contain a definition for 'CreateUserWizard2' and no extension method 'CreateUserWizard2' accepting a first argument of type 'DocumentMembership.ClientRegister' could be found (are you missing a using directive or an assembly reference?)
When I begin typing Roles.AddUserToRole(this. intellisense doesn't seem to give anything useful to be able to get the username from the form.
I'm really not very skilled at .NET so it's probably something obvious I'm doing wrong, but I just can't seem to find a fix.
Can anyone suggest how to get this working or what might be the issue?
Thanks!
you can try with this code
CreateUserWizard2.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
Got it. It was indeed something silly. The ascx file was inheriting from the wrong code-behind, hence it wasn't recognising any method of retrieving the username from the form.
if it's about the role assigning try this :
Roles.AddUserToRole((sender as CreateUserWizard).UserName, "LC");
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);
When I am using ValidationSummary to validate my page if I have duplicate errors my validation will show all this errors. I want to display a distinct list of errors. I think that the best approach is to ovverride an event. But I don't know what event to override. What is the event that deals with showing errors.
I don't want solutions for MVC projects!
ValidationSummary collect all the error in your input and display.
So indirectly you have answerd your question by yourself in your question
You just don't know the syntax i think.Here it is:
If you have some collection of input in aspx,you have defined also the regular expression for specific input.For example:
<div>
<asp:TextBox ID="txt" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt" ControlToValidate="txt" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:TextBox ID="txt1" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt1" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt1" ControlToValidate="txtEmail" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Save" ValidationGroup="Submit" OnClick="btnSubmit_Click" />
</div>
<div>
<asp:ValidationSummary ID="ValidationSummary"runat="server"ValidationGroup="Submit" />
</div>
So in every input or button you have to define ValidationGroup attribute
Else if you want to check in codebehind if all of this input are validated you have to do this:
if(Page.IsValid)
{
//your code here
}
I'm trying to get apply the ID from an asp.net control for a <label>'s for property for accessibility. For some reason I can't get the full ID that .NET creates.
HTML on ASPX Page
<td>
<label id="lblSelReport" runat="server" for="selReport">Select Report Type</label>
<asp:DropDownList ID="selReport" runat="server"></asp:DropDownList>
</td>
What is rendered is:
<td>
<label id="AxMstr_ExMstr_bodyPlaceHolder_bodyPlaceHolder_lblSelReport" for="selReport">Select Report Type</label>
<select name="AxMstr$ExMstr$bodyPlaceHolder$bodyPlaceHolder$selReport" id="AxMstr_ExMstr_bodyPlaceHolder_bodyPlaceHolder_selReport">
...
</select>
</td>
I would think the for property of <label> would be "AxMstr_ExMstr_bodyPlaceHolder_bodyPlaceHolder_selReport" since I'm giving it the ID of the DropDownList control. Clearly I'm doing something wrong but I don't know what.
Any thoughts? Thanks!
Server controls all have a 'ClientID' property, which resolves to whatever messed up value .Net gives that control at runtime. Something like this:
<asp:Label ID="lblName" runat="server" />
<%= lblName.ClientID %>
I've had a look at similar questions (1,2) but they don't appear to provide me a solution :S
I'm trying to add an extra field to a registration form that is already pre-built (i.e. comes with) in ASP.Net Web Form.
What I'm doing is, in the Register.aspx.cs backend I try to obtain the value txtCompanyInput.Text();:
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
... // some code here
String CompanyName = txtCompanyInput.Text();
... // other code here
Response.Redirect(continueUrl);
}
The Register.aspx itself looks like so:
...
<li>
<asp:Label runat="server" AssociatedControlID="ConfirmPassword">
Confirm password</asp:Label>
<asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
CssClass="field-validation-error" Display="Dynamic"
ErrorMessage="The confirm password field is required." />
<asp:CompareValidator runat="server" ControlToCompare="Password"
ControlToValidate="ConfirmPassword"
CssClass="field-validation-error" Display="Dynamic"
ErrorMessage="The password and confirmation password do not match." />
</li>
<li>
<asp:TextBox ID="txtCompanyInput" runat="server"></asp:TextBox>
</li>
...
When I try to build the project I run into an error saying:
Error 1 The name 'txtCompanyInput' does not exist in the current context c:\users\...\Account\Register.aspx.cs 62 34 ProjectTest
Is there a reason why I'm not able to reference the txtCompanyName field in the backend?
If you are using VS2013 update 5 like me, do these steps to regenerate the designer file:
Delete the stated Register.designer.cs
Highlight 'Register.aspx' (must do!)
Go Menu->'Project', Click 'Convert to Web Application'
Right click on anything in the solution explorer will NOT give you choice to 'Convert to web application'.
Hope this helps some beginners.
Ehsan Sajjad was on the right track. The Register.aspx.designer.cs file was not getting updated.
I've finally figured out this was due to the Textboxs and Labels being placed inside the asp:CreateUserWizard block - which you shouldn't do. Even after manually adding the elements to the designer.cs file would throw an error when referencing the Textbox (Object reference not set to an instance of an object)
I should have included a bigger code segment from the Register.aspx file in my initial question.
I have just started a new ASP.NET web application and I am totally new to everything and after moving a few things round I have been getting this error and I don't understand what it means.
It happens when I go to enter anything in the Password box.
Could someone please help me out on this issue.
EDIT:
<li>
<asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" onkeypress="capLock(event)" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
</li>
Try to do that inside
window.onload = function(){
// code goes here
}
I think you are trying to access DOM even before it is being created.
the document.getElementById('divMayus') is undefined
document.getElementById("divMayus") is returning null or undefined.
You should look at the rendered html and find the div that you are trying to reference to make sure the id is the same as you are trying to reach. Asp.net alters the ids of server controls depending on their containers. So if you moved stuff around, the id may have also been changed.