DropDown not showing selected value when loaded - c#

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>

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));

Getting value from Dropdownlist in Checkboxlist in 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"];

AJAX Trigger other things to update on click

I have been adding UpdatePanels to my application around the objects that I just want to update.
Example, When I click a button I want a ListBox to update. I would wrap the UpdatePanel around the list box in the html, but when I do this my design goes all over the place.
I am not sure If I'm using AJAX the correct way, as I have seen people adding Triggers but don't know how and where to use them, here is an example of my code:
<asp:ListBox ID="lst_AdminExistingDepartments" runat="server" >
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
</asp:ListBox>
</div>
<div class="AddEditUpdateDepartment">
<asp:Label ID="lbl_AdminAddEditUpdateDepartments" runat="server" Text="Modify Departments" />
<asp:Label ID="lbl_AdminAddDepartment" runat="server" Text="Add Department" />
<input id="txt_AddDepartment" runat="server" type="text" class="aclass" />
<input id="btn_AddDepartment" runat="server" type="button" class="ButtonAdminPage" value="Add" onserverclick="btn_AddDepartment_ServerClick" />
Here is a ListBox and also a Textbox and a Button. When they click that Button I would want the ListBox to update with the record entered in the TextBox using AJAX?
At the most basic level you need to place the UpdatePanel around all controls being affected by the action. So you need the List, TextBox and Button all inside the UpdatePanel.
Following on from that you have more advanced scenarios where you can have Triggers and multiple UpdatePanels etc. However, it would be best to start with something simple (i.e. everything you need in one UpdatePanel and advance from there.

Drop Down List Selected Index changed not working in Update panel

I have a drop down list in UpdatePanel_2, it gets populated when Button_1 is clicked in UpdatePanel_1.
My ddlist markup is,
<asp:DropDownList id="drop1" runat="server" EnableViewState="true" AutoPostBack="true" OnSelectedIndexChanged="Drop1_SelectedIndexChanged" />
then code behind is,
protected void Drop1_SelectedIndexChanged(object sender, EventArgs e)
{ }
I also tried putting AutoPostback=true to my DropDownList, still no success.
I also added triggre to update panel 2 but no gain,
<Triggers>
<asp:AsyncPostbackTrigger ControlID="drop1" EventName="SelectedIndexChanged" />
</Triggers>
I am populating DropDownList using a button not PAGE LOAD METHOD PLEASE READ before answering.
Thanks
Check the data to populate the DropDownList in the Page_Load event and always check IspostBack:
if(!IsPostBack)
{
//DropDownList configuration
}
Use EnableViewState:
<asp:DropDownList ID="ddlAddDepPlans" runat="server" AutoPostBack="true" EnableViewState="true" />
Hope it helps you.
I had the same issue. My problem was that the values of my ListItems were all the same :D
<asp:DropDownList ID="ddlFilterLogins" runat="server" Visible="true" AutoPostBack="true">
<asp:ListItem Value="0" Text="All"></asp:ListItem>
<asp:ListItem Value="0" Text="Some"></asp:ListItem>
<asp:ListItem Value="0" Text="Some more"></asp:ListItem>
</asp:DropDownList>
It should be like this:
<asp:DropDownList ID="ddlFilterLogins" runat="server" Visible="true" AutoPostBack="true">
<asp:ListItem Value="0" Text="All"></asp:ListItem>
<asp:ListItem Value="1" Text="Some"></asp:ListItem>
<asp:ListItem Value="2" Text="Some more"></asp:ListItem>
</asp:DropDownList>
Hope this helps. This might be hard to find sometimes :)
Please, when you initialize it in Page_Load() check if not is postback. If you don't do it, you will always set the default value, and this replaces the value setted in the event.
if(!IsPostBack)
{
//DropDownList configuration
}
You can use Init event instead of SelectIndexChanged.
It worked fine for me.
Hope you got my point.
It was also a wired problem for me. finally It was because of identical listitems in the dropdown as shown below. during development you may use same items just for testing. change them.
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>

Categories