stop autopost back on default value of dropdownlist asp.net - c#

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.

Related

asp dropdown reload page when setting dropdown.selectedvalue

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.

Aspx dropdownlist not displaying top value

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.

ASP.Net DropDownList in GridView - Codebehind always sees 1st item as selected

I am working on an ASP.Net application with code behind as C#. In an aspx page, there is a gridview control. One column contains dropdownlist, as follows:
<asp:GridView ID="gvModulesSem1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Grade" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gvModulesTextPadding">
<ItemStyle Width="120px" CSSClass="gvModulesTextPadding" />
<ItemTemplate>
<asp:DropDownList ID="ddlSelectGrade" Text='<%# Eval("Grade")%>' runat="server">
<asp:ListItem Value="-1.0">Select Grade</asp:ListItem>
<asp:ListItem Value="Distinction">Distinction</asp:ListItem>
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B+">B+</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C+">C+</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D+">D+</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
<asp:ListItem Value="P">P</asp:ListItem>
<asp:ListItem Value="F">F</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There are some other columns too, all of which contain labels. But, to keep it simple, I have removed the codes for the other columns.
In page load, when Page.IsPostBack is false, the GridView is bound to a DataTable (which has been filled). This populates the GridView. All the DropDownLists have the first item i.e. "Select Grade" selected.
As a user, I change the selected items of the dropdown lists. Then click a button (outside the grid). In the button click event procedure, when I try to read the selected item of any dropdown list, I find that it is always the first item.
How do I correct this problem?
Thank you.

dropdown showing improper values

I have a web form in which there are some controls along with dropdown list as below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
and theres a gridview in which data is shown on btninsert click.
and I have a link button EDIT. on clicking edit the corresponding values are displayed in their respective controls.
Now, coming to the point,insert works fine, but when i click on Edit link every thing is fine except the value in dropdown list.
it should be like as shown in screenshot below:
but what I get is shown below:
how can I overcome this???
Try giving value to the Select Range list item and then chcek, may be that helps.
Maybe this is helpful for you
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="" >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
In edit click event add a "Select Range" value into the drop down.
ddlRange.Items.Insert (0,"Select Range");
Add <asp:ListItem Value=null>Select Range</asp:ListItem> line.
Try Below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem Value="Select Range">Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
I tried using ddlrange.SelectedValue instead of ddlrange.SelectedItem.Text, and issue is solved.
Thanks everyone for your efforts, I appreciate.

Render dropdown selected value in gridview

<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:DropDownList ID="Division" runat="server">
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text="Computer" Value="Computer"></asp:ListItem>
<asp:ListItem Text="Electronics" Value="Electronics"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
In i am not able to diplay the selected values in the dropdown which is already in the database.
If you are saying that you want to have the Division value for the row to be selected in the dropdown, you need to bind the SelectedValue property of the DropDownList to the field in your data source that contains the Division value. It would look something like this:
<asp:DropDownList ID="Division" runat="server" SelectedValue='<%# Bind("Division") %>' >
You can use Selected="True" as in:
<asp:ListItem Text="Select" Value="Select" Selected="True"></asp:ListItem>

Categories

Resources