Creating Custom Web Control - asp.net

I'm trying to create a custom web control, but I have limited experience with Web Forms. How do I access the text passed in between the open an close tags? The control should work like the this.
Usage
<asp:YesOrNoQuestion>Are you above the age of 18 ? </asp:YerOrNoQuestion>
Control
<tr id="trQ6CA" runat="server" visible="false">
<td>
<asp:Label ForeColor="Black" ID="QuestionLabel" runat="server">
// Question Goes Here
</asp:Label>
</td>
<td>
<asp:RadioButtonList ID="buttonCollection" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator ControlToValidate="buttonCollection" ID="Requiredfieldvalidator" InitialValue="" runat="server" Text="*" ValidationGroup="vgApplication">
*
</asp:RequiredFieldValidator>
</td>
</tr>
Code I'm Replacing
<tr id="trQ6CA" runat="server" visible="false">
<td valign="top" align="left">
<asp:Label ID="Label10" runat="server" ForeColor="Black">
<b>DOES THE PROPOSED INSURED:</b>
Have a comprehensive health benefits from an insurance policy, an HMO plan, or employer health benefit plan? <br />
If No, persons without such comprehensive coverage are not eligible for this insurance policy.
</asp:Label>
</td>
<td valign="top">
<asp:RadioButtonList ID="rbtQ6S1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>
</td>
<td valign="top">
<asp:RequiredFieldValidator ID="Requiredfieldvalidator65" runat="server" Text="*" ControlToValidate="rbtQ6S1" InitialValue="" ValidationGroup="vgApplication">
*
</asp:RequiredFieldValidator>
</td>
</tr>

You can choose prefix and tag when you register the control:
https://msdn.microsoft.com/en-us/library/sbz9etab.aspx

Related

ASP.NET validator never validates as true

The code below has always worked, but now somehow I can't submit the form anymore even though I provide both username and password. I get no errors in backend or the Chrome Developer Console. I tried enabling/disabling all validators and found that if I add Enabled="false" to validator UserNameRequired, the form does submit. But obviously I want to have this requiredfieldvalidator enabled.
Why is this happening?
You can also see it live here (fill in a random email address/password): http://www.zorgbeurs.nl/login
<table width="300">
<tr>
<td>
<asp:Label ID="UserNameLabel" Text="Email" runat="server" AssociatedControlID="UserName"/>
</td>
<td>
<asp:TextBox ID="UserName" Text="" runat="server" Width="160px"/><br />
<asp:RequiredFieldValidator Enabled="false" ValidationGroup="loginuser" Display="Dynamic" SetFocusOnError="True" ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="*"/>
<asp:RegularExpressionValidator ControlToValidate="UserName" ValidationGroup="loginuser" ErrorMessage="Ongeldig emailadres" ValidationExpression="<%$resources:glossary,regexValidEmail %>" ID="revUsername" runat="server" Display="Dynamic"/>
</td>
</tr>
<tr>
<td valign="top">
<asp:Label ID="PasswordLabel" Text="<%$ Resources:Glossary,Password%>" runat="server" AssociatedControlID="Password"/>
</td>
<td valign="top">
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="160px"/><br />
<asp:RequiredFieldValidator Enabled="false" SetFocusOnError="True" Display="Dynamic" ValidationGroup="loginuser" ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="*" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ImageButton CausesValidation="true" ValidationGroup="loginuser" ID="LoginMember" meta:resourcekey="ibtnLogin" CommandName="Login" runat="server"/>
<asp:CheckBox TextAlign="Right" CssClass="cblist" Font-Bold="false" ID="RememberMe" runat="server" Text="<%$ Resources:Glossary,loginrememberme%>" /><br />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="FailureText" runat="server" ForeColor="Red" Font-Bold="true" EnableViewState="False"/>
</td>
</tr>
</table>
update
I also tried adding the ValidationExpression of revUsername expression directly in the code [-+.'\w]+#[-.\w]+\.[-.\w]+, but that does not help.
I can see the required validators working in your live site without making a postback.
Regarding the email's regular expression validator, it seems that your expression is not working properly. So, I suggest you to change your regular expression, try this one:
<asp:RegularExpressionValidator Enabled="true" ControlToValidate="UserName"
ValidationGroup="loginuser" ErrorMessage="Ongeldig emailadres"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ID="revUsername" runat="server" Display="Dynamic" />
Also, remember to enable it again in order to make it work both on client and server side.
Hope it can help you!

Remove certain parts of a webform based on selection from Drop Down List

I want to be able to be able to include and exclude parts of a form based on what type of request the user selects from a drop down list, also included on the form. Here is my current code. I can make the text boxes disappear but I can't make the labels disappear too.
<div>
<table>
<tr>
<td>
<font color="red">*</font>MAC Request Type:
</td>
<td>
<asp:DropDownList ID="ddlMACRequestType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMACRequestType_Change">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
<asp:ListItem Text="Add Device" Value="Add Device"></asp:ListItem>
<asp:ListItem Text="Change Device" Value="Change Device"></asp:ListItem>
<asp:ListItem Text="Delete Device" Value="Delete Device"></asp:ListItem>
<asp:ListItem Text="Alert Change" Value="Alert Change"></asp:ListItem>
<asp:ListItem Text="Other" Value="Other"></asp:ListItem>
</asp:DropDownList>
<asp:CheckBox ID="chkbxLiveSearch" runat="server" Visible="false" Checked="true" AutoPostBack="true" OnCheckedChanged="chkbxLiveSearch_Change" Text=" Use Live Search" />
</td>
</tr>
<tr>
<td>
<font color="red">*</font>Device:
</td>
<td>
<asp:TextBox ID="txtDevice" runat="server" MaxLength="255" width="375" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<font color="red">*</font>Interface:
</td>
<td>
<asp:TextBox ID="txtInterface" runat="server" MaxLength="255" width="375" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
Someone recommended I use an asp:label, but how would I add my red asterisks as well? They are important for the project. This is something that someone suggested, but isn't working as expected.
<asp:Panel Visible="false">
<td>
<font color="red">*</font>Device:
</td>
<td>
<asp:TextBox ID="txtDevice" runat="server" MaxLength="255" width="375" ClientIDMode="Static"></asp:TextBox>
</td>
</asp:Panel>
In order to hide the label, create an <asp:Label> for it. This will give you the server side functionality similar to your <asp:TextBox>.
To make things even easier, I will often lean towards the use of an <asp:Panel>. This is essentially a server side <div> and gives you the ability to hide many things all at once rather than hiding all of them individually.
This also won't leave a bunch of empty table rows and cells all over the place.
<asp:Panel ID="pnlDevice" runat="server">
<tr>
<td>
<font color="red">*</font>Device:
</td>
<td>
<asp:TextBox ID="txtDevice" runat="server" MaxLength="255" width="375" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
</asp:Panel>
Then just hide the panel like other controls.
pnlDevice.Visible = false;

asp:CreateUserWizard - how to display minimum password requirement

It's not displaying the min requirement until the 'CreateUser' is clicked. How do I make it to validate the password requirement beforehand and display a message when the password field loses focus.
With the default markup of CreateUserWizard control as below, you cannot have such functionality.
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
Therefore, the idea is to customize the CreateUserWizard control as per your needs. Its better to use Validators so that you can display the messages beforehand. They key is to create a <ContentTemplate> element within the <asp:CreateUserWizardStep> element.
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
Sign Up to get started</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">
User Name:</asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required."
ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
<tr>
<td>
<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" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
</ContentTemplate>
</asp:CreateUserWizardStep>
Read MSDN for a complete tutorial.

Validation for End Date should be greater or equal to Start Date in asp.net

I'm using this code to validate two date but the validation is not working when I enter the same or after the Start Date
<asp:CompareValidator ID="dateCompareValidator" runat="server"
ControlToValidate="EndDate"
ControlToCompare="StartDate"
Operator="GreaterThanEqual" Type="Date"
ErrorMessage="The second date must be after the first one.<br /><br />">
</asp:CompareValidator>
Do i need to use the hard code ?
Aspx File:
<tr>
<td align="right">
Start Date:
</td>
<td align="left">
<telerik:RadDatePicker ID="RadDtpFromDate" runat="server" Calendar-EnableShadows="true" AutoPostBack="true"
Culture="en-IN" DateInput-DateFormat="dd-MMM-yyyy" DateInput-EmptyMessage="DD-MMM-YYYY"
ShowPopupOnFocus="true" ToolTip="Input Date" ZIndex="30001"
onselecteddatechanged="RadDtpFromDate_SelectedDateChanged" />
<asp:RequiredFieldValidator runat="server" ID="rfvFromDate" Display="None" ControlToValidate="RadDtpFromDate"
InitialValue="" ValidationGroup="FinalSave" ErrorMessage="From Date is Mandatory"
ForeColor="Red"></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vceFromDate" TargetControlID="rfvFromDate"
runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
<asp:CustomValidator ID="cvFromDate" runat="server" ControlToValidate="RadDtpFromDate"
Display="None"></asp:CustomValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vceRadFromDate" runat="server" TargetControlID="cvFromDate">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
<td align="right">
End Date:
</td>
<td align="left" colspan="2">
<telerik:RadDatePicker ID="RadDtpToDate" runat="server" Calendar-EnableShadows="true" AutoPostBack="true"
Culture="en-IN" DateInput-DateFormat="dd-MMM-yyyy" DateInput-EmptyMessage="DD-MMM-YYYY"
ShowPopupOnFocus="true" ToolTip="Input Date" ZIndex="30001"
onselecteddatechanged="RadDtpToDate_SelectedDateChanged" />
<asp:RequiredFieldValidator runat="server" ID="rfvToDate" Display="None" ControlToValidate="RadDtpToDate"
InitialValue="" ValidationGroup="FinalSave" ErrorMessage=" To Date is Mandatory"
ForeColor="Red"></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vceToDate" TargetControlID="rfvToDate"
runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
<asp:CustomValidator ID="cvToDate" runat="server" ControlToValidate="RadDtpToDate"
Display="None"></asp:CustomValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vceRadToDate" runat="server" TargetControlID="cvToDate">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>
Cs File :
if (RadDtpFromDate.SelectedDate > RadDtpToDate.SelectedDate)
{
dateCompareValidator.IsValid = False;
return;
}
Now Try this..................

validation showing in both panels in asp.net

i am using two panels (login and registration) in asp.net where in both panels i used button control. and i also used validations for particular textboxes in both panels.
i want to show both panels at same time.
now what problem i am facing is that when i click on button of login panel, it shows validation error text in registration panel and vice-versa..
Is there nay other way to use any other control instead of button.
i am using visual studio-2008
i had created login panel as:
<asp:Panel ID="pnllogin" runat="server">
<table class = "style1">
<tr>
<td>
<asp:Label ID="lblloginid" runat="server" Text="Login_ID" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtboxloginid" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredfieldvalidator1" runat="server"
ControlToValidate="txtboxloginid" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblpassword" runat="server" Text="Password" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtboxpassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredfieldvalidator2" runat="server"
ControlToValidate="txtboxpassword" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:HyperLink ID="hyperlinkforgotpassword" runat="server" ForeColor="Red" NavigateUrl="~/ForgotPassword.aspx">Forgot Password??</asp:HyperLink>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:ImageButton ID="imgbtnlogin" runat="server" ImageUrl="~/images/login.png"
onclick="imgbtnlogin_Click" />
</td>
</tr>
</table>
</asp:Panel>
and somewhat same code for registration panel..
can someone please help me..
Make use of ValidationGroup property to fire the validation message related to proper panel
here is detail info : http://www.w3schools.com/ASPNET/prop_webcontrol_imagebutton_validationgroup.asp
demo : http://www.w3schools.com/ASPNET/showasp.asp?filename=demo_prop_webcontrol_imagebutton_validationgroup
Specifying Validation Groups

Resources