Change the ControlToValidate of RequiredFieldValidator dynamically - c#

I have a textbox and dropdown inside a <td> based on certain condition I'll have to hide the text box and show the dropdown. If dropdown is visible is it possible to use the same requiredfield Validator. Is this possible?
<td>
<asp:TextBox ID="txtLimit" runat="server" />
<asp:DropDownList ID="ddlCurLiabiltyLimits" runat="server" CssClass="TextNormal" OnSelectedIndexChanged="ddlCurLiabiltyLimits_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtLimit" ErrorMessage="Answer is required." ClientIDMode="Static" />
</td>
In my code behind :
RequiredFieldValidator1.ControlToValidate = ddlCurLiabiltyLimits.ID ;
but this doesn't seem to be working.

One thing that might be causing you problems is when using RequiredFieldValidator on a DropDownList, you will want to specify the RequiredFieldValidator.InitialValue property to indicate the Value of the "first" item in the DropDownList. Maybe that is where your problem is?

My guess is that you might be assigning the ID to the ControlToValidate property too late in the page lifecycle. Is it possible to assign this during the page's OnInit event?
It might be easier to just use a second RequiredFieldValidator for the DropDownList.

Related

ValidationGroup Not Firing

I have a text box with required field validator:-
<asp:TextBox ID="txtCName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCName" runat="server" ErrorMessage="Pleas Enter Name" ControlToValidate="txtCName" ></asp:RequiredFieldValidator>
I need to show error message on button click:-
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
ValidationGroup="sub">
I am using the following code to assign validation group to the required field validator (it is in document.ready() function) :-
$("#<%=rfvCName.ClientID %>").attr('ValidationGroup', 'sub');
But it is not showing any error message on button click. Please help me.
I need to do it using Jquery. I don't want to assign ValidatioGroup directly to the control.
Add validation group ValidationGroup="Sub"
<asp:RequiredFieldValidator ID="rfvCName" runat="server" ErrorMessage="Pleas Enter Name" ValidationGroup="sub" ControlToValidate="txtCName" ></asp:RequiredFieldValidator>
when the page(server side) prerender the object requiredFieldValidator, some javascript is added into page automatically by .net framework.
this javascrript code check the validationgroup.
add manually the validationgroup attribute client-side without this javascript code not work.
you can see this code in developpers tool searching "ValidationGroup"
if you cannot add attribute in html, you can add this attribute in a event server side like load or prerender.

comparevalidation for text box value and hidden field value

<asp:TextBox ID="txtAppSanctionLimit" runat="server" onblur="calcCustDebtEquity()"> </TextBox>
<asp:HiddenField ID="hfAppReqeustAmt" runat="server" Value="0" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Proposed Limit is never greater than Request Limit" ControlToCompare="txtAppSanctionLimit" ControlToValidate="txtRequestLimits" Operator="GreaterThan"
ValidationGroup="Report3" Display="none" ></asp:CompareValidator>
<asp:Button ID="btnLimtUpdate" runat="server" Text="Update" ValidationGroup="Report3"
onclick="btnLimtUpdate_Click"/>
<asp:ValidationSummary ID="ValidationSummary3" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="Report3" />
Above all has been used in my defalt.aspx page under Visual Studio 2010.
I want to show Validation Message if txtAppSanctionLimit value is greater than hfAppReqeustAmt value. note that hfAppReqeustAmt value get from database with query.
how can I solve this problem.
The accepted answer is completely incorrect!
Simply put you can't use a CompareValidator with a HiddenField. You need to use a CustomValidator.
See: asp:RequiredFieldValidator does not validate hidden fields
The attribute for the target
ControlToCompare="txtAppSanctionLimit"
should probably be pointed at your hidden field.
ControlToCompare="hfAppReqeustAmt"
and the attribute for the ControlToValidate should be pointed to your user data entry field.
ControlToValidate="txtAppSanctionLimit"
as it is currently pointing to a control that is not shown in the example code.
EDIT: Per MSDN for the CompareValidator.ControlToCompare -
If the control to compare is hidden or is inside a container (such as
a Panel control) that is not visible, the validator performs
server-side validation only. The validator client script supports only
visible controls.

ASP C# RequiredFieldValidator

[textbox1]
[textbox2]
[button1] [linkbutton1]
i have 2 textboxes, 2 RequiredFieldValidator, button1 and linkbutton.
the button1 and 2textboxes are working properly with RequiredFieldValidator, but if i click the linkbutton the RequiredFieldValidator also appears.
I want the RequiredFieldValidator focus only in button1. Is this possible?
Set CausesValidation="false" in your link button markup like this
<asp:LinkButton ID="lnkButton" runat="server" CausesValidation="false"></asp:LinkButton>
you need to specify Validation Groups
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>
Use ValidationGroup property of RequiredFieldValidator.
Set the ValidataionGroup="someName" for your textbox1-requiredfieldvalidator, textbox2- requiredfieldvalidator and your button1 control.
Note: Keep the name of the ValidationGroup same.

Validation effects the entire form, no button can be pressed

I've got a page that contains a huge amount of controls, (gridviews, reportviewers, buttons etc.)
Now I've a couple of textboxes and dropdowns that represent a "form" user might want to fill up and submit by clicking a button. Some of these controls contain "RequiredFieldValidator"s.
<fieldset runat="server" >
<asp:Label ID="lblName" runat="server" Text="Name:" AssociatedControlID="txtName" />
<asp:RequiredFieldValidator ID="rfvTxtName" runat="server" ControlToValidate="txtName" Text="(Required)" />
<asp:TextBox ID="txtName" Width="175px" runat="server" />
....
....
</fieldset>
The problem I encounter is that no other button on the entire asp form can be clicked because of these validation controls.
The question is; how do I set this validation to only listen to that particular fieldset and not the whole page?
Set the ValidationGroup property of those controls to something unique.
More info here.
Set your controll's ValidationGroup property to make them one group . Like
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
ValidationGroup="MyGroup" />
The ValidationGroup property specifies which group of controls is
validated on validation. This property is mostly used when there are
several buttons in a form.
ASP.NET ValidationGroup Property
You ca also change property CausesValidation to False for control that you don't want to validate.

Asp:Label is not shown when visible is set to true?

I have a simple web form which has a couple list boxes and a search button. When the button is clicked, it returns a DataSet. If the dataset contains records, I set the asp:label which is initially set to false to true, but this is not happening. If the dataset has records and the visible property is set to true, the label still does not show up.
I have also tried putting the label and a couple other controls in an html table and setting a runat="server" attribute on the table and changing the visibility on that, but it does not show either.
Here is aspx code:
<table>
<tr>
<td>
<asp:Label ID="lblSortBy" runat="server" Text="Sort By:" Visible="false">
</asp:Label>
<asp:DropDownList
ID="ddlSortBy"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlSortBy_SelectedIndexChanged">
<asp:ListItem Value="Gross">Gross</asp:ListItem>
<asp:ListItem Value="Population">Population</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
Here is simplified code behind when a button is clicked:
public void GetData()
{
DataView dv = GetReportData().DefaultView;
if(dv.ToTable().Rows.Count > 0)
{
lblSortBy.Visible = true;
}
else
{
lblSortBy.Visible = false;
}
}
I have a couple Update Panels around some ListBoxes and a GridView, but not the Label and Dropdown. Would this cause an issue?
I did a test, I set a label that was in an update panel to false if records were found and the label disappeared, so it is working if it is in an update panel.
If I'm not mistaken, your label should exist on an updatepanel, because as far as the static HTML page is concerned, the one and only time that your current label exists, it's set to be not visible. You would have to reload the whole page to make it visible again.
If the button is inside an UpdatePanel, then the Table, Label, etc. also have to be inside an UpdatePanel to get updated. Otherwise only the contents of the UpdatePanel get updated when clicking the button (this is what's called partial page-rendering).
So if the button is in an UpdatePanel, you have two possibilities to solve the problem:
put the table, Label, DropDownList etc. into the same UpdatePanel
or put them in another UpdatePanel and set the UpdateMode of that property to Always, so that it gets updated, even if a Postback was initiated by a control within another UpdatePanel.
See this page in MSDN for details.
You just need runat="server" on the label itself; though Visible should default to True.
Make sure you add a ForeColor to avoid mixing it in w/ background.
Debug to ensure your label has content and it's not in another control whose Visible=False.
If the table is changing visible and is the parent container of the label I don't believe it is necessary to change the label's visibility at all as it should always be set to visible.
I am assuming that you are gonna hide the ddl as well if there is no data. Have you tried putting a panel around both of them and setting its visibility to true
if you are returning rows and your button is in an updatepanel, then is your label and ddl in that updatepanel as well
thanks its really useful, put Lable in a update panel.
<ContentTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="LinkNM" runat="server" Text="Learn>" BackColor="Transparent" style=" color: #6699FF;text-decoration-color:none;border:none;font-size:x-large" OnClick="LinkNM_Click"/>
<asp:Label ID="lblChapterName" runat="server" BackColor="Transparent" style=" color: #6699FF;text-decoration-color:none;border:none;font-size:x-large" ></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnFileUpload" />
</Triggers>
</asp:UpdatePanel>

Categories

Resources