Refresh RadioButtonList in asp.net - c#

I got this code for my RadioButtonList:
<div style="margin-top:5px"><asp:RadioButtonList ID="RadioButtonList6" RepeatColumns = "2" RepeatDirection="Vertical" RepeatLayout="Table" runat="server">
<asp:ListItem ValidationGroup="Curriculum" style="margin-right:12px; margin-top:-10px" >Si</asp:ListItem>
<asp:ListItem ValidationGroup="Curriculum" >No</asp:ListItem></asp:RadioButtonList></div>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1"
ControlToValidate="RadioButtonList6" Text="Elige una opción"
ValidationGroup="Curriculum">
</asp:RequiredFieldValidator>
It works just fine, problem is, when go to another page, if i click back, Radio Buttons keep selected, i need them to step back or 'refresh' when i go back to the form, just to be clear, my form is a 9 pages one, so when user clicks finished it goes to another page, but if user wants to go back, it appears to be still selected.
How can i solve this?
I hope i've explained myself, thanks in advance!

you can use
RadioButtonList.SelectedIndex=-1
properties of radiobutton list is listed below
use this link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.aspx

Related

ASP.NET Button Click is not working on first click when Textbox AutoPostBack is True

I have three textbox and one button. Here is my problem:
I use autopostback="true" in textbox, i entered some value and after first click of the button is not working. Then i click again, second click is working. I looked on F12 developer tool on Browser, there is no an error. When i set autopostback="false" than button click is working without any problem.
I would like to use autopostback="true" because it helps me. I think there is a focus problem because when i click out of textbox and than click on the button, button works with autopostback="true"
Do you have any idea for solution of this problem. All controls are working server-side.
<asp:Textbox ID="Name" runat="server" ValidationGroup="myval" AutoPostBack="true"></asp:Textbox>
<asp:Textbox ID="Surname" runat="server" ValidationGroup="myval" AutoPostBack="true"></asp:Textbox>
<asp:Textbox ID="City" runat="server" ValidationGroup="myval" AutoPostBack="true"></asp:Textbox>
<asp:Button ID="send_btn" runat="server Text="Check and Send" OnClick="send_btn_Click" ValidationGroup="myval"></asp:Button>
I found a solution. It's not stable solution but however it's working. I hope it helps you too. My solution is:
<asp:Button ID="send_btn" runat="server Text="Check and Send" OnClick="send_btn_Click" OnClientClick="return true;" ValidationGroup="myval"></asp:Button>
I added OnClientClick="return true;" and button click is working on first click now.
If you have better solution waiting for your response.

Page scrolling on selecting value from drop down menu

i have successfully completed my web application for an organization and it works up to expectations but i noticed one problem and tried to cure it via searching on Google and asking seniors but none of these helped much.
Problem:
I have multiple drop downs lists on page, in which selecting value in one drop down triggers the loading of another drop down i.e. Country > cities situation, problem is that whenever i click any value it scrolls page to the top and i have to scroll back again then again and again which realy looks bad and unprofessional. Please help me.
Code:
<asp:UpdatePanel ID="updGridViewSMS" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<br />
<asp:Panel ID="pnlBoxesDropDowns" runat="server">
<label style="width:400px">Relevant Region</label>
<asp:DropDownList ID="ddlRegions" runat="server" CssClass="DropDown_Width" Width="147px" OnSelectedIndexChanged="ddlRegions_SelectedIndexChanged" AppendDataBoundItems="True" AutoPostBack="true" >
<asp:ListItem Value="-1" Selected="True">-Select-</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqFieldValidatorRegions" runat="server"
ControlToValidate="ddlRegions" ErrorMessage="Region is Required" InitialValue="-1"
ForeColor="Red" ValidationGroup="Complaints">Region is Required</asp:RequiredFieldValidator>
<label style="width:400px">Relevant District</label>
<asp:DropDownList ID="ddlDistricts" runat="server" CssClass="DropDown_Width" Width="147px" OnSelectedIndexChanged="ddlDistricts_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqFieldValidatorDistricts" runat="server"
ControlToValidate="ddlDistricts" ErrorMessage="Region is Required" InitialValue="-1"
ForeColor="Red" ValidationGroup="Complaints">District is Required</asp:RequiredFieldValidator>
<label>Relevant P.Station</label>
<asp:DropDownList ID="ddlPoliceStations" runat="server" Width="147px" CssClass="DropDown_Width">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqFieldValidatorPoliceStations" runat="server"
ControlToValidate="ddlPoliceStations" ErrorMessage="Police Station is Required" InitialValue="-1"
ForeColor="Red" ValidationGroup="Complaints">Police Station is Required</asp:RequiredFieldValidator>
<label>Priority</label>
<asp:DropDownList ID="ddlPriority" runat="server">
<asp:ListItem Text="Top" Value="1"></asp:ListItem>
<asp:ListItem Text="Normal" Value="2"></asp:ListItem>
</asp:DropDownList>
</asp:Panel>
<br />
<br />
<asp:Timer runat="server" Interval="60000" ID="RefreshSmsComplaints" OnTick="RefreshSmsComplaints_Tick" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RefreshSmsComplaints" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
help please.
The core issue here is that you are posting back to the server on every dropdown select. This means that the web page reloads, because it is your webserver that updates the HTML versus the more modern way to do things which is to have javascript do it on the client. Here is a hacky, imperfect, but quick solution:
In the browser, inspect your html DOM. Each of your dropdown ASPX tags should have been replaced with something along the lines of
<select id="ctl000$ddlDistricts"...
You can use url hashtags to scroll to that area by appending #ctl000$ddlDistricts to the url. Since the server knows which control was posted, it can get the control's client-side ID, and write it into the following javascript (preferably at the bottom of the page).
<script type="text/javascript">
location.href='#' + <%=PostedControl.ClientID %>
</script>
Why is this imperfect? Because it will scroll your page to a point where the dropdown is at the top edge of the browser. This means that if a user selects the dropdown with it halfway down the page, once the page reloads, that will be at the top. At the very least, however, your dropdowns will then be within view.
Your original question indicated that you wanted the page to look "professional" and while this gets the job done, it is imperfect, as will all solutions be when you are dealing with classic ASPX webforms-style postbacks. Specifically, your dropdowns have something that looks like this:
OnSelectedIndexChanged=MyFunction
What this tells the ASPX form is, in essence, "when you render this dropdown control into HTML, add something into the tag like onChange="postMyFormBackToServerCausingReloadAndPageScroll. What you really want, perhaps, is to have it do onChange=GetNewDropdownFromServerAndReplaceOnPageWithoutReload. That involves more knowledge than I can add to an already long answer, but here are some helpful links:
http://www.codeproject.com/Articles/691298/Creating-AJAX-enabled-web-forms (a bit old but probably a good stepping stone)
http://www.codedigest.com/Articles/jQuery/318_Doing_AJAX_with_jQuery_in_ASPNet.aspx (a slightly more modern way to do it, but you do more yourself with this method as opposed to relying on .NET)
Lastly, the fully modern way to do things that involves completely rewriting your page
http://www.codeproject.com/Articles/575397/An-Absolute-Beginners-Tutorial-on-ASP-NET-MVC-for (not sure if this is the right article for you, just google for ".NET MVC")

ASP Drop Down collapses after first click

I have an ASP Drop Down List that opens then immediately collapses on one click. I'm using a Drop Down on another page with the same CssClass, and it's working fine (first click expands the DDL and the second click collapses it). Anyone have any idea what could be happening? I would greatly appreciate any suggestions or input.
<label>
<asp:RadioButton ID="rbToAccount" runat="server" GroupName="rbTo" />
Account
<asp:DropDownList ID="ddlToAccount" runat="server" CssClass="span4 m-wrap">
</asp:DropDownList>
</label>
Try This :
<label></label>
<asp:RadioButton ID="rbToAccount" runat="server" GroupName="rbTo" />
Account
<asp:DropDownList ID="ddlToAccount" runat="server" CssClass="span4 m-wrap">
</asp:DropDownList>
is the event tied to a Jquery function??
It sounds like on dropdown list change it is causing a postback, which then is refreshing the page to it's original state. Try adding OnClientClick="return false" as an attribute to the ddl control

How to align a control side-to-side?

I'm making dynamic radio buttons by using ASP.NET. Where there a validation control that I want to align side of this radios buttons.
Code:
<asp:RadioButtonList
ID="tiposeg"
runat="server"
RepeatColumns="3">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b/asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator15"
ControlToValidate="tiposeg"
ErrorMessage='<% # foo.ErrorMessages.EmptyField %>'
runat="server"
/>
The problem is: if making by using <asp:RadioButtonList, the <asp:RequiredFieldValidator is displayed into next line, unlike if I do it by using "HTML pure", that is displayed side of control (without need to use CSS). I hope this is clear. Thanks in adavnce.
EDIT
in other words, it's possible put the <span> generated by asp:RequiredFieldValidator inside <table> generated by <asp:RadioButtonList?
EDIT 2
My current ASP.NET code;
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
RepeatColumns="3">
<asp:ListItem>abcd</asp:ListItem>
<asp:ListItem>xyz</asp:ListItem>
</asp:RadioButtonList>
</li>
<li style="display:inline">
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="RadioButtonList1"
ErrorMessage="select atleast one radiobutton!"
/>
The form:
What I'm getting:
What I'm expecting:
Do you really want to arrange the radio buttons in a grid with multiple rows and columns? If not, delete the RepeatColumns attribute and add RepeatLayout="Flow" and RepeatDirection="Horizontal":
<asp:RadioButtonList runat="server" ID="RadioButtonList1"
RepeatLayout="Flow" RepeatDirection="Horizontal">
This will render the radio buttons without a containing <table>, so the validator will appear to the right of the last radio button.
Could you achieve what you want by putting the controls in DIVs and floating them both left?

CascadingDropDown in TabContainer and Target in other Tab

I have divised a page with a TabContainer.
Before, this page worked with a CascadingDropDown that populate a DropDownList.
Now this DropDownList is in another tab. So I catch an exception "Object Reference Null" when I try to see this page.
How did runs to load a DropDownList in another Tab when I change a value in a different Tab ?
Update:
Thanks for your answer, but unfortunaly it doesn't work.
I have tried with CascadingDropDownExtender outside the TabContainer, but Reference null is catched again.
With CascadingDropDownExtender in same Panel as child DropDownList, I have a javascript alert that say Parent Control not found.
Here the code with CascadingDropDownExtender in same Panel as child DropDownList
<cc2:TabPanel runat="server" HeaderText="Carte Identité" ID="TabCI">
<ContentTemplate>
<ProspectMgt:MarketSegmentDropDownList ID="MarketSegmentDropDownList1" runat="server"
AutoPostBack="false" Width="400px"></ProspectMgt:MarketSegmentDropDownList>
<asp:CompareValidator ID="CompareValidator2" runat="server" CssClass="error" ToolTip="Required"
ControlToValidate="MarketSegmentDropDownList1" ValueToCompare="-1" Operator="NotEqual"
ErrorMessage="MktSegment" Display="Dynamic" Font-Bold="True"></asp:CompareValidator>
</ContentTemplate>
</cc2:TabPanel>
<cc2:TabPanel ID="TabSize" runat="server" HeaderText="Size & Volume">
<ContentTemplate>
<asp:DropDownList ID="DropDownListSize" AutoPostBack="false" runat="server">
</asp:DropDownList>
<cc2:CascadingDropDown ID="cddSize" runat="server" TargetControlID="DropDownListSize"
Category="Size" LoadingText="[...]"
ServicePath="~/Modules/ProspectMgt/WebService/ServiceAjax.asmx" ServiceMethod="GetSizeByMktId"
ParentControlID="MarketSegmentDropDownList1" />
</ContentTemplate>
</cc2:TabPanel>
So I don't see why it is not working. It was working perfectly without TabContainer. I'm using AjaxToolKit 30512. However, I'm looking to drop CascadingDropDownExtender and manage this issue by javascript only.
Is the CascadingDropDownExtender inside the first tab? If it is, if you pull the CascadingDropDownExtender outside the tab container altogether, does that fix the issue? Posting some markup would help resolve this more easily...
HTH.

Categories