net application. With this application a user can add a guestconnection for a time. for this i have a button that open a modal dialog who a user can add a guest. the input must be a firstname,lastname,company and the time. i use the validation controls and a ValidationGroup. The Validationcontrols check if I forget a input but if I click on "add" the code doesn't run. I try this with a simple div but the same method:
Here is my aspx code:
<div id="add">
<div id="Div2" class="popupConfirmation" runat="server" style="width:350px; height:290px;">
<div class="bodycontrol">
<table>
<tr>
<td><asp:Label ID="Label3" runat="server" Text="Vorname"></asp:Label></td>
<td><asp:TextBox ID="TextBox1" runat="server" ValidationGroup="valid" ></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><asp:Label ID="Label4" runat="server" Text="Nachname"></asp:Label></td>
<td><asp:TextBox ID="TextBox2" runat="server" ValidationGroup="valid"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox2"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><asp:Label ID="Label5" runat="server" Text="Firma"></asp:Label></td>
<td><asp:TextBox ID="TextBox3" runat="server" ValidationGroup="valid"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox3"></asp:RequiredFieldValidator></td>
</tr>
</table>
<br />
<table>
<tr>
<td><asp:LinkButton ID="LinkButton1" runat="server" class="GuestButtons" Text="Hinzufügen" ValidationGroup="valid" onclick="btn_GuestListViewAddDialog_YES_Click" ></asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton2" runat="server" class="GuestButtons" Text="Abbrechen" ValidationGroup="never"></asp:LinkButton></td>
</tr>
</table>
<br />
<table>
<tr>
<td><asp:Label ID="Label10" runat="server" Text="*Bitte alle Felder ausfüllen"></asp:Label></td>
</tr>
</table>
</div>
</div>
</div>
here is my c# code:
protected void btn_GuestListViewAddDialog_YES_Click(object sender, EventArgs e)
{
if (Page.IsValid) //Here i make a breakpoit but it doesn't use this code :(
{
...//here is my code
}
}
this is in my script block if i click on the linkbutton:
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$lw_content$LinkButton1", "", true, "valid", "", false, true))
If you want to force validation on serverside you need to call Page.Validate() before you check Page.IsValid.
protected void btn_GuestListViewAddDialog_YES_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
The LinkButton must also have a ValidationGroup, the same valid-group if you want to validate this group if the link-button was clicked or a different group if you don't want to trigger validation.
Edit: However, Page.Validate should be redundant since CausesValidation is true by default for a LinkButton and you have specified also a ValidationGroup for it. So i'm at a loss.
Related
In past, i worked on ListViews (.net 2.0) using a custom Template field but what i am trying to achieve here is the following
I am now working on .net 4.6
So basically a list which shows items like above and on mouse-hover few options show up as shown in the following screenshot
I also have to trigger those option to do different things -
How can I do that in asp.net, may I please have some code references.
Cheers
P.S.
This is a rough example of how i am creating the List Item Template (as requested)
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<AlternatingItemTemplate>
<table >
<tr>
<td ><asp:Image ID="image1" ImageUrl='<%# Bind("url") %>' runat="server" Width="98px" /> </td>
<td><h2><asp:Label ID="_label" runat="server" Text ='<%# Bind("title") %>'></asp:Label></h2><asp:Label ID="Label1" runat="server" Text ='<%# Bind("description") %>'></asp:Label></td>
</tr>
</table>
</AlternatingItemTemplate>
<EmptyDataTemplate>
No data was returned.
</EmptyDataTemplate>
<ItemSeparatorTemplate>
<br />
</ItemSeparatorTemplate>
<ItemTemplate>
<table >
<tr>
<td ><asp:Image ID="image1" ImageUrl='<%# Bind("url") %>' runat="server" Width="98px" /> </td>
<td><h2><asp:Label ID="_label" runat="server" Text ='<%# Bind("title") %>'></asp:Label></h2><asp:Label ID="Label1" runat="server" Text ='<%# Bind("description") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<ul id="itemPlaceholderContainer" runat="server" style="">
<li runat="server" id="itemPlaceholder" />
</ul>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
I can add any html formatting to this template e,g i can add ASP:button etc but i don't know how to trigger those to perform certain tasks.
One easy way to achieve your requirement is to keep those buttons there but invisible and show them up when the parent container is hovered. following as a quick sample
aspx
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<tr class="row-data">
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="PositionLabel" runat="server" Text='<%# Eval("Position") %>' />
</td>
<td>
<div class="btn-area">
<asp:Button runat="server" Text="Button1" />
<asp:Button runat="server" Text="Button2" />
</div>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
Name
</th>
<th runat="server">
Position
</th>
<th>
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
css
.btn-area
{
display: none;
}
.row-data:hover .btn-area
{
display: block;
}
code-behind
protected void Page_Load(object sender, EventArgs e)
{
ListView1.DataSource = new List<dynamic>() {
new { Name = "Andy", Position = "PG"},
new { Name = "Bill", Position = "SD"},
new { Name = "Caroline", Position = "Manager"}
};
ListView1.DataBind();
}
UPDATE
ListView ItemCommand can capture the postback by button pressed and CommandName makes you able to recognize which button fired it.
<asp:Button runat="server" Text="Button1" CommandName="c1" />
<asp:Button runat="server" Text="Button2" CommandName="c2" />
code-behind
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "c1")
{
// do something when button1 pressed
}
else if (e.CommandName == "c1")
{
// do something when button2 pressed
}
}
I have written modalpopupextender and it is successfully displaying. My problem is after enter the text in modalpopup i will click on save button but it is not working, after clicking the button popup will close and data will not store in my database following are the aspx code
<asp:Button ID="freeservice" runat="server" CssClass="hidden" />
<asp:ModalPopupExtender ID="ModalPopupExtender" runat="server" BehaviorID="mpe" PopupControlID="panel" TargetControlID="freeservice" OkControlID="savetxt" BackgroundCssClass="modalBackground" CancelControlID="cancel"></asp:ModalPopupExtender>
<asp:Panel ID="panel" runat="server" CssClass="modalPopup" Style="display: none;">
<asp:Button ID="cancel" runat="server" Text="X" CssClass="modalDialog" />
<div class="Popup_header">
Free service
</div>
<div class="modalbody">
<h3>Do you want to get free text update on your mobile when IRS takes decision on your
form?
<br />
If yes, please enter mobile number on which you want to get update:</h3>
<table>
<tr>
<td>Mobile Number</td>
<td style="width: 1px">
<asp:TextBox ID="txtmobile" runat="server" CssClass="divtxt"></asp:TextBox>
<asp:RegularExpressionValidator ID="revmobile" runat="server" SetFocusOnError="true"
Display="None" ErrorMessage="Mobile number must be of 10 digits" ControlToValidate="txtmobile"
ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
<asp:ValidatorCalloutExtender ID="vce_revmobile" runat="server" PopupPosition="Right"
TargetControlID="revmobile" HighlightCssClass="errorField"></asp:ValidatorCalloutExtender>
<asp:FilteredTextBoxExtender ID="ftemobile" runat="server" TargetControlID="txtmobile" ValidChars="0123456789"></asp:FilteredTextBoxExtender>
</td>
<td>(Not Mandatory)</td>
</tr>
</table>
<h3>Do you want to get Schedule 1 by fax for free?
<br />
If yes, please enter fax number where you want to receive your Schedule 1:
</h3>
<table>
<tr>
<td style="width:24%">Fax Number</td>
<td style="width: 1px">
<asp:TextBox ID="txtfax" runat="server" CssClass="divtxt"></asp:TextBox>
<asp:RegularExpressionValidator ID="revfax" runat="server" ControlToValidate="txtfax" SetFocusOnError="true" Display="None" ErrorMessage="Fax number must be of 10 digits"></asp:RegularExpressionValidator>
<asp:ValidatorCalloutExtender ID="vce_revfax" runat="server" PopupPosition="Right" TargetControlID="revfax" HighlightCssClass="errorField"></asp:ValidatorCalloutExtender>
<asp:FilteredTextBoxExtender ID="ftefax" runat="server" TargetControlID="txtfax" ValidChars="0123456789"></asp:FilteredTextBoxExtender>
</td>
<td>(Not Mandatory)</td>
</tr>
</table>
</div>
<div style="text-align: right; padding-top: 6px;">
<asp:Button runat="server" ID="savetxt" OnClick="savetxt_Click" Text="Save" CssClass="btn" />
</div>
</asp:Panel>
and my back end code is
protected void savetxt_Click(object sender, EventArgs e)
{
try
{
Form2290.FreeServices objFreeService = new FreeServices();
objFreeService.FormID = Convert.ToString(Session["FORM_KEY"]);
objFreeService.Mobile = txtmobile.Text.Trim();
objFreeService.Fax = txtfax.Text.Trim();
BAL_F2290 objBAL = new BAL_F2290();
objBAL.SaveFreeServices(objFreeService);
}
catch (Exception ex)
{
string a = ex.Message;
}
}
please help me
sorry guys i made mistake in modalpopupextender i have set okcontrolID="savetxt" should not be there. Just i removed and code successfully executed. Thanks guys for helping me
To elaborate my question, I have shopping cart website. Also I have textboxes like username, address, contact etc. All of those has the required field validator. However I also do have the "view profile" and "log out" link button on the page. When I try to clike those links, its not letting me because the required fields are not filled up. Is there any trick I can do with this? Thank you for any answers :)
Here is my code.
<asp:TextBox ID="txtCustomerName" runat="server" Width="231px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Customer Name is Required." ForeColor="Red" ControlToValidate="txtCustomerName"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="left">
Phone No:
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtCustomerPhoneNo" runat="server" Width="231px"
MaxLength="11"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Phone Number is required." ForeColor="Red" ControlToValidate="txtCustomerPhoneNo"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="left">
EmailID:
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtCustomerEmailID" runat="server" Width="231px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage=" Email Address is Required." ForeColor="Red" ControlToValidate="txtCustomerEmailID"
></asp:RequiredFieldValidator>
</td>
and here is my link codes.
protected void link_ViewProfile_Click(object sender, EventArgs e)
{
Response.Redirect("viewprofile.aspx");
}
protected void link_Logout_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("Home.aspx");
}
For those link button on which you want to run validation add validationgroup attribute. For Example
<asp:button id="Button2"
text="Validate"
causesvalidation="true"
validationgroup="LocationInfoGroup"
runat="Server" />
Also add validationgroup attribute in RequiredFieldValidator
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Phone Number is required." ForeColor="Red" ControlToValidate="txtCustomerPhoneNo"
validationgroup="LocationInfoGroup"
></asp:RequiredFieldValidator>
here is the link on how i managed to solve it. https://msdn.microsoft.com/en-us/library/ms227424(v=vs.140).aspx. credit to #Tonny
I am trying to clear the form on cancel button click with a function clearForm() that uses String.Empty with each field of the form but this is not working. I am neither getting any error nor getting the expected result.
Here's my design code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="AddEmployee.aspx.cs" Inherits="AddEmployee" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Add Employee</title>
<link href="css/Style.css" type="text/css" rel="Stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="toolkit1" runat="server"></ajax:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table align="center" class="loginBox">
<tr>
<th colspan="2" style="color:White; font-size:medium; padding-bottom:10px;" align="left">Personal Information</th>
</tr>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="txtName" CssClass="signup_textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqName" ControlToValidate="txtName" runat="server" Display="None" ErrorMessage="Name" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Address</td>
<td><asp:TextBox TextMode="MultiLine" Height="40" Width="135" ID="txtAddress" CssClass="signup_textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqAdd" ControlToValidate="txtAddress" runat="server" Display="None" ErrorMessage="Address" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>DoB:</td>
<td>
<asp:TextBox ID="txtDoB" CssClass="signup_textbox cal_box" runat="server" />
<ajax:CalendarExtender ID="CalendarExtender1" TargetControlID="txtDoB" Format="dd/MM/yyyy" runat="server"></ajax:CalendarExtender>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:RadioButtonList CssClass="signup_textbox" ID="GenderList" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Selected="True">Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<th colspan="2" style="color:White; font-size:medium; padding-bottom:10px;" align="left">Professional Information</th>
</tr>
<tr>
<td>Department:</td>
<td>
<asp:DropDownList ID="ddlDept" CssClass="signup_textbox" runat="server"></asp:DropDownList>
</td>
</tr>
<tr>
<td>Designation:</td>
<td><asp:TextBox ID="txtDesig" CssClass="signup_textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqDesig" ControlToValidate="txtDesig" runat="server" Display="None" ErrorMessage="Designation" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Salary:</td>
<td><asp:TextBox ID="txtSal" CssClass="signup_textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqSal" ControlToValidate="txtSal" runat="server" Display="None" ErrorMessage="Salary" ></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server" id="rexSal" controltovalidate="txtSal" validationexpression="^[0-9]{5}$" errormessage="Salary must be max 5 digits" Display="None" />
</td>
</tr>
<tr>
<th colspan="2" style="color:White; font-size:medium; padding-bottom:10px;" align="left">Login Information</th>
</tr>
<tr>
<td>Email:</td>
<td>
<asp:TextBox ID="txtEmail" CssClass="signup_textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqEmail" ControlToValidate="txtEmail" runat="server" Display="None" ErrorMessage="Email" ></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="valEmail" runat="server" ErrorMessage="Invalid Email" ControlToValidate="txtEmail" ValidationExpression="^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$" CssClass="error_msg" Display="none" />
</td>
</tr>
<tr>
<td>Username:</td>
<td>
<asp:TextBox ID="txtUser" CssClass="signup_textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqUser" ControlToValidate="txtUser" runat="server" Display="None" EnableClientScript="true" SetFocusOnError="true" ErrorMessage="Username" ></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" Display="None" ControlToValidate="txtUser" ValidationExpression="^[\s\S]{3,}$" runat="server" ErrorMessage="Username must have at least 3 characters required."></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="txtPass" CssClass="signup_textbox" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqPass" ControlToValidate="txtPass" runat="server" Display="None" EnableClientScript="true" SetFocusOnError="true" ErrorMessage="Password" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:TextBox ID="txtConfPass" CssClass="signup_textbox" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqConfPass" ControlToValidate="txtConfPass" runat="server" Display="None" EnableClientScript="true" SetFocusOnError="true" ErrorMessage="Confirm Password" ></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cmpPass" runat="server" ControlToCompare="txtPass" ControlToValidate="txtConfPass" ErrorMessage="Password must match" CssClass="error_msg" Display="None"></asp:CompareValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="padding:5px;"><span><asp:Button ID="btnRegister" Width="60" runat="server" Text="Register" CssClass="btnLogin" OnClick="btn_AddEmp" /></span>
<span><asp:Button ID="btnClr" Width="60" runat="server" Text="Cancel" CssClass="btnLogin" OnClick="btn_ClearForm" /></span>
</td>
</tr>
</table>
<asp:Panel ID="errorsPanel" runat="server" Style="display: none; border-style: solid; border-width: thin; border-color: #FFDBCA" Width="175px" BackColor="White">
<asp:ValidationSummary ID="valSummary" runat="server" CssClass="error_msg" HeaderText="You must enter following" DisplayMode="BulletList" EnableClientScript="true" ShowSummary="true" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Here's my code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
public partial class AddEmployee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_ClearForm(object sender, EventArgs e) //to clear form on IsPostBack or on press of cancel button
{
if (!IsPostBack)
{
clearForm();
}
}
#region public functions
public void clearForm()
{
txtName.Text = String.Empty;
txtAddress.Text = String.Empty;
txtDoB.Text = String.Empty;
txtDesig.Text = String.Empty;
txtSal.Text = String.Empty;
txtEmail.Text = String.Empty;
txtUser.Text = String.Empty;
txtPass.Text = String.Empty;
txtConfPass.Text = String.Empty;
GenderList.Items[0].Selected = true;
GenderList.Items[1].Selected = false;
}
#endregion
}
There is nothing wrong with string.Empty. But you do not need condition in btn_ClearForm as btn_ClearForm handler will always called on postback and you put the condition if !IsPostBack
// if (!IsPostBack)
//{
clearForm();
// }
Your code would be
protected void btn_ClearForm(object sender, EventArgs e)
{
clearForm();
}
Got the fix!
Just use CausesValidation="false" on the cancel button so as to disable validation upon cancel button click event.
Because when you have used validation on your controls without using CausesValidation="false" on the cancel button, your controls will be forced to pass through validation conditions given to them and since the cancel button is meant to clear your form regardless of checking the validity of the data in form fields, the form will get stuck.
So whenever you have used validation conditions on your controls, just make sure you don't get them fired on the click of Cancel/clear/reset button.
Thanks
so let say i have a user control which called 7 times in an aspx file, i put field validator control in that user control, the problem is everytime i click some buttons which cause postback, field validator always validate controls against all instances of my user control, what i want is to validate against specific user control, i have tried this :
<asp:TextBox runat="server" ID="DateNew" CssClass="time" Width="185px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Date can not be empty" ControlToValidate="DateNew" ValidationGroup='<%= ClientID %>' Text="*"> </asp:RequiredFieldValidator>
<asp:Button runat="server" ID="NewSchedule" Text="Schedule" CssClass="bgrey"
OnClientClick="CheckKeyValues()" OnClick="NewSchedule_Click" ValidationGroup='<%= ClientId %>'/>
this is not working... any correction? thanx before. btw some example would be great.
update : aspx file with a user control called 7 times
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="divError" class="n fail" style="display:none; margin:15px; width:2330px">
<h5>
The following errors were found
</h5>
<asp:ValidationSummary ID="ValidationSummary" runat="server" />
</div>
<div class="warea clearfix" style="width: 2350px">
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress" TestType="Interview 1" />
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress1" TestType="Interview 2"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress2" TestType="Interview 3"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress3" TestType="English Test"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress4" TestType="Medical Examination"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress5" TestType="Internal Psycho Test"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress6" TestType="External Psycho Test"/>
</div>
</div>
user control file :
<h3><asp:Label runat="server" ID="TypeNew"/></h3>
<table>
<tr>
<td>Date</td>
<td>
<asp:TextBox runat="server" ID="DateNew" CssClass="time" Width="185px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Date can not be empty" ControlToValidate="DateNew">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Time</td>
<td>
<asp:TextBox runat="server" ID="HourNew" Width="40px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Time can not be empty" ControlToValidate="HourNew" Text="*">
</asp:RequiredFieldValidator>:
<asp:TextBox runat="server" ID="MinuteNew" Width="40px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Minute can not be empty" ControlToValidate="MinuteNew" Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Location</td>
<td>
<asp:TextBox runat="server" ID="LocationNew" TextMode="MultiLine"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Location can not be empty" ControlToValidate="LocationNew" Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Interviewer</td>
<td>
<asp:TextBox runat="server" ID="InterviewerNew" autocomplete="off" onchange="MarkFlag(this.id)"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Interviewer can not be empty" ControlToValidate="InterviewerNew" Text="*">
</asp:RequiredFieldValidator>
<asp:HiddenField runat="server" ID="InterviewerKeys"/>
<asp:HiddenField runat="server" ID="InterviewerEmail"/>
</td>
</tr>
<tr>
<td> </td>
<td><asp:Button runat="server" ID="NewSchedule" Text="Schedule" CssClass="bgrey" OnClientClick="CheckKeyValues()" OnClick="NewSchedule_Click" /></td>
</tr>
</table>
C# :
public String ValidationGroup
{
get
{
return RequiredFieldValidator1.ValidationGroup;
}
set
{
RequiredFieldValidator1.ValidationGroup = value;
RequiredFieldValidator2.ValidationGroup = value;
RequiredFieldValidator3.ValidationGroup = value;
RequiredFieldValidator4.ValidationGroup = value;
RequiredFieldValidator5.ValidationGroup = value;
NewSchedule.ValidationGroup = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ValidationGroup = this.ID;
}
Expose a property on your Web User Control named ValidationGroup:
public String ValidationGroup
{
get { return RequiredFieldValidator1.ValidationGroup; }
set {
RequiredFieldValidator1.ValidationGroup = value;
NewSchedule.ValidationGroup = value;
}
}
Then set it to a unique value for each instance you create on your web form.
Give the validation group value anything like "Name" as i have given.
And add the same validation group to both controls on which validation is to be fired and the control whose post back should fir validation.
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Field value is required" ControlToValidate="TextBox1"
ValidationGroup="Name"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="Name" />
<asp:Button ID="Button2" runat="server" Text="Button" />
As in this case TextBox1 validation fires only when Button1 is clicked not on Button2 click event.