I have a form in which i am using input field with name Password1 . after postback i need to maintain the value in password using viewstate . How can i do it in ASP.NET .My code is for a registration form in which i have given a password field .i have given auto postback for the dropdown list . when the auto postback occurs the password field is getting empty. instead of that i need to maintain the value of the password as it is.
<div>
<%--<asp:ScriptManager runat="server"></asp:ScriptManager>--%>
<ajaxToolkit:ToolkitScriptManager runat="server">
</ajaxToolkit:ToolkitScriptManager>
<h1 style="color:bisque;text-align:center;">USER FORM REGISTRATION</h1>
<table class="auto-style1" style="color:crimson">
<tr>
<td class="auto-style2">First Name</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Last Name</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Password</td>
<td class="auto-style3">
<input id="Password1" type="password" runat="server"/></td>
</tr>
<tr>
<td class="auto-style2">Date of Birth</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox2" runat="server"
ClientIDMode="Static" ReadOnly="true"></asp:TextBox>
<asp:Image ID="imgCal" DescriptionUrl="#.jpg"
AlternateText="" runat="server" />
<ajaxToolkit:CalendarExtender ID="Calend" runat="server"
Animated="true" PopupButtonID="imgCal" TargetControlID="TextBox2">
</ajaxToolkit:CalendarExtender>
</td>
</tr>
<tr>
<td class="auto-style2">Age</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox7" runat="server"
ClientIDMode="Static" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Gender</td>
<td class="auto-style3">
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList></td>
</tr>
<tr>
<td class="auto-style2">Nationality</td>
<td class="auto-style3">
<asp:DropDownList ID="DropDownList1" runat="server"
Width="145px" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">n1</asp:ListItem>
<asp:ListItem Value="1">loc2</asp:ListItem>
<asp:ListItem Value="2">loc3</asp:ListItem>
<asp:ListItem Value="3">other</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox9" runat="server" Visible="false">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Mobile Number</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<asp:UpdatePanel ID="Update1" runat="server">
<ContentTemplate>
<tr>
<td class="auto-style2">Residential Adress</td>
<td class="auto-style3">
<textarea id="TextArea1" cols="20" name="TextArea1"
runat="server"></textarea></td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
</table>
</div>
by default password textbox will not retail value on postback.so you need following ways to preserve value based on this link
protected void Page_Load(object sender, EventArgs e)
{
string pwd = txtPwd.Text;
txtPwd.Attributes.Add("value", pwd);
}
Related
error creating control , Unable to create type 'cc1: TabContainer', could not load file or assembly System.Web.extension, Version=1.0.61025.0'
The system cannot find the file specified
im getting this error . can u tell me where im wrong? why cant i see my design in aspx ?
code
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:TabContainer runat="server" ID="Tab_Continer" ActiveTabIndex="0">
<cc1:TabPanel ID="TabPnl_Project" runat="server">
<HeaderTemplate>
Keyword Position</HeaderTemplate>
<ContentTemplate>
<table>
<tr>
<td>
<asp:Label ID="lbl_Project" runat="server" Text="Project"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlProject" runat="server" OnSelectedIndexChanged="ddlProject_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator3" runat="server"
ErrorMessage="*" ControlToValidate="ddlProject" ValidationGroup="ValPos"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLkeyword" runat="server" Text="Keyword"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlKeyword" runat="server" OnSelectedIndexChanged="ddlKeyword_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator2" runat="server"
ErrorMessage="*" ControlToValidate="ddlKeyword" ValidationGroup="ValPos"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblPos" runat="server" Text="Position"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPostion" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="txtPostion" ValidationGroup="ValPos"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtPostion"
ValidationExpression="^[0-9]{0,3}$" ErrorMessage="*" ValidationGroup="ValPos"
ToolTip="Enter only numeric values" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl_Date" runat="server" Text="Date"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="Ajax_Calend" runat="server" TargetControlID="txtDate">
</cc1:CalendarExtender>
<asp:RequiredFieldValidator Display="Dynamic" ID="Valid_Req_DtFrm" runat="server"
ErrorMessage="*" ControlToValidate="txtDate" ValidationGroup="ValPos"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<asp:Button ID="btn_Submit" runat="server" Text="Submit" ValidationGroup="ValPos"
OnClick="btn_Submit_Click" CssClass="button" />
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<asp:Label ID="LblMsg" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="Tab_Update" runat="server">
<HeaderTemplate>
Traffic</HeaderTemplate>
<ContentTemplate>
<asp:UpdatePanel ID="update12" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblProject" runat="server" Text="Project"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddl_Project" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator4" runat="server"
ErrorMessage="*" ControlToValidate="ddl_Project" ValidationGroup="ValTraffic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTraffic" runat="server" Text="Traffic"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTarffic" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator5" runat="server"
ErrorMessage="*" ControlToValidate="txtTarffic" ValidationGroup="ValTraffic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtTarffic"
ValidationExpression="^[0-9]{0,5}$" ErrorMessage="*" ToolTip="Enter only numeric values"
ValidationGroup="ValTraffic" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Date"></asp:Label>
</td>
<td>
<asp:TextBox ID="Txt_Date" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="Txt_Date">
</cc1:CalendarExtender>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator6" runat="server"
ErrorMessage="*" ControlToValidate="Txt_Date" ValidationGroup="ValTraffic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<asp:Button ID="btnSave" runat="server" ValidationGroup="ValTraffic" Text="Submit"
OnClick="btnSave_Click" CssClass="button" />
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<asp:Label ID="lbl_Msg" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
The code below get me a comment , puts it in the database and then waits for a page reload to show the comment. How can i make the submited comment to be shown right after it has been posted along with the new ones ? This is the code that does the stuff :
Layout:
<table>
<tr>
<td colspan="2">All Comments</td>
<td>
 
</td>
</tr>
<tr> <td colspan="2"> </td></tr>
<tr>
<td>
<asp:Repeater ID="Repeater1" runat="server" ItemType="SiteStiri.Models.Comments" SelectMethod="GetComments" >
<ItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label id="lblDate" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblComment" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
<AlternatingItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label id="Label1" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label2" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</td>
</tr>
<td>
<table>
<tr>
<td colspan="2" >Add Your Comment</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server" Visible="False"></asp:Label>
</td>
<td>
 
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtcomment" runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
<td>
 
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
<td>
 
</td>
</tr>
</table>
</table>
</td>
</tr>
</table>
Code Behind:
public void fnSubmitComment()
{
AddComment cmt = new AddComment();
String x = Request.QueryString["newsID"].ToString();
int y = Convert.ToInt32(x);
bool addSucces = cmt.Addcomment( y , txtcomment.Text);
if (addSucces)
{
lblStatus.Text = "Your Comment has been Added Successfully.";
}
else
{
lblStatus.Text = "Your Comment has not been Added.";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
fnSubmitComment();
}
public IQueryable<Comments> GetComments()
{
var _db = new SiteStiri.Models.CommentsContext();
IQueryable<Comments> query = _db.Comments;
String x = Request.QueryString["newsID"].ToString();
int y = Convert.ToInt32(x);
query = query.Where(p => p.NewsID == y);
return query;
}
I heard i can do that with ajax ? (My knowledge about ajax is 0 currently) Any tips on how i can do this ?
You must wrapper you repeater in an updatepanel,
set UpdateMode="Conditional" and add a trigger on btnSubmit so when click the button the update the repeater with the normal page life cycle
Remember to bind the repeater after that you add the commet in the codebehind and must be present in your page a ScriptManager control
more or less thus
<asp:ScriptManager runat="server" />
<table>
<tr>
<td colspan="2">All Comments</td>
<td> 
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" ItemType="SiteStiri.Models.Comments" SelectMethod="GetComments">
<ItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label ID="lblDate" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblComment" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
<AlternatingItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label2" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td colspan="2">Add Your Comment</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server" Visible="False"></asp:Label>
</td>
<td> 
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtcomment" runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
<td> 
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
<td> 
</td>
</tr>
</table>
</td>
</tr>
</table>
I've got 5 required field validators on an aspx page.
With all validators active, none of the code behind any of the command buttons on the page will fire.
However, with ANY four out of five validators active, buttons all function ok.
Is there a limit to the amount of validators that can be used on a single page? Is there any other reason that this could be happening?
Help appreciated, thanks..
Daf.
Markup looks like this..
> <%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/Admin.Master" AutoEventWireup="true" CodeBehind="RegisterUser.aspx.cs" Inherits="LectureQuestions.Interface.Admin.RegisterUser" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">Register New User
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="TitleTxt" runat="server">Register New User
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<table ID="tblInputField" runat="server" width="100%" cellspacing="15">
<tr>
<td width="10%"></td>
<td width="20%">First Name</td>
<td width="60%">
<asp:TextBox ID="txtFirstName" runat="server" Width="98%"></asp:TextBox>
</td>
<td width="10%">
<asp:RequiredFieldValidator ID="txtFirstNameRequired" runat="server"
ControlToValidate="txtFirstName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="20%">Last Name</td>
<td width="60%">
<asp:TextBox ID="txtLastName" runat="server" Width="98%"></asp:TextBox>
</td>
<td width="10%">
<asp:RequiredFieldValidator ID="txtLastNameRequired" runat="server"
ControlToValidate="txtLastName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="20%">E-mail</td>
<td width="60%">
<asp:TextBox ID="txtEmail" runat="server" Width="98%"></asp:TextBox>
</td>
<td width="10%">
<asp:RequiredFieldValidator ID="txtEmailRequired" runat="server"
ControlToValidate="txtEmail" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="20%">Phone</td>
<td width="60%">
<asp:TextBox ID="txtPhone" runat="server" Width="98%"></asp:TextBox>
</td>
<td width="10%">
<asp:RequiredFieldValidator ID="txtPhoneRequired" runat="server"
ControlToValidate="txtPhone" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="20%">Password</td>
<td width="60%">
<asp:TextBox ID="txtPassword" runat="server" Width="98%"></asp:TextBox>
</td>
<td width="10%">
<asp:RequiredFieldValidator ID="txtPasswordRequired" runat="server"
ControlToValidate="txtPassword" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="20%">User Type</td>
<td width="60%">
<asp:DropDownList ID="txtUserType" runat="server" Width="99.5%">
<asp:ListItem>-- Please Select --</asp:ListItem>
<asp:ListItem>Student</asp:ListItem>
<asp:ListItem>Lecturer</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>
</td>
<td width="10%">
<asp:Label id="txtUserTypeValidation" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="20%">Course</td>
<td width="60%">
<asp:DropDownList ID="txtCourse" runat="server" Width="99.5%">
</asp:DropDownList>
</td>
<td width="10%"></td>
</tr>
</table>
<table ID="tblAllValidationMsg" runat="server" width="100%" cellspacing="5">
<tr>
<td width="10%"></td>
<td width="80%" align="center">
<asp:Label id="allValidationMsg" runat="server" height="22px" ForeColor="Red"></asp:Label>
</td>
<td width="10%"></td>
</tr>
</table>
<table ID="tblCommandButtons" runat="server" width="100%" cellspacing="10">
<tr>
<td width="25%"></td>
<td width="25%" align="right">
<asp:Button class="navbutton" ID="btnRegister" runat="server"
Text="Register User" OnClick="btnRegister_Click" />
</td>
<td width="25%" align="left">
<asp:Button class="navbutton" ID="btnCancel" runat="server"
Text="Cancel" onclick="btnCancel_Click" />
</td>
<td width="25x%"></td>
</tr>
</table>
</asp:Content>
Answer:
No there is no limit to validations on the page.
Misc:
Pranay has provided the fix in his answer.
In the end, I placed a second regular expression validator on one of the fields for testing.
When I removed this validator, problem ceased. Don't know how or why; any advice on this welcome..
I'm trying to implement validation in my ASP.NET Webforms application. Right now, the only validator that works is for the Date of Birth (DOB) field. It is a custom field.
ASPX Code
<table>
<tr> <td align="center" colspan="2">
<asp:Label ID="RegisterTitleID" runat="server">Register here for your New Account </asp:Label></td>
</tr>
<tr>
<td align="right"> <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="EmailTextBox">E-mail:</asp:Label> </td>
<td> <asp:TextBox ID="EmailTextBox" runat="server" Height="25px" Width="200px"
CausesValidation="True"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server"
ControlToValidate="EmailTextBox" ErrorMessage="E-mail is required."
ToolTip="E-mail is required." ValidationGroup="RegistrationWizard"
Display="Dynamic">*</asp:RequiredFieldValidator> </tr>
<tr>
<td align="right"> <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="PasswordTextBox">Password:</asp:Label> </td>
<td> <asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password"
Height="25px" Width="200px" CausesValidation="True"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server"
ControlToValidate="PasswordTextBox" ErrorMessage="Password is required."
ToolTip="Password is required." ValidationGroup="RegistrationWizard"
Display="Dynamic">*</asp:RequiredFieldValidator> </tr>
<tr>
<td align="right"> <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPasswordTextBox">Confirm Password:</asp:Label> </td>
<td> <asp:TextBox ID="ConfirmPasswordTextBox" runat="server" TextMode="Password" Height="25px" Width="200px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server"
ControlToValidate="ConfirmPasswordTextBox" ErrorMessage="Confirm Password is required."
ToolTip="Confirm Password is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>
<tr>
<td align="center" colspan="2">
<asp:CompareValidator ID="PasswordCompare" runat="server"
ControlToCompare="PasswordTextBox" ControlToValidate="ConfirmPasswordTextBox"
Display="Dynamic"
ErrorMessage="The Password and Confirmation Password must match."
ValidationGroup="RegistrationWizard"> </asp:CompareValidator> </td> </tr>
<tr>
<td align="right"> <asp:Label ID="TitleLabel" runat="server" AssociatedControlID="TitleTextBox">Title:</asp:Label> </td>
<td> <asp:TextBox ID="TitleTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="TitleRequired" runat="server"
ControlToValidate="TitleTextBox" ErrorMessage="Title is required."
ToolTip="Title is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>
<tr>
<td align="right"> <asp:Label ID="FirstNameLabel" runat="server" AssociatedControlID="FirstNameTextBox">First Name:</asp:Label> </td>
<td> <asp:TextBox ID="FirstNameTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="FirstNameRequired" runat="server"
ControlToValidate="FirstNameTextBox" ErrorMessage="First Name is required."
ToolTip="First Name is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>
<tr>
<td align="right"> <asp:Label ID="SurnameLabel" runat="server" AssociatedControlID="SurnameTextBox">Surname:</asp:Label> </td>
<td> <asp:TextBox ID="SurnameTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="SurnameRequired" runat="server"
ControlToValidate="SurnameTextBox" ErrorMessage="Surname is required."
ToolTip="Surname is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>
<tr>
<td align="right"> <asp:Label ID="DOBLabel" runat="server" AssociatedControlID="DOBTextBox">Date of Birth:</asp:Label> </td>
<td> <asp:TextBox ID="DOBTextBox" runat="server" Height="25px" Width="200px">dd/mm/yyyy</asp:TextBox>
</td>
<asp:RequiredFieldValidator ID="DOBRequired" runat="server"
ControlToValidate="DOBTextBox" ErrorMessage="Date of birth is required."
ToolTip="Date of birth is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator>
<asp:CustomValidator runat="server"
ID="DateRangeCustomValidator"
ControlToValidate="DOBTextBox"
onservervalidate="valDateRange_ServerValidate"
ErrorMessage="Enter a valid date. " />
</tr>
<tr>
<td align="right"> <asp:Label ID="GenderLabel" runat="server" AssociatedControlID="GenderDropDownList">Gender:</asp:Label> </td>
<td> <asp:DropDownList ID="GenderDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList> </td>
<asp:RequiredFieldValidator ID="GenderRequired" runat="server"
ControlToValidate="GenderDropDownList" ErrorMessage="Gender is required."
ToolTip="Gender is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator>
</tr>
<tr>
<td align="right"> <asp:Label ID="PhoneNumberLabel" runat="server" AssociatedControlID="PhoneNumberTextBox">PhoneNumber:</asp:Label> </td>
<td> <asp:TextBox ID="PhoneNumberTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td></tr>
<tr>
<td align="right"> <asp:Label ID="FavouriteTeamLabel" runat="server" AssociatedControlID="FavouriteTeamDropDownList">Favourite Team:</asp:Label> </td>
<td>
<asp:DropDownList ID="FavouriteTeamDropDownList" runat="server" DataSourceID="Team"
DataTextField="Team" DataValueField="Team" Height="27px" Width="205px">
</asp:DropDownList>
<asp:SqlDataSource ID="Team" runat="server"
ConnectionString="<%$ ConnectionStrings:RocoSportsDBConnectionString %>"
SelectCommand="SELECT [Team] FROM [Team]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td align="right"> <asp:Label ID="MaritialStatusLabel" runat="server" AssociatedControlID="MaritialStatusDropDownList">Maritial Status:</asp:Label> </td>
<td> <asp:DropDownList ID="MaritialStatusDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>Single</asp:ListItem>
<asp:ListItem>Married</asp:ListItem>
<asp:ListItem>Separated</asp:ListItem>
<asp:ListItem>Unmarried Relationship</asp:ListItem>
<asp:ListItem>Domestic Partnership</asp:ListItem>
</asp:DropDownList> </td>
</tr>
<tr>
<td align="right"> <asp:Label ID="EducationLabel" runat="server" AssociatedControlID="EducationDropDownList">Education:</asp:Label> </td>
<td> <asp:DropDownList ID="EducationDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>None</asp:ListItem>
<asp:ListItem>Primary School</asp:ListItem>
<asp:ListItem>Secondary School</asp:ListItem>
<asp:ListItem>College</asp:ListItem>
<asp:ListItem>Undergrad</asp:ListItem>
<asp:ListItem>Postgrad</asp:ListItem>
</asp:DropDownList> </td>
</tr>
<tr>
<td align="right"> <asp:Label ID="ProfessionLabel" runat="server" AssociatedControlID="ProfessionDropDownList">Profession:</asp:Label> </td>
<td> <asp:DropDownList ID="ProfessionDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>None</asp:ListItem>
<asp:ListItem>Management</asp:ListItem>
<asp:ListItem>Professional</asp:ListItem>
<asp:ListItem>Armed Forces</asp:ListItem>
<asp:ListItem>Sales</asp:ListItem>
<asp:ListItem>Adminstrative</asp:ListItem>
<asp:ListItem>Farming</asp:ListItem>
<asp:ListItem>Construction</asp:ListItem>
</asp:DropDownList> </td>
</tr>
<tr>
<td align="right"> <asp:Label ID="SalaryLabel" runat="server" AssociatedControlID="SalaryDropDownList">Salary:</asp:Label> </td>
<td> <asp:DropDownList ID="SalaryDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>None</asp:ListItem>
<asp:ListItem>5000</asp:ListItem>
<asp:ListItem>10000</asp:ListItem>
<asp:ListItem>20000</asp:ListItem>
<asp:ListItem>30000</asp:ListItem>
<asp:ListItem>50000</asp:ListItem>
<asp:ListItem>100000</asp:ListItem>
</asp:DropDownList> </td>
</tr>
<tr>
<td align="right"> <asp:Label ID="EmploymentStatusLabel" runat="server" AssociatedControlID="EmploymentStatusDropDownList">EmploymentStatus:</asp:Label> </td>
<td> <asp:DropDownList ID="EmploymentStatusDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem>Unemployed</asp:ListItem>
<asp:ListItem>Employed</asp:ListItem>
<asp:ListItem>Self-employed</asp:ListItem>
<asp:ListItem>Volunteer</asp:ListItem>
</asp:DropDownList> </td>
</tr>
<tr>
<td align="right"> <asp:Label ID="InterestsLabel" runat="server" AssociatedControlID="InterestsCheckBox">Interests:</asp:Label> </td>
<td>
<asp:CheckBoxList ID="InterestsCheckBox" runat="server"
onselectedindexchanged="InterestsCheckBox_SelectedIndexChanged">
<asp:ListItem>Reading</asp:ListItem>
<asp:ListItem>Football</asp:ListItem>
<asp:ListItem>Swimming</asp:ListItem>
<asp:ListItem>Basketball</asp:ListItem>
<asp:ListItem>Cooking</asp:ListItem>
<asp:ListItem>Music</asp:ListItem>
<asp:ListItem>Philosophy</asp:ListItem>
<asp:ListItem>Programming</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr> <td align="center" colspan="2" style="color:Red;"> <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal> </td> </tr>
<tr> <td align="center" colspan="2"> <asp:Button ID="RegisterButton" runat="server" Text="Register" onclick="registerbutton_Click"/> </td> </tr>
</table>
What could be the problem that's causing this code not to work, yet is not throwing any errors? Also, I'd like to position the validators better, and add color to them?
You are using a ValidationGroup for all your validators except the DOB Custom validator.
Add it to your custom validator and also the Register Button.
<asp:CustomValidator runat="server"
ID="DateRangeCustomValidator"
ControlToValidate="DOBTextBox"
onservervalidate="valDateRange_ServerValidate"
ErrorMessage="Enter a valid date. "
ValidationGroup="RegistrationWizard"/>
<asp:Button ID="RegisterButton" runat="server" Text="Register"
ValidationGroup="RegistrationWizard" onclick="registerbutton_Click"/>
You have ValidationGroup set on your validators but not on your button.
I am rather confused I have got a createuser wizard control that I use with my custom user classes and role classes.
I have a step so that the admin user can create new users and assign them their roles. The OnActivate method is firing but the OnDeactivate is not. I have put break points on both methods and confirmed that it is not going into the method at all. I have included both the markup and code behind below, (sorry it is a bit long I did not want to cut anything out incase that was the problem):
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" ContinueDestinationPageUrl="~/admin/default.aspx"
DisableCreatedUser="false" Font-Names="Verdana" BackColor="white" Font-Size="10pt"
BorderWidth="1px" BorderColor="#CCCC99" BorderStyle="Solid" CompleteSuccessText="The account has been successfully created."
UnknownErrorMessage="The account was not created. Please try again.">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep2" runat="server" Title="Step 1: Basic User Information"
OnDeactivate="UpdateCurrentLvl_OnDeactivate">
<ContentTemplate>
<table style="font-size: 10pt; font-family: Verdana" border="0" width="400">
<tr>
<td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
colspan="2">
Create Your UserID
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">UserID:</asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ToolTip="User Name is required."
ErrorMessage="User Name is required." ValidationGroup="CreateUserWizard1" ControlToValidate="UserName">
*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ToolTip="Password is required."
ErrorMessage="Password is required." ValidationGroup="CreateUserWizard1" ControlToValidate="Password">
*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Re-Type Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ToolTip="Confirm Password is required."
ErrorMessage="Confirm Password is required." ValidationGroup="CreateUserWizard1"
ControlToValidate="ConfirmPassword">
*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">Email:</asp:Label>
</td>
<td>
<asp:TextBox ID="Email" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<br />
<br />
<table style="font-size: 10pt; font-family: Verdana" border="0" width="400">
<tr>
<td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
colspan="2">
If You Forget Your Password
</td>
</tr>
<tr>
<td align="right">
Security Question:
</td>
<td>
<asp:DropDownList ID="Question" runat="server" Width="200">
<asp:ListItem Text="[Select a Question]" />
<asp:ListItem Text="Favorite Pet" />
<asp:ListItem Text="Mother's Maiden Name" />
<asp:ListItem Text="Who was your childhood hero?" />
<asp:ListItem Text="Your favorite pasttime?" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="QuestionRequired" runat="server" InitialValue="[Select a Question]"
ControlToValidate="Question" ErrorMessage="Select a Security Question" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" width="170">
Your Answer:
</td>
<td>
<asp:TextBox ID="Answer" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
ErrorMessage="RequiredFieldValidator" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
</table>
<br />
<br />
<table style="font-size: 10pt; font-family: Verdana" border="0" width="400">
<tr>
<td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
colspan="2">
Help Us Customize Your Experience
</td>
</tr>
<tr>
<td align="right" width="170">
Set Level of the user:</td>
<td>
<asp:DropDownList ID="cLevel" runat="server">
<asp:ListItem Text="[Select a Level]" />
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" InitialValue="[Select a Level]"
ControlToValidate="cLevel" ErrorMessage="Select a Level" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:CompareValidator ID="PasswordCompare" runat="server" ErrorMessage="The Password and Confirmation Password must match."
ValidationGroup="CreateUserWizard1" ControlToValidate="ConfirmPassword" ControlToCompare="Password"
Display="Dynamic">
</asp:CompareValidator>
</td>
</tr>
<tr>
<td style="color: red" align="center" colspan="2">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:WizardStep runat="server" ID="wsAssignUserToRoles" AllowReturn="False" Title="Step 2: Assign User To Roles"
OnActivate="AssignUserToRoles_Activate" OnDeactivate="AssignUserToRoles_Deactivate">
<table>
<tr>
<td>
Select one or more roles for the user:</td>
</tr>
<tr>
<td>
<asp:ListBox ID="AvailableRoles" runat="server" SelectionMode="Multiple" Height="104px"
Width="264px"></asp:ListBox>
</td>
</tr>
</table>
</asp:WizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep2" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
<TitleTextStyle Font-Bold="True" BackColor="#6B696B" ForeColor="White"></TitleTextStyle>
</asp:CreateUserWizard>
The code behind is:
public partial class admin_createuser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
// Create an empty Profile for the newly created user
ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
// Populate some Profile properties off of the create user wizard
p.CurrentLevel = Int32.Parse(((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("clevel")).SelectedValue);
// Save profile - must be done since we explicitly created it
p.Save();
}
// Activate event fires when the user hits "next" in the CreateUserWizard
public void AssignUserToRoles_Activate(object sender, EventArgs e)
{
// Databind list of roles in the role manager system to a listbox in the wizard
AvailableRoles.DataSource = Roles.GetAllRoles(); ;
AvailableRoles.DataBind();
}
// Deactivate event fires when user hits "next" in the CreateUserWizard
public void AssignUserToRoles_Deactivate(object sender, EventArgs e)
{
// Add user to all selected roles from the roles listbox
for (int i = 0; i < AvailableRoles.Items.Count; i++)
{
if (AvailableRoles.Items[i].Selected == true)
Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items[i].Value);
}
}
//set the user level to the value set in the form as it has been inserted into the DB at this point.
public void UpdateCurrentLvl_OnDeactivate(object sender, EventArgs e)
{
int level = Int32.Parse(((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("clevel")).SelectedValue);
MyMembershipUser myUser = (MyMembershipUser)Membership.GetUser(CreateUserWizard1.UserName);
myUser.CurrentLVL = level;
Membership.UpdateUser(myUser);
}
}
Well I seem to have found "an" answer... no idea why this would be the case.
I had the create user wizard page under /admin/createuser.aspx. When I put this in the root of the website ie.. /createuser.aspx it all worked!!!!
can't explain it but it seemed to fix the problem.. very odd!
..... edit
I spoke too soon. It seems to be a permissions thing. I think it wants to log me (the user who is creating another account) out and log in the new user, who is not in any groups yet. This means they can not access any page that has security on it in the web.config which mine do.
question is how can I stop the person being logged out and give the new user permissions?
My ultimate solution was to use this statement in the CreateUserWizard control:
LoginCreatedUser="false"
It simply stopped the user from being logged in and removed any worries of the permissions issues as the currently logged in user had to have permissions for that page in the first place.