ASP LinkButton onclick show div - c#

I'm new at Jquery and im trying to display a div on the LinkButton ClickEvent (If I use a button instead of a linkbutton it will work)
This is my Jquery code
<script type="text/javascript">
$(document).ready(function () {
$('#<%=lbLog.ClientID%>').click(function () {
$("#login").show(2000);
alert("hello");
});
});
</script>
The hello message is displayed by not the div
and this is my html code:
<asp:LinkButton ID="lbLog" runat="server" onclick="lbLog_Click">Login</asp:LinkButton>
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
<div id="login" style="display:none">
Username: <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUsername" runat="server" ControlToValidate="txtUsername" Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
<asp:Button ID="btnLog" runat="server" Text="Login" onclick="btnLog_Click" ValidationGroup="LogGroup" />
</div>

try this
$("#login").css('display','block');

The ASP.NET link button will cause a postback which will refresh the page and hide the div thanks to style="display:none" no matter how you change your jQuery function. Try this instead:
<script type="text/javascript">
$(document).ready(function () {
$("#fakeLink").click(function () {
$("#login").show(2000);
});
});
</script>
<style type="text/css">
.fakeLink
{
color: blue;
text-decoration: underline;
cursor: pointer;
}
</style>
<div>
<span class="fakeLink" id="fakeLink">Login</span>
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
<div id="login" style="display:none;">
Username:
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUsername" runat="server" ControlToValidate="txtUsername"
Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"
Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
<asp:Button ID="btnLog" runat="server" Text="Login" OnClick="btnLog_Click" ValidationGroup="LogGroup" />
</div>
</div>

You don't need onclick="lbLog_Click". The jquery selector for the ID looks incorrect '#<%=lbLog.ClientID%>'. View the source and make sure the id on the linkbutton matches what your passing to the selector in your javascript code.

Related

AsyncFileUpload in Repeater in UpdatePanel

The AsyncFileUpload works. Only issue is the file name disappears when the LinkButton to repeat the AsyncFileUpload control is pressed. Is there a way to get and store the file name? FileName does not work. Not really keen on sharing code-behind but may do so if it is necessary to solve this issue.
<asp:UpdatePanel ID="LibraryResourceUpdatePanel" runat="server">
<ContentTemplate>
<div class="field-group list-of-resource">
<asp:Repeater ID="RptRequest" runat="server" OnItemDataBound="RptRequest_ItemDataBound">
<ItemTemplate>
<div class="resource">
<div class="remove-input">
<asp:LinkButton ID="LbRemoveRequest" CssClass="ic fa fa-minus-circle" runat="server" OnClick="LbRemoveRequest_Click" CausesValidation="false"></asp:LinkButton>
<span>Remove</span>
</div>
<h2>Details of Resources
<span class="counter">
<asp:Literal ID="LitCount" runat="server"></asp:Literal>
</span>
</h2>
<ul>
<li>
<fieldset class="form-group">
<legend>Accession No.</legend>
<asp:TextBox ID="TxbAccessionNumber" CssClass="form-control" runat="server" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="TxbAccessionNumber" ErrorMessage="Email is required" ForeColor="Red" Display="Dynamic" />
</fieldset>
</li>
<li>
<fieldset class="form-group">
<legend>Details</legend>
<asp:TextBox ID="TxbDetails" runat="server" Rows="4" TextMode="MultiLine" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="TxbDetails" ErrorMessage="Details are required" ForeColor="Red" Display="Dynamic" />
</fieldset>
</li>
<li>
<fieldset class="form-group">
<legend>Image</legend>
<ajaxToolkit:AsyncFileUpload runat="server"
ID="FileUpload" OnUploadedComplete="FileUpload_UploadedComplete" ClientIDMode="AutoID" PersistFile="true"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="FileUpload" ErrorMessage="File Upload required" ForeColor="Red" Display="Dynamic" />
</fieldset>
</li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div class="add-input">
<asp:LinkButton ID="LbAddRequest" CssClass="ic fa fa-plus-circle" runat="server" OnClick="LbAddRequest_Click" CausesValidation="false" ></asp:LinkButton>
<span>Add another request</span>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LbAddRequest" EventName="click" />
</Triggers>
</asp:UpdatePanel>
First add command name to linkbutton and remove click event:
<asp:LinkButton ID="LbAddRequest" runat="server"
CommandName="AddRequest"></asp:LinkButton>
<span>Add another request</span>
And try this ItemCommand event in code-behind to get in work:
protected void RptRequest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "AddRequest")
{
FileUpload myFileUpload = (FileUpload)e.Item.FindControl("FileUpload");
if (myFileUpload.HasFile)
{
try
{
string filename = Path.GetFileName(myFileUpload.FileName);
myFileUpload.SaveAs(Server.MapPath("~/") + filename);
myStatusLabel.Text = "Upload Success";
}
catch (Exception ex)
{
myStatusLabel.Text = "Upload Fail" + ex.Message;
}
}
else
{
myStatusLabel.Text = "myFileUpload Has No File";
}
}
}
Read detail here : https://forums.asp.net/t/1904302.aspx?FileUpload+Inside+a+Repeater+Can+t+Find+File

null reference exception on page redirect

I'm getting a null reference exception on singleKingButton.Checked = true; in the page load event. If I comment it out then I still get one on the next line setting the date. The odd thing is the page loads fine it's when I hit the submit button that I get the error. Thanks in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace garrettPenfieldUnit4
{
public partial class Default : System.Web.UI.Page
{
Reservation newReservation = new Reservation();
//Load events, automatically inserts todays date and selects the single king radio button
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
singleKingButton.Checked = true;
arrivalDateBox.Text = DateTime.Today.ToShortDateString();
}
//When submit button is clicked show the two confirmation labels.
protected void submitButton_Click(object sender, EventArgs e)
{
newReservation.ArrivalDate = Convert.ToDateTime(arrivalDateBox.Text);
newReservation.DepartureDate = Convert.ToDateTime(departureDateBox.Text);
newReservation.NoOfPeople = peopleDropDown.SelectedIndex;
newReservation.SpecialRequests = specialRequestsBox.Text;
newReservation.FirstName = firstNameBox.Text;
newReservation.LastName = lastNameBox.Text;
newReservation.Email = addressBox.Text;
newReservation.Phone = telephoneNumberBox.Text;
newReservation.PreferredMethod = contactDropDown.SelectedIndex.ToString();
if (singleKingButton.Checked)
{
newReservation.BedType = "Single King";
}
if (twoQueensButton.Checked)
{
newReservation.BedType = "Two Queens";
}
if (singleQueenButton.Checked)
{
newReservation.BedType = "Single Queen";
}
Session["Reservation"] = newReservation;
Response.Redirect("~/ConfirmationPage.aspx");
}
//When the clear button is clicked clear the form and reset fields to their default values
protected void clearButton_Click(object sender, EventArgs e)
{
arrivalDateBox.Text = DateTime.Today.ToShortDateString();
departureDateBox.Text = String.Empty;
peopleDropDown.SelectedIndex = 0;
singleKingButton.Checked = true;
twoQueensButton.Checked = false;
singleQueenButton.Checked = false;
specialRequestsBox.Text = String.Empty;
firstNameBox.Text = String.Empty;
lastNameBox.Text = String.Empty;
addressBox.Text = String.Empty;
telephoneNumberBox.Text = String.Empty;
contactDropDown.SelectedIndex = 0;
}
}
}
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="garrettPenfieldUnit4.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Royal Inn and Suites</title>
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/Main.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
</head>
<body>
<form id="form1" runat="server" defaultbutton="submitButton" defaultfocus="arrivalDateBox">
<div class="container">
<div id="page-header">
<h1 style="color: blue;">Royal Inn and Suites</h1>
<p style="color: red; font-style: italic;">
Where you're always treated like royalty
</div>
<h3 style="color: blue;">Reservation Request</h3>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
</div>
<div id="form-group">
<h4>Request Data</h4>
<div>
Arrival Date<asp:RequiredFieldValidator ID="arrivalDateBoxValidator" runat="server" ErrorMessage="Arrival date is required"
ControlToValidate="arrivalDateBox" ForeColor="Red" Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="arrivalDateValidator" runat="server" ErrorMessage="Arrival date must be before departure date."
ControlToValidate="arrivalDateBox" ControlToCompare="departureDateBox" Display="Dynamic"
Operator="DataTypeCheck" Type="Date" ForeColor="red">*</asp:CompareValidator>
</div>
<div>
<asp:TextBox ID="arrivalDateBox" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div>Departure Date</div>
<div>
<asp:TextBox ID="departureDateBox" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="departureDateBoxValidator" runat="server" ErrorMessage="Departure date is required"
ControlToValidate="departureDateBox" ForeColor="Red" Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="departureDateCompareValidator" runat="server" ErrorMessage="Departure date must be after arrival date"
ControlToValidate="departureDateBox" ForeColor="Red" ControlToCompare="arrivalDateBox" Display="Dynamic" Operator="GreaterThan"
Type="Date">*</asp:CompareValidator>
</div>
<div>Number of People</div>
<div>
<asp:DropDownList ID="peopleDropDown" runat="server" CssClass="form-control">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<div>
Bed Type
<asp:RadioButton ID="singleKingButton" runat="server" Text="King" GroupName="bedType" />
<asp:RadioButton ID="twoQueensButton" runat="server" Text="Two Queens" GroupName="bedType" />
<asp:RadioButton ID="singleQueenButton" runat="server" Text="Single Queen" GroupName="bedType" />
</div>
</div>
<h3>Special Requests</h3>
<div>
<asp:TextBox ID="specialRequestsBox" runat="server" TextMode="MultiLine" CssClass="form-control"></asp:TextBox>
</div>
<h3>Contact Information</h3>
<div>First Name</div>
<asp:TextBox ID="firstNameBox" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="firstNameBoxValidator" runat="server" ErrorMessage="First name is required"
ControlToValidate="firstNameBox" ForeColor="Red">*</asp:RequiredFieldValidator>
<div>Last Name</div>
<asp:TextBox ID="lastNameBox" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="lastNameBoxValidator" runat="server" ErrorMessage="Last name is required"
ControlToValidate="lastNameBox" ForeColor="Red">*</asp:RequiredFieldValidator>
<div>E-mail Address</div>
<asp:TextBox ID="addressBox" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="addressBoxValidator" runat="server" ErrorMessage="Email address is required"
ControlToValidate="addressBox" ForeColor="Red" Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="addressBoxExpressionValidator1" runat="server" ErrorMessage="Invalid Email"
ControlToValidate="addressBox" ForeColor="Red" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>Telephone Number</div>
<asp:TextBox ID="telephoneNumberBox" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="telephoneNumberBoxValidator" runat="server" ErrorMessage="Telephone Number is required"
ControlToValidate="telephoneNumberBox" ForeColor="Red" Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="telephoneExpressionValidator" runat="server"
ErrorMessage="Invalid phone number" ControlToValidate="telephoneNumberBox" ForeColor="red" Display="Dynamic" ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">*</asp:RegularExpressionValidator>
<div>Preferred Method of Contact</div>
<asp:DropDownList ID="contactDropDown" runat="server" CssClass="form-control">
<asp:ListItem>E-mail</asp:ListItem>
<asp:ListItem>Telephone</asp:ListItem>
</asp:DropDownList>
<div>
<asp:Button ID="submitButton" class="btn btn-primary btn-space" runat="server" Text="Submit" OnClick="submitButton_Click" />
<asp:Button ID="clearButton" class="btn btn-primary btn-space" runat="server" Text="Clear" OnClick="clearButton_Click" BackColor="#33CC33" />
</div>
<div>
<asp:Label ID="submitLabel1" runat="server" Text="Thank you for your Request." Visible="False" ForeColor="Green"></asp:Label>
</div>
<div>
<asp:Label ID="submitLabel2" runat="server" EnableViewState="False" Text="We will get back to you within 24 hours." Visible="False" ForeColor="Green"></asp:Label>
</div>
</div>
</form>
</body>
</html>
One of these all properties is receiving 'NULL' and at the time of inserting this values in database your table structure is not allowing 'NULL'.
So make sure that if you want to store 'NULL' in table then allow NULL in table structure (Table design).
You can check it using "line by line execution" of your code (using break point).

multiple forms run at server make visible to client asp.net

i have made registeration form, forgot password and login form in a single aspx page . I am using css to style the whole form. I am using 3 form tags. Here is code:
ASMX
<form id="form1" runat="server" visible="true">
<ul>
<li>
<asp:TextBox ID="email" runat="server" TextMode="email" placeholder="Email" OnTextChanged="email_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="email" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*Requied</asp:RequiredFieldValidator>
</li>
<li>
<asp:TextBox ID="password" runat="server" TextMode="Password" placeholder="Password" OnTextChanged="password_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="password" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*Requied</asp:RequiredFieldValidator>
</li>
<li>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</li>
<li>
<asp:Label ID="Invalid" runat="server" Text="Invalid Username or Password" Visible="False" ></asp:Label>
<asp:Label ID="Try" runat="server" Text="You can't try again ,Register Your Self" Visible="False"></asp:Label>
</li>
</ul>
</form>
<form runat="server" id="form2" visible="false">
<ul>
<li>
<asp:TextBox ID="username" runat="server" MaxLength="15" placeholder="UserName" OnTextChanged="username_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="username" ErrorMessage="RequiredFieldValidator" ForeColor="Red">*Please Input user name</asp:RequiredFieldValidator>
<asp:Label ID="userchk" runat="server" ForeColor="Red"></asp:Label>
</li>
<li>
<asp:TextBox ID="EmailR" runat="server" placeholder="Email (must be valid)" OnTextChanged="EmailR_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="EmailR" ErrorMessage="RequiredFieldValidator" ForeColor="Red">*Please Input email</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="EmailR" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="RegularExpressionValidator" ForeColor="Red">Input Valid Email</asp:RegularExpressionValidator>
</li>
<li>
<asp:TextBox ID="FirstName" runat="server" placeholder="FirstName" MaxLength="15" OnTextChanged="FirstName_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="FirstName" ErrorMessage="RequiredFieldValidator" ForeColor="Red">*Please Input first name</asp:RequiredFieldValidator>
<asp:Label ID="chkemail" runat="server" ForeColor="Red"></asp:Label>
</li>
<li>
<asp:TextBox ID="LastName" runat="server" placeholder="Lastname" MaxLength="15" OnTextChanged="TextBox3_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="LastName" ErrorMessage="RequiredFieldValidator" ForeColor="Red">*Please Input Last name</asp:RequiredFieldValidator>
</li>
<li>
<asp:TextBox ID="Pass" runat="server" placeholder="Password" TextMode="Password" MaxLength="15" OnTextChanged="Pass_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="Pass" Display="Dynamic" ErrorMessage="RequiredFieldValidator" ForeColor="Red">*Please Input password</asp:RequiredFieldValidator>
</li>
<li>
<asp:TextBox ID="Cpass" runat="server" placeholder="Confirm Password" TextMode="Password" MaxLength="15" OnTextChanged="Cpass_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="Cpass" ErrorMessage="RequiredFieldValidator" ForeColor="Red">*Please Enter the Confirm Password </asp:RequiredFieldValidator>
</ul>
</form>
<form runat="server" visible="false" id="form3">
<ul>
<li>
<asp:TextBox ID="SendEmail" runat="server" TextMode="Email" placeholder="Enter Email" OnTextChanged="SendEmail_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="SendEmail" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*Requied</asp:RequiredFieldValidator>
</li>
<li>
<asp:Button runat="server" text="Send email" OnClick="SendMail_Click" CssClass="button" Visible="true" CausesValidation="False"/>
</li>
<li>
<asp:Label ID="ml" runat="server" Text="Email sended Sucessfully" Visible="False"></asp:Label>
<asp:Label ID="ml0" runat="server" Text="Email Didn't Match " Visible="False"></asp:Label>
</li>
</ul>
</form>
I used following Js in toglling categories:
JS
<script>
$(document).ready(function () {
$("#list1").click(function () {
$("#form1").fadeIn("slow");
$("#form2").css("display", "none");
$("#form3").css("display", "none");
});
$("#list2").click(function () {
$("#form2").fadeIn("slow");
$("#form2").css("visible", "true");
$("#form1").css("display", "none");
$("#form3").css("display", "none");
});
$("#list3").click(function () {
$("#form3").fadeIn("slow");
$("#form3").css("visible", "true");
$("#form2").css("display", "none");
$("#form1").css("display", "none");
});
});
</script>
and here is my css FORMS
I have to use multiple forms in any case .. can anyone help please??

how to set RequiredFieldValidator to validate against specific user control which has multiple instance?

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.

Stop ModalPopup Extender to popup

I have a modal popup extender which fires on a button click ... now irrespective on the onclientclick function return value true/false its always poping up. I need to stop the modal to fire on return of false and modal to fire the return to true..how to do this?
Please find the code below :
<div style="text-align:center;" runat="server" id="pnlButton">
<asp:Button CssClass="button" ID="btnBack" runat="server" Text="Back"
Width="120px" onclick="btnBack_Click" UseSubmitBehavior="false" />
<asp:Button CssClass="button" ID="btnCancel" runat="server"
Text="Cancel Request" Width="130px" onclick="btnCancel_Click" onClientClick="Validate();" />
</div>
<div style="text-align:center;">
<asp:Button CssClass="button" ID="btnDone" Visible="false" runat="server"
Text="Done" Width="110px" onclick="btnDone_Click" />
</div>
<ajaxToolkit:ConfirmButtonExtender ID="cbe" runat="server"
TargetControlID="btnCancel"
DisplayModalPopupID="ModalPopupExtender1"
ConfirmText="Are you sure you want to click this?" />
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnCancel"
PopupControlID="PNL" OkControlID="ButtonOk" CancelControlID="ButtonCancel" BackgroundCssClass="modalBackground" />
<asp:Panel ID="PNL" runat="server" Style="display: none; width: 400px; background-color: White;
border-width: 2px; border-color: Black; border-style: solid; padding: 20px;">
<h2>Are you sure you want to cancel this request?</h2>
<br />
<br />
<div style="text-align: right;">
<asp:Button ID="ButtonOk" runat="server" Text="Yes" CssClass="button" />
<asp:Button ID="ButtonCancel" runat="server" Text="No" CssClass="button" />
</div>
</asp:Panel>
You can open Popup from javascript
function ValidateAndOpen(){
if(Validate()){
var modalDialog = $find("ModalPopupExtender1");
// get reference to modal popup using the AJAX api $find() function
if (modalDialog != null) {
modalDialog.show();
}
}
return false;
}
You must set ClientIDMode="Static" on ModalPopupExtender
and set OnClientClick="return ValidateAndOpen();"
change you code to the following
onClientClick="return Validate();"
Put this script onto your form:
function pageLoad(sender, args) {
if (args.get_isPartialLoad() === false) {
$find("<%= ModalPopupExtender1.ClientID %>").add_showing(function (sender, args) { args.set_cancel(Validate()); });
}
}

Categories

Resources