Getting value from Dropdownlist in Checkboxlist in C# - c#

Ok. I did something crazy. This actually renders correct but how would you get the selected value from the dropdown from the server-side using C#?
I tried getting the dropdownlist code
CheckBoxList.Items[0].Text.Substring(CheckBoxList.Items[0].Text.indexOf("<select>"));
But now that I have the dropdown, how do I get the selected value from it?
EDIT 5/15/15 5:39PM EST
I think it would if I wrote the code as to how I am creating this:
CheckBoxList chkBoxLst = new CheckBoxList();
chkBoxLst.Items.Add("Grade");
chkBoxLst.Items.Add("2");
chkBoxLst.Items.Add("3");
chkBoxLst.Items[0].Text += "<select id='Letter' runat='server'>
<option>A</option>
<option>B</option>
<option>C</option>
</select>"
I am creating this dynamically with server-side code.
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Grade <select id="Letter" runat="server">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:CheckBoxList>
If you see what I am trying to do and know a better way, suggestions welcome.

If what you are trying to accomplish is get the selected value, change this
<select id="Letter" runat="server">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
for this
<asp:DropDownList ID="Letter" runat="server" >
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
</asp:DropDownList>
and to get the selected value do this
string selectedValue = Letter.SelectedValue;

You can also get the value from the Form values collection using the id for the SELECT element.
var val = Request.Form["Letter"];

Related

How to make an asp dropdown list item unselectable

I have written following code to display drop down list :
<asp:DropDownList AutoPostBack="True" ID="ddlCities" runat="server" class="form-control input-sm" placeholder="" TabIndex="5">
<asp:ListItem>Select City</asp:ListItem>
<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
<asp:ListItem value="3_3004"> Ahmedabad East</asp:ListItem>
<asp:ListItem value="3_3005"> Ahmedabad West</asp:ListItem>
<asp:ListItem value="3_3006"> Ahmedabad-Bopal and Surroundings</asp:ListItem>
<asp:ListItem value="3_3007"> Ahmedabad-Gandhinagar</asp:ListItem>
<asp:ListItem value="3_3008"> Ahmedabad-Sabarmati and Surroundings</asp:ListItem>
<asp:ListItem value="3_3009"> Ahmedabad-SG Highway and Surroundings</asp:ListItem>
</asp:DropDownList>
I want to make following item inside dropdown unselectable :
<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
Note : I CAN'T USE OPTGROUP!!!!
and i don't want to hide it. it will be shown in dropdown, but user can't select this item.
I have tried adding 'disabled' attribute, but it hides that item.
i have also tried :
ddlCities.Items[1].Attributes.Add("Style", "cursor:not-allowed");
It doesn't allow cursor, but still user can select this item, is there any other way to make this particular item unselectable??
Even though you said that disabled attribute hides the element, you are wrong.
Disabled attribute is exactly what you should use for this:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">First</asp:ListItem>
<asp:ListItem Value="2" disabled="disabled">Second</asp:ListItem>
<asp:ListItem Value="3">First</asp:ListItem>
</asp:DropDownList>
Result:
I think you're looking for something like this. Please see if it can help you.
[Update]
just looked back to your post and understand you cannot use OPTGROUP. May i know the reason? as your requirement needs this option.

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
}

DropDown not showing selected value when loaded

I have dropdown as below:
<asp:DropDownList ID="ddlPriority" runat="server">
<asp:ListItem Text="Yes" Value="True" Selected="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
From above code it should show default text as "Yes" when loaded.
I havent wrote any code behind for binding this drop down. Just wanted to have hard-Code Yes No values in it.
But its not showing me anything selected when i load the page.
Its as below when i load page:
When i done inspect element for this dropdown i got:
<select name="ctl00$MainContent$ddlPriority" id="MainContent_ddlPriority">
<option selected="selected" value="True">Yes</option>
<option value="False">No</option>
</select>
I am wondering why default selected Yes is not comming in drop down...
Note: Yes - No values are comming when i scroll the drop down, but default selected value is not comming when i load the page.
Please help me.
Try to add this property to your DropDownlist: AppendDataBoundItems="true"
try add these properties to your DropDownlist ddlPriority
EnableViewState = "true"
AppendDataBoundItems="true"
Have you try this :
<asp:DropDownList id="ddlPriority" runat="server" >
<asp:ListItem value="0">Select</asp:ListItem>
<asp:ListItem value="1" Selected="True">Yes</asp:ListItem>
<asp:ListItem value="2">No</asp:ListItem>

Getting a dropdownlist to check a checkbox in asp.net/C#

<input runat ="server" type ="checkbox" id="helprequest" />
<label for="helprequest">Help request</label>
<asp:DropDownList ID="options" runat="server" OnSelectedIndexChanged="checkHelpRequest">
<asp:ListItem Text="Windows"></asp:ListItem>
<asp:ListItem Text="Macintosh"></asp:ListItem>
<asp:ListItem Text="Linux"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
In my codebehind, I have
protected void checkHelpRequest(object sender, EventArgs e)
{
helprequest.Checked = true;
}
But when I select something on the dropdownlist, the checkbox, does not get marked as checked, how do I get the checkbox to appear as checked when I change the index on a dropdownlist?
Your DropDownList does not have AutoPostBack='true' set. Without setting this, your dropdown will not post back when you change the selected index.
Just change it to:
<asp:DropDownList AutoPostBack="true" ID="options"
runat="server" OnSelectedIndexChanged="checkHelpRequest">
Without setting this, your checkHelpRequest method will still be called when your drop down changes index, but only after a postback is caused by some other control, like a button, or another DropDownList that does have AutoPostBack set.

Categories