ASP.net ValidationSummary C# - c#

I have two questions that are in the form of radio button lists in C# web application. These questions have required field validators associated to them and I also have a ValidationSummary at the end of my web control. When I click on my "Submit" button, the two required field validations appear for each of the questions right next to it and the validationsummary works correctly by stating "Answer the following questions:" and then listing each of the questions that were not selected. The problem I'm having is when I select one of the two questions, the "Required Field" message disappears next to the question, but not in the ValidationSummary. How can I get the ValidationSummary to update or refresh when one of the questions in the error messages are selected? Please let me know if I need to be more specific. Thanks for your help!
<asp:RadioButtonList ID="Question1" RepeatLayout="Flow" runat="server">
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
Text="Required" ErrorMessage="Question 1"
Display="Dynamic" ControlToValidate="Question1"
EnableClientScript="true">
</asp:RequiredFieldValidator>
<asp:RadioButtonList ID="Question2" RepeatLayout="Flow" runat="server">
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
Text="Required" ErrorMessage="Question 2"
Display="Dynamic" ControlToValidate="Question2"
EnableClientScript="true">
</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Answer the following questions:"
DisplayMode="BulletList"
EnableClientScript="true"/>
<asp:Button ID="buttonSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_OnClick"/>
//Code behind for button
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
Response.BufferOutput = true;
Response.Redirect("~/Page.aspx");
}

I'm pretty sure the summary doesn't dynamically update by default, but rather only on form submit.
Ordinarily, you could get around this by hooking the client-side onblur event and calling ValidationSummaryOnSubmit(), but it looks like the RadioButtonList doesn't trigger this event when the selected radio button changes.
The next candidate would be the client-side onClick event, but the RadioButtonList control doesn't have an OnClientClick event.
So, the solution which worked for me when I tested it was to hook the client-side onClick event for each ListItem in the RadioButtonList. Like this:
<asp:RadioButtonList ID="Question1" RepeatLayout="Flow" runat="server">
<asp:ListItem Text="Yes" Value="Yes" onClick="ValidationSummaryOnSubmit()"></asp:ListItem>
<asp:ListItem Text="No" Value="No" onClick="ValidationSummaryOnSubmit()"></asp:ListItem>
</asp:RadioButtonList>
However, this will get rapidly ugly if you've got a bunch of these that you're going to want to do. Might consider subclassing RadioButtonList and adding a server-side addition of the ListItem onclick hook to each of the child listitems before sending it to the client, and then using your custom version of RadioButtonList instead of the stock version.

Related

How do I use RequiredValidator to validate a RadioButtonList in Repeater

We have a RadioButtonList control inside a Repeater control with two options, Yes or No.
We would like to force our users to choose a Yes or No before proceeding.
In other words, if a user clicks to submit or in our case, click the Next button to go to the next without selecting either Yes or No, an error needs to be raised letting the user know that s/he must select a Yes or No to continue.
The following code uses RequiredValidator control to handle this but it does not do anything.
Any ideas how to resolve this?
Purchased:<asp:radiobuttonlist ID="rblType" runat="server" ValidationGroup ="stype" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;">
<asp:ListItem Text="Yes" />
<asp:ListItem Text="No" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="rblType"
ErrorMessage="required"
ValidationGroup ="stype"
runat="server"/>
Thanks a lot in advance.

invisible controls in asp.net takes up space in asp.net

I have an ASP.NET web application. I have one TextBox (txtProductName) and one DropDownList(ddlChanges) visible at the time of page load. Also there is a TextBox(txtDeliveryStatus) and a Label (lblDeliveryStatus) invisible at page load.
<asp:TextBox ID="txtProductName" runat="server" CssClass="selectstyle" MaxLength="8" Width="60" />
<asp:Label ID="lblDeliveryStatus" runat="server" Style="visibility: hidden;">Delivery Status : </asp:Label>
<asp:TextBox ID="txtDeliveryStatus" runat="server" CssClass="selectstyle" Style="visibility: hidden;"/>
<asp:DropDownList runat="server" ID="ddlChanges" CssClass="selectstyle" onchange="javascript:ddlChangesChanged();">
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem>
<asp:ListItem Text="Status Change" Value="1"></asp:ListItem>
<asp:ListItem Text="Product Name Change" Value="2"></asp:ListItem>
<asp:ListItem Text="Category Change" Value="3"></asp:ListItem>
</asp:DropDownList>
I will make the Label and TextBoxfor delivery status visible when user selects Status Change from the DropDownList. But the problem is that the Label and TextBox for delivery status are invisible and still they take up some space on the page between visible controls.
How to remove that space of invisible controls?
the reason the white space is still showing after you control.Visible = false is because the "br" newLine tags are remaining. Wrap them with your button tag and hide both to truly remove the white space.
You should use the display attribute:
<asp:Label ID="lblDeliveryStatus" runat="server" Style="display: none;">Delivery Status : </asp:Label>
<asp:TextBox ID="txtDeliveryStatus" runat="server" CssClass="selectstyle" Style="display: none;"/>
For completeness you can check this link.
You can use visible attribute of control.
To hide:
Control.Visibile=false;
Whenever you want to display
Control.Visible=true;

Display a new link on the same page each time a new value is selected in dropdown list

I am creating a web application in ASP.NET C# , following is my issue
I have a Drop down list, the requirement is each time I select an item in the drop down list, a new button should be displayed on the same page. This new button redirects to different window each time user select different options from the drop down menu. I am trying to use script something like below but it wont refresh each time a new value from the drop down list is selected
<script runat="server">
protected void Button1_Click(Object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "BI_Config")
{
Button2.Style.Add("display", "inherit");
}
}
</script>
The code for dropdown list is as below
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="BI_Config"></asp:ListItem>
<asp:ListItem Value="GWI_Infostore"></asp:ListItem>
<asp:ListItem Value="Logging"></asp:ListItem>
<asp:ListItem Value="RawDatafeeds"></asp:ListItem>
<asp:ListItem Value="RawDataFeedsGWA"></asp:ListItem>
<asp:ListItem Value="Reporting"></asp:ListItem>
<asp:ListItem Value="Staging"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="View Reports" OnClick="Button1_Click" />
The code for button is as below
<asp:Button ID="Button2" runat="server" Text="Display Model Report" OnClientClick="window.open('Model Report.xml');return false;" style="display: none;" />
I want a different button to display each time
Any help on this is highly appreciated.

Hidden/Visible display depending on C# Dropdown selection

To be brief, I need the Textbox with ID="textComments" to only appear when the dropdown option of 'Other' is selected from the dropdown, and then disappear if another selection is made. I could do this with JS but it needs to be in C#.
<asp:DropDownList runat="server" ID="dropEnquiryType" CssClass="dropdownRequestList">
<asp:ListItem Value="Customer Services" Text="Customer Services"></asp:ListItem>
<asp:ListItem Value="Website" Text="Website"></asp:ListItem>
<asp:ListItem Value="Contract" Text="Contract Hire"></asp:ListItem>
<asp:ListItem Value="Other" Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:label runat="server" ID="lblComments" AssociatedControlID="textComments" CssClass="textLabel">Comments:</asp:label>
<asp:TextBox runat="server" MaxLength="200" TextMode="MultiLine" Columns="40" Rows="4" ID="textComments" Wrap="true" CssClass="textComments"></asp:TextBox>
And help would be greatly appreciated.
Make the DropDownList first AutoPostBack=true
handle it's SelectedIndexChanged-event
Switch visibility of both controls there
aspx:
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="dropEnquiryType_Changed" runat="server" ID="dropEnquiryType" CssClass="dropdownRequestList">
<asp:ListItem Value="Customer Services" Text="Customer Services"></asp:ListItem>
<asp:ListItem Value="Website" Text="Website"></asp:ListItem>
<asp:ListItem Value="Contract" Text="Contract Hire"></asp:ListItem>
<asp:ListItem Value="Other" Text="Other"></asp:ListItem>
</asp:DropDownList>
codebehind:
protected void dropEnquiryType_Changed(Object sender, EventArgs e)
{
lblComments.Visible = dropEnquiryType.SelectedValue == "Other";
textComments.Visible = lblComments.Visible;
}
If you absolutely have to do this within C#, then a simple check in PreRender should be sufficient:
textComments.Visible = (dropEnquiryType.SelectedValue == "Other");
This will also require you to set AutoPostback on dropEnquiryType, though, which uses ... JavaScript!

ASP.NET RequiredFieldValidator not working with DropDownList Firefox 6

Having an issue validating a dropdownlist with a requiredfieldvalidator in Firefox 6. The code I have works in all browsers, except Firefox 6.
Here is what happens, I select a value from the dropdownlist that is not the default value and click the button. An error message is returned, meaning validation failed even though I have selected a different field. If I do this again, and select the SAME value from the dropdownlist and click the button. No error message, it passes validation. This only fails the first time you select a value, and only in Firefox 6.
Another example, load up the page, select a value, select the same value again, click the button, and it passes. WTH?
<asp:dropdownlist id="ddlHour" cssclass="select select-small" runat="server">
<asp:listitem value="" text="" selected="true"></asp:listitem>
<asp:listitem value="1">1</asp:listitem>
<asp:listitem value="2">2</asp:listitem>
</asp:dropdownlist>
<asp:requiredfieldvalidator id="rfvHour" initialvalue="" controltovalidate="ddlHour" errormessage="Please select an hour" display="none" validationgroup="banquetForm" runat="server"/>
And I have a button with the same validation group later on in the form. Any help is appreciated, thanks.
Try using the following:
<asp:dropdownlist id="ddlHour" cssclass="select select-small" runat="server">
<asp:listitem value="0" Text="" selected="true"></asp:listitem>
<asp:listitem value="1">1</asp:listitem>
<asp:listitem value="2">2</asp:listitem>
</asp:dropdownlist>
<asp:RequiredFieldValidator ID="HourValidate" runat="server" ControlToValidate="ddlHour" ErrorMessage="Please select an hour" InitialValue="0"></asp:RequiredFieldValidator>
how to set required field validator with DropDownList
suppose you are having this items in your dropdownlist..
DropDownList1.Items.Add(new ListItem("--Select--","0"));
DropDownList1.Items.Add(new ListItem("Kaushal","1"));
DropDownList1.Items.Add(new ListItem("Naresh","2"));
DropDownList1.Items.Add(new ListItem("Pankaj", "3"));
Then -: set the requiredfieldvalidator's controltovalidate property to DropDownlist1 and set the InitialValue property of the requiredfieldvalidator to 0 as this will be the value for
--Select-- you can show above.
Correct me if I'm wrong, but I think you are using the wrong validator. By default, a dropdown always has a value, so making it required doesn't make sense. I think what you want to do is compare it, to make sure it's not the first choice. This page shows a quick demo of how to do that.
http://forums.asp.net/t/1188035.aspx/1

Categories

Resources