I have ReCaptcha control on my registration form:
<recaptcha:RecaptchaControl ID="ReCaptcha" runat="server" PublicKey="<%$ appSettings:ReCaptchaPublicKey %>" PrivateKey="<%$ appSettings:ReCaptchaPrivateKey %>" Theme="white" />
<asp:CustomValidator ID="ReCaptchaCustomValidator" runat="server" ErrorMessage="ReCaptcha error message." Display="None" ValidationGroup="Step1" EnableClientScript="true" OnServerValidate="ReCaptcha_ServerValidate" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" ValidationGroup="Step1" CssClass="validationSummary" />
CodeBehind
protected void ReCaptcha_ServerValidate(object source, ServerValidateEventArgs e)
{
ReCaptcha.Validate();
if (ReCaptcha.IsValid)
e.IsValid = true;
else
e.IsValid = false;
}
If user enter ReCaptcha value not properly, ReCaptcha control disappear after server-side validation.
How can I reload it after validation?
protected void Page_PreRender(object sender, EventArgs e)
{
CaptchaControl1.Enabled = true;
}
Related
I want to enable and disable textfields on checked change event but this event is not firing.
Following is my radio button code
<asp:RadioButton ID="submitter" runat="server" GroupName="rd2" OnCheckedChanged="submitter_CheckedChanged" Text="Submitter" />
<asp:RadioButton ID="following" runat="server" ForeColor="Black" Text="Following" GroupName="rd2" OnCheckedChanged="following_CheckedChanged" />
Following is my checked change event code
protected void following_CheckedChanged(object sender, EventArgs e)
{
if (submitter.Checked == true)
{
contactName.Enabled = false;
Contactmail.Enabled = false;
Response.Write("<script LANGUAGE='JavaScript' >alert('following event submitter check')</script>");
}
if (following.Checked == true)
{
contactName.Enabled = true;
Contactmail.Enabled = true;
Response.Write("<script LANGUAGE='JavaScript' >alert('following event following check')</script>");
}
}
protected void submitter_CheckedChanged(object sender, EventArgs e)
{
if (submitter.Checked == true)
{
contactName.Enabled = false;
Contactmail.Enabled = false;
Response.Write("<script LANGUAGE='JavaScript' >alert('submitter event submitter check')</script>");
}
if (following.Checked == true)
{
contactName.Enabled = true;
Contactmail.Enabled = true;
Response.Write("<script LANGUAGE='JavaScript' >alert('submitter event following check')</script>");
}
Please tell me where is problem. thanks
set AutoPostBack="true"
Postback means sending Data to server. It will execute server side events.
for more Details Click Here
<asp:RadioButton ID="submitter" runat="server" GroupName="rd2" OnCheckedChanged="submitter_CheckedChanged" Text="Submitter" AutoPostBack="true" />
<asp:RadioButton ID="following" runat="server" ForeColor="Black" Text="Following" GroupName="rd2" OnCheckedChanged="following_CheckedChanged" AutoPostBack="true" />
You need to add AutoPostBack="true".
Change the aspx code to as below:
<asp:RadioButton AutoPostBack="true" ID="submitter" runat="server" GroupName="rd2" OnCheckedChanged="submitter_CheckedChanged" Text="Submitter" />
<asp:RadioButton AutoPostBack="true" ID="following" runat="server" ForeColor="Black" Text="Following" GroupName="rd2" OnCheckedChanged="following_CheckedChanged" />
I have this code
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
RadioButton2.Checked = false;
DropDownList1.Enabled = true;
}
if (!RadioButton1.Checked)
{
RadioButton2.Checked = true;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
RadioButton1.Checked = false;
DropDownList1.Enabled = false;
}
if (!RadioButton1.Checked)
{
RadioButton1.Checked = true;
}
}
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="BookStore" DataTextField="Name" DataValueField="Name" Height="51px" Width="300px" DataMember="DefaultView">
</asp:DropDownList>
<asp:SqlDataSource ID="BookStore" runat="server" ConnectionString="<%$ ConnectionStrings:BookStoreConnectionString %>" SelectCommand="SELECT [Name] FROM [Books] ORDER BY [Name]"></asp:SqlDataSource>
<p>
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="1" OnCheckedChanged="RadioButton1_CheckedChanged" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="1" OnCheckedChanged="RadioButton2_CheckedChanged" />
</p>
But when I click radiobuttons, dropdownlist is alwais enabled. When I write this functions in webforms app(not asp.net) its working correct.What could be the reason of this uncorrect working?
May be problem in RadioButton2_CheckedChanged event in second if condition, this should check RadioButton2.Checked not RadioButton1.Checked. instead of checking this condition if you put else still this will work no need to check again.
if (!RadioButton2.Checked)
{
RadioButton1.Checked = true;
}
I have enabled required field validator for txtProjectDate textbox, which is not allowing the calendar to be poped-up after clicking the button for calendar.So, i cannot select values to be entered in to the textbox.
ERROR:Mandatory Field
ASPX FILE
<tr>
<td>Project Started at</td>
<td>::</td>
<td><asp:TextBox ID="txtProjectDate" runat="server" OnTextChanged="txtProjectDate_TextChanged1"></asp:TextBox>
<asp:ImageButton ID="btnShowCalendar" runat="server" OnClick="btnShowCalendar_Click" ImageUrl="D:\calendar4.png" />
<asp:Calendar ID="calProjectDate" runat="server" OnSelectionChanged="calProjectDate_SelectionChanged" Visible="False"></asp:Calendar>
<asp:RequiredFieldValidator ID="rfvProjectDate" runat="server" ControlToValidate="txtProjectDate" ErrorMessage="MANDATORY"></asp:RequiredFieldValidator>
</td>
</tr>
ASPX.CS CODES FOR textbox,calendar,button
protected void btnShowCalendar_Click(object sender, ImageClickEventArgs e)
{
calProjectDate.Visible = true;
}
protected void calDateofBirth_SelectionChanged(object sender, EventArgs e)
{
txtProjectDate.Text = calProjectDate.SelectedDate.ToShortDateString();
calProjectDate.Visible = false;
}
protected void txtProjectDate_TextChanged(object sender, EventArgs e)
{
}
protected void calProjectDate_SelectionChanged(object sender, EventArgs e)
{
txtProjectDate.Text =
calProjectDate.SelectedDate.ToShortDateString();
calProjectDate.Visible = false;
}
Pls help me to find out the solution...
Simply set CausesValidation attribute to false in ImageButton:-
<asp:ImageButton ID="btnShowCalendar" runat="server" CausesValidation="false"
OnClick="btnShowCalendar_Click" ImageUrl="D:\calendar4.png" />
I have this form with a custom validator, and Button.
But, my custom Validator shows error only after button click.
This is my validator, Button and code behind.
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="User Name cannot be Empty!" ForeColor="Red"
onservervalidate="CustomValidator1_ServerValidate"
ControlToValidate="userNameTxt" ValidateEmptyText="True"
ValidationGroup="save-valid"></asp:CustomValidator>
<asp:Button ID="saveButton" runat="server" Text="Save" CssClass="save-button"
onclick="saveButton_Click" TabIndex="7" ValidationGroup="save-valid" />
This is my code behind.
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (IsUserValid(userNameTxt.Text))
{
CustomValidator1.ErrorMessage = "User Name cannot be Empty!";
args.IsValid = false;
}
else
args.IsValid = true;
}
protected void saveButton_Click(object sender, EventArgs e)
{
//This code executes regardless of CUstom Validator
}
You can try to use client side validation.
<script type="text/jscript">
function textBoxControl(source,arguments){
if(arguments.Value.length >0) //or control things (e.g. at least 6 character)
arguments.isValid = true;
else
arguments.isValid = false;
}
<script>
Code behind is like below.
<asp:TextBox ID="userNameTxt" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1"
runat="server" ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate"
ControlToValidate="userNameTxt"
ClientValidationFunction="textBoxControl"></asp:CustomValidator>
When I put the below code inside Save Button click, the problem gets solved.
if (!Page.IsValid)
return;
I have used more than one Button in a Master page. Also there is a button in my login page. The problem is while submitting the content of login page with login button , it works but when in login page i clik Home button(Master page buttons) , it doesn't work, it works as a submitting button .
My Master page code :
<body style="margin:0px;">
<form id="form1" runat="server">
<div>
<div class="auto-style1" style="background-color: #3399FF; height: 42px;">
<asp:Button ID="homeButton" runat="server" CssClass="auto-style2" Text="Home" Width="126px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="homeButton_Click" />
<asp:Button ID="newsButton" runat="server" CssClass="auto-style3" Text="News" Width="127px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="newsButton_Click" />
<asp:Button runat="server" CssClass="auto-style4" Text="Shared Files" Width="123px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="Unnamed1_Click" />
<asp:Button ID="memberButton" runat="server" CssClass="auto-style5" Text="Members" Width="117px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="memberButton_Click" />
<asp:Button ID="blogButton" runat="server" CssClass="auto-style6" Text="Blogs" Width="103px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="blogButton_Click" />
<asp:Button ID="loginButton" runat="server" BackColor="#3366FF" BorderStyle="None" CssClass="auto-style8" Height="42px" Text="Log in" Width="82px" OnClick="loginButton_Click" />
<asp:Button ID="Button1" runat="server" BackColor="#3366FF" BorderStyle="None" CssClass="auto-style9" Height="42px" Text="Register" Width="96px" OnClick="Button1_Click" />
</div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
And My Master Page code Behind is :
protected void homeButton_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("Home.aspx");
}
protected void newsButton_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("News.aspx");
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("Shared_Files.aspx");
}
protected void memberButton_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("Members.aspx");
}
protected void blogButton_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("Blogs.aspx");
}
protected void loginButton_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("Login.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.RedirectPermanent("Register.aspx");
}
Menu Control in Master page is advisable instead of using button control for Navigation between the pages
You problem in body of ClickEventHandlers. You use RedirectPermanent method.
From msdn:
The RedirectPermanent(String) method overload provides a 301 HTTP status code in the response and includes the URL to redirect the request to. A 301 HTTP status code is a standard code in an HTTP response. It indicates that a permanent redirection exists, and it provides the redirection location.
Calling the RedirectPermanent(String) method overload terminates the response.
So you need use simple Redirect instead