I have the following asp dropdown:
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="True">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Once a selection has been made, and the data loaded, I want to set the value in the dropdown to display the value chosen by the user.
I am doing this by:
ArticleCategories.SelectedValue = CategoryID.ToString();
This works fine the first time, but from then the page is stuck on that selection, as the selected value gets loaded before the new value can load.
I have tried disabling the itemchangelistener by:
ArticleCategories.SelectedIndexChanged += new EventHandler(doNothing);
But that does not work.
I have also tried to disable postback which also does not work.
Any ideas?
You should only set the selection when loading the page the first time. In ASP.NET, you can achieve this by checking the IsPostBack property:
if(!IsPostBack)
{
ArticleCategories.SelectedValue = CategoryID.ToString();
}
You can do Autopostback= false
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="False">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Hey before selecting another item from Dropdown you should clear previous selection then selected another one.
ArticleCategories.ClearSelection();
Hope it helps you.
Related
Goal: I have 3 dropdowns for 3 different people.
I would like to be able to update the database with value from dropdown that is used. I would like to be able to use more than one dropdown without having to manually refresh the page. It's on Site.Master so they appear on every page.
Problem: the value in the database updates correctly after the update, but the text in the dropdown becomes incorrect if I use more than one dropdown without refreshing the page.
It works fine if user makes a change on first dropdown, then goes to another page, then uses the second dropdown. BUT the view acts strange if I
Change Dropdown1 (e.g. from True to False).
Then change Dropdown2 (True to False).
At this point, dropdown 1 will show True again (even though it was changed to False a couple steps ago).
It's almost as if the first dropdown gets the current value from the second dropdown.
However, if I refresh the page, it shows the appropriate values (and the database has the correct value the entire time)
Probably easier with screenshots:
After changing Professor1 from True to False (OK):
After changing Professor2 from True to False (NOT OK - notice Professor 1 appears as True again, even though its correct in the DB):
What I've tried:
I've used debug breakpoints on every line in the C# I could think of
I've tried moving the code into the OnSelectedIndexChanged method
I've tried changing from .Text to .SelectedValue
I've tried using multiple OnSelectedIndexChanged methods instead of one
HTML:
<div>
<asp:Image ID="img1" runat="server" height="20" width="20"
CssClass="professoricon"/>
<sup><asp:Label ID="myLabel" runat="server" Font-Size="20px"
CssClass="oval"/></sup>
<asp:Label ID="myLabelMid" runat="server" CssClass="professorname"/>
<asp:DropdownList runat="server" id="ddlAvailability1"
AutoPostBack="true"
OnSelectedIndexChanged="ddlAvailability_OnSelectedIndexChanged"
CssClass="dropdowns">
<asp:ListItem ID="LT1"></asp:ListItem>
<asp:ListItem ID="RT1"></asp:ListItem>
</asp:DropdownList>
</div>
<asp:Panel ID="row2" runat="server" Visible="false">
<asp:Image ID="img2" runat="server" CssClass="professoricon"/>
<sup><asp:Label ID="L2" runat="server" Font-Size="20px"
CssClass="oval"/></sup> <asp:Label ID="M2" runat="server"
CssClass="professorname"/>
<asp:DropdownList runat="server" id="ddlAvailability2"
AutoPostBack="true"
OnSelectedIndexChanged="ddlAvailability2_OnSelectedIndexChanged"
CssClass="dropdowns">
<asp:ListItem ID="LT2"></asp:ListItem>
<asp:ListItem ID="RT2"></asp:ListItem>
</asp:DropdownList>
</asp:Panel>
C#:
PageLoad() {
myLabel.Text = professorStatsListDto.professorStatsList[0].NumberOfActiveVisits.ToString();
myLabelMid.Text = professorStatsListDto.professorStatsList[0].LastName.ToString();
LT1.Text = professorStatsListDto.professorStatsList[0].Available.ToString();
RT1.Text = (!professorStatsListDto.professorStatsList[0].Available).ToString();
if (professorStatsListDto.professorStatsList.Count>1)
{
L2.Text = professorStatsListDto.professorStatsList[1].NumberOfActiveVisits.ToString();
M2.Text = professorStatsListDto.professorStatsList[1].LastName.ToString();
LT2.Text = professorStatsListDto.professorStatsList[1].Available.ToString();
RT2.Text = (!professorStatsListDto.professorStatsList[1].Available).ToString();
row2.Visible = true;
}
}
I have a dropdownlist(autopostback enabled) in my project in binded through sqldatasource, i have added an initial value to it using list item. The problem is the page is being postbacked even when selecting the initial value which i dont want. Is there a way to achieve this?
<asp:DropDownList ID="DropDownListTheme" runat="server"
DataSourceID="SqlDataSourceTheme" DataTextField="h_theme"
DataValueField="h_theme" Height="30px" Width="30%"
AppendDataBoundItems="true" AutoPostBack="True">
<asp:ListItem Text="--Select One--" Value="-1" />
</asp:DropDownList>
Set the initial item as selected initially
<asp:ListItem Text="--Select One--" Selected="True" Value="-1" />
Add
onchange="if(this.selectedIndex == 0)return false;"
event to drop down list.
I have an aspx page (C# code page). I have some hard coded dropdownlists and for some reason they are not displaying the top list item (value). I added an extra top list item (value) and now the correct values display for the values already in it, but that extra one does not display.
The only functionality I do with my dropdownlists in my C# code is to hide or show them. And then do validation or not based on the selected value.
My aspx code:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True"
AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
C# Code:
ddlAction.Visible = false;
ddlAction.Visible = true;
I use dropdownlist's regularly and have never had this problem before. Does anyone have any ideas what the issue could be?
UPDATE TO THIS ISSUE:
I added my items in my C# code as per Rahul. Did a quick test and it worked.
Now this morning, I am once again getting blanks for the first item ("Please Select").
Aspx code:
<asp:DropDownList ID="ddlAction" runat="server"
AppendDataBoundItems="True" Height="27px" Width="159px">
</asp:DropDownList>
C# code:
ddlAction.Visible = true;
ddlAction.AppendDataBoundItems = true;
ddlAction.Items.Insert(0, new ListItem("Please Select","Select"));
ddlAction.Items.Insert(1, new ListItem("Yes", "Yes"));
ddlAction.Items.Insert(2, new ListItem("No", "No"));
ddlAction.DataBind();
Rendered source code:
<select name="ctl00$ContentPlaceHolder1$ddlAction" id="ContentPlaceHolder1_ddlAction" style="height:27px;width:159px;">
<option selected="selected" value="Select"></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
Try to use AppendDataBoundItems = true property of DropSownList into your .aspx page.
you may also assign value from code behind as well like
ddlAction.Items.Insert(0, new ListItem(String.Empty, String.Empty));
use AppendDataBound = true in your aspx coe.
<asp:DropDownList ID="ddlAction" AppendDataBound = true runat="server" Visible="True" Height="25px"
Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
Edit 1
More detail about List Item
<asp:ListItem Value="-2" Text="Please Select"></asp:ListItem>
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="-1" Text="No"></asp:ListItem>
I suggest you declare your DropDownList ListItems using its internal properties and defining what ListItem must be the selected one:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True" AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Text="Please Select" Value="Select" Selected="True"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"</asp:ListItem>
</asp:DropDownList>
It's the way ASP.NET uses to work and will return you the right selected value on the server side on postbacks.
I think that you don't have to use nor the DataBind() method neither set the AppendDataBoundItems, because, you already inserted the ListItems and you aren't loading options from a database!
I think you need to tell what ListItemIndex is the selected one by seting a value to the DropDownList.SelectedIndex property.
EDIT
Also, try to read to MSDN documentation about the AppendDataBoundItems property and the enter link description here method.
I have an existing RadioButtonList on a page and need to set the second button to be checked as default instead of the first.
I probably need to do it with javascript on the page as I cannot edit the original control.
<list:RadioButtonList runat="server" Class="class" Text="text"
AlternativeText="alternative text" />
Any idea how i can detect the control and set its default value?
If you use ASP.NET you can set the following:
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Selected="True" ></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
I've added the attribute Selected="True", so you always have a default value selected.
You can also do this in code:
if (RadioButtonList1.SelectedIndex == -1) //-1 is the indication of none selected
{
RadioButtonList1.SelectedIndex = 2; //select index 2 (can also be value or text)
}
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