VS 2005 Webpage. Multi select dropdown list in asp.NET - c#

I have a asp.NET webpage written in VS 2005 (C#). It currently contains a dropdown list. I need to change this dropdown list so that multiple items can be selected. Here is the current code (.aspx page)
<div style="float: left; width: 280px;">
<asp:Label ID="lblExport" EnableViewState="false" runat="server" Text="Export"></asp:Label>
<asp:DropDownList TabIndex="24" runat="server" Width="70px" ID="ddlExport"
DataSource='<%# CodeListManager.Instance().GetCodelist(CodeListCache.Export)%>'
DataTextField="Value" DataValueField="Name" SelectedValue='<%# Bind("Export") %>'
AppendDataBoundItems="true">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
</div>
Is there a way to keep this as a dropdown or would I need to use a listbox? If I use a listbox how would I program it to allow multi select? Can this be done on .aspx page or will it have to be on the aspx.cs page?

Related

Foreach RadioButtons in ASP.NET Webforms

I'm have ASP MultiView for an application form. Inside one of the view I have few radio button as follows--
<div class="row">
<asp:CustomValidator ID="Group1Validator" GroupName="Group1" runat="server"
ValidationGroup="Group1" Display="Dynamic"></asp:CustomValidator>
<asp:RadioButton ID="Group1Yes" runat="server" GroupName="Group1" Text="Yes"/>
<asp:RadioButton ID="Group1No" runat="server" GroupName="Group1" Text="No" />
</div>
Now on the code behind I want to iterate through all the RadioButtons in that page and pull the values to be saved in the DB. I have tried the solution HERE both Loop and LINQ version, but the problem is my RadioButton controls are not directly in the page. Let me elaborate-
First, I have multi view in a page-
Multi view contains views-
And finally inside views I have the RadioButton controls-
Now my goal is the fetch the values of all the RadioButton that are selected which is laid out using the div above.
Any help and guidance will be much appreciated! Thanks!
You must put your RadioButtons as childs of RadioButtonList, then iterate in foreach loop over RadioButtonList items.
Why not use a RadioButtonList
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList>
And then you can get the value in code behind
string value = RadioButtonList1.SelectedValue;
You can also add items to the RadioButtonList in code behind
RadioButtonList1.Items.Insert(0, new ListItem("Yes", "1", true));
RadioButtonList1.Items.Insert(1, new ListItem("No", "0", true));

opposite of selectedvalue in radio button

I'm working in ASP.net and I want to display both values C and F.
This is my default file:
<asp:Label ID="RadioLabel" runat="server" Text="Typ av konvertering"/>
<asp:RadioButtonList ID="RadioButtonList" runat="server">
<asp:ListItem value="C" Selected="true">Celcius till fahrenheit</asp:ListItem>
<asp:ListItem value="F">Fahrenheit till celcius</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="SubmitButton" runat="server" Text="Konventera" OnClick="SubmitButton_Click" />
I use RadioButtonList.SelectedValue in my code behind file to get the value I select.
My question is if there is any way to display the other value that I didn't select?
I'm trying to make a celcius to fahrenheit converter and reverse.
To get all items in RadioButtonList you can do something like this.
foreach (ListItem item in RadioButtonList.Items)
{
// write your code here
}

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.NET C#, dynamic drop down in repeater causing full post back

I have a page that contains a Repeater, which contains server control elements, within an UpdatePanel and while all other controls behave normally, the Drop Down control causes a full postback every time.
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:SomeWorkingControl ID="swc" runat="server" />
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="0" Value="0" />
<asp:ListItem Text="1" Value="1" />
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
This is vaugely what my code looks like, the DropDownList control is actually in a UserControl, but the theory is the same.
If I apply an event to SomeWorkingControl then there is an Ajax postback and all is fine.
However, the event associated with the DropDownList causes a full postback! I know usually you would set an Async trigger for the DropDown, but since it is created in a repeater (and therefore I can not know how many there will be) so I don't really see how that could work.
Is there anybody who has experienced this before and knows a workaround perhaps?
Try to change this line:
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="true">
for:
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="AutoID">
Recently I had the same problem and I found out that the ClientIDMode can solve it.
Please have a look here: asp.net ClientIDMode Changes

Access Parent Page Control in Child User Control

I have an aspx page in which I have Ajax UpdatePanel which has a AJAX Tabcontainer which has 5 tab. In First tab I have a DropDownList for ProductID. In the second tab I have used a UserControl whose parameter needs to be reflected based on productID. I want to Access the DropDownList ProductID in the user control which I am not getting. Part of my code is
DropDownList IDList = (DropDownList)(this.Parent.FindControl("ProductID");
this is not working and I am getting NULL. I also have Tried
DropDownList IDList = (DropDownList)(this.Page.FindControl("ProductID");
Please tell me how I can do this.
As asked Part of necessary code is
<asp:UpdatePanel ID="UpdatePanelTankFormula" runat="server">
<ContentTemplate>
<asp:tabcontainer id="TabContainerTankFormula" AutoPostBack="true" runat="server" OnClientActiveTabChanged="clientActiveTabChanged" activetabindex="0" style="width:100%;">
<asp:TabPanel ID="InputDataTab" runat="server" HeaderText="Data Input">
<ContentTemplate>
<asp:DropDownList id="TankNameCombo" DataTextField="TankName" runat="server" AutoPostBack="true" DataValueField="customertankid" width="200px" onclick="javascript:SetHiddenField();"> </asp:DropDownList>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="LacticAcidAdditionTab" runat="server" HeaderText="Lactic Acid Addition">
<ContentTemplate>
//My user control
<UC:TankNote runat="server" ID="LaticTankNotesUC" GetNoteType="LAC" MEQMode="False"></UC:TankNote>
</ContentTemplate>
</asp:TabPanel>
</asp:tabcontainer>
<ContentTemplate>
</asp:UpdatePanel>
Now in the Code Behind of this User Control I want to access this DropDownList, which I am not getting. For the fix I have define a Public function that return the value of this list. But its a fix not solution.
You will have something like this in tab control.
<cc1:TabContainer ID="TabContainer1" runat="server">
<cc1:TabPanel ID="TabPanel1" runat="server">
<ContentTemplate>
<asp:DropDownList id="dropdownlist1" runat="Server"/>
</ContentTemplate>
</cc1:TabPanel>
So first you need to find tabPanel1 then find dropdownlist1 like following.
TabContainer TabContainer1= (TabContainer)(this.Page.FindControl("TabContainer1");
if (TabContainer1!=null){
TabPanel TabPanel1= (TabPanel)(this.TabContainer1.FindControl("TabPanel1");
if(TabPanel1 !=null){
DropDownList dropdownlist1= (DropDownList)(this.TabPanel1.FindControl("dropdownlist1");
}}

Categories