My DropDownList SelectedIndex returns to 1 when I click submit - asp.net

I have two dropdownlists:
First one:
<asp:DropDownList ID="Drddl" class="form-control form-control-sm" Height="30" runat="server"
Width="350" Enabled="False" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="FullName" DataValueField="Name">
</asp:DropDownList>
and gets filled on page load :
If Not Page.IsPostBack Then
FillDrData()
End If
The second:
<asp:DropDownList ID="Drddl" class="form-control form-control-sm" Height="30" runat="server" Width="350" Enabled="False" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="FullName" DataValueField="Name"></asp:DropDownList>
and gets filled on SelectedIndexChanged of first one:
Private Sub Drddl_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Drddl.SelectedIndexChanged
FillAgenceData()
End Sub
My question is:
Everything is working fine until I click submit button that read selected items from both dropdownlists and save it in SQL database. The Agenciesddl selecteditem always return to 1.

Be mindful of the postbacks of each dropdownlist. Quick and dirty way would be to set the selected values in ViewState or Session variables then get the variables on Submit. See below for rough example (pardon the c#)
Private void Drddl_SelectedIndexChanged(sender As Object, e As EventArgs)
{
ViewState["DrddlVal"] = Drddl.SelctedValue;
}
private void submit(sender As Object, e As EventArgs)
{
string dbparmval = ViewState["DrddlVal"].ToString();
}

As usual the answer is very simple.
the DataValueField property of DropDownList must be UNIQUE
when SelectedIndexChanged event triggered, the DropDownList goes to first value if he find more than one item has same value.
thanks for this article:
enter link description here
thanks everyone..

Related

Dropdown in listview SelectedIndexChanged doesn't work first time but does after

I've searched high and low, I can't seem to find out what's happening with this. I've simplified the code, but I really have taken it back to as basic as this and still have the same problem.
I have a drop down list in a a repeater (in a Web Form with master page):
<asp:DropDownList ID="TicketQuantityDDL" runat="server" CssClass="qtyddl" AutoPostBack="true" OnSelectedIndexChanged="TicketQuantityDDL_SelectedIndexChanged" CausesValidation="false" SelectedIndex='<%# CInt(Eval("Quantity")) - 1%>'>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
Handler
Protected Sub TicketQuantityDDL_SelectedIndexChanged(sender As Object, e As EventArgs)
myLiteral.text = "Selected Index Changed handled..."
End Sub
The first time the page is loaded if I change the DDL the the page is posted back - the selected index change handler is NOT fired (I've stepped through the code, page.ispostback is true). Every time after the handler works unless the page is full reloaded.
Things I've tried:
Manually adding a handler OnItemCreated
Manually adding a handler OnItemDataBound
Manually registering the control for async postback with scriptmanager
Using OnClientSelectedIndexChanged to trigger postback from the client
Removing AutoPostBack and all of the above again...
I've used Page.Request.Params("__EVENTTARGET") to verify that when the partial postback is fired that the control is the drop down.
Even though viewstate is enabled I've tried specifying this for the control and the page directly.
Disabling validation.
I've tried not binding the value of the drop down and just leaving it
as is with no selected value and then manually setting the initial
selected value - no dice.
Tried removing update panel, same issue.
Things that are DEFINITELY not happening here.
I'm not rebinding on post back if not page.ispostback... databind...
I'm not selecting the same value/first item in the drop down
This isn't an auto ID problem, the controls ID stay the same through postbacks.
I'm not doing anything funky other than binding the repeater to a list of objects.
Why isn't the handler firing the first time? After the first time everything works exactly as intended.
Update
I've replicated the exact same behaviour in a list view. Due to time constraints I've used another approach but I'd really like to know how to fix this or at least know why it doesn't work.
Update 2
I've tested the functionality with a bog standard web form and it functions correctly. Something is up with this being in a contentplaceholder from a masterpage, the script manager or update panel. It's as if the event handler for the dropdown is only registered after the first post back, I've tried registering the handler in DataBound and also in the page LoadComplete events, the same thing still happens.
Update 3
I've since changed it to a list view, I'm having the exact same issue though.
This is on a web form with master page, the master page contains the script manager, the list view is in an update panel, although I've tried removing this and I still have the same issue. I've not included the onselectedindexchanged code, I've made it as simple as changing the text of a literal - doesn't work first post back, does the second.
I had originally specified the list items manually but have changed this to programatically at itemDataBound, still no difference.
As I stated above when I check which control caused the postback it's definitely the ddl, it just doesn't fire selectindexchanged the first time. I've also tried specifying the OnSelectedIndexChange in the control itself, still no dice.
Page load ,bind, list view and on item created code.
Page Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim _Basket = SessionHandler.getSessionObject(SessionHandler.SessionObjects.Basket)
If _Basket Is Nothing OrElse DirectCast(_Basket, BasketContainer).BasketItemList.Count = 0 Then
BasketSectionContainer.Visible = False
alertLiteral.Text = AlertGenerator.GetAlertHTML("No Items in Basket", "There are no items in your basket, please use the menu above to navigate the site.", AlertGenerator.AlertType.warning)
If _Basket IsNot Nothing Then SessionHandler.removeSessionObject(SessionHandler.SessionObjects.Basket)
Exit Sub
Else
Dim lBasket = DirectCast(_Basket, BasketContainer)
BindBasket(lBasket)
End If
End If
End Sub
Bind
Private Sub BindBasket(lBasket As BasketContainer)
basketListView.DataSource = lBasket.BasketItems
basketListView.DataBind()
bindTotals(lBasket) 'This just sets text of literals on the page outside of the listview
If lBasket.Postage Then
PostageDDL.visible = True 'This is outside of the list view also
End If
End Sub
Item Created
Private Sub basketListView_ItemCreated(sender As Object, e As ListViewItemEventArgs) Handles basketListView.ItemCreated
Dim QtyDDL As DropDownList = DirectCast(e.Item.FindControl("TicketQuantityDDL"), DropDownList)
AddHandler QtyDDL.SelectedIndexChanged, AddressOf TicketQuantityDDL_SelectedIndexChanged
End Sub
_Item Data Bound _
Private Sub basketListView_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles basketListView.ItemDataBound
Dim data As BasketItem = DirectCast(e.Item.DataItem, BasketItem)
Dim QtyDDL As DropDownList = DirectCast(e.Item.FindControl("TicketQuantityDDL"), DropDownList)
For i As Integer = 1 To 6
QtyDDL.Items.Add(New ListItem(i.ToString, i.ToString))
Next
QtyDDL.DataTextField = data.BasketItemID.ToString 'no command arg for DDL so using this, I've tested without, doesn't make a difference.
Select Case data.BasketType
Case BasketInfo.BasketItemType.DiscountedTickets, BasketInfo.BasketItemType.Tickets, BasketInfo.BasketItemType.Goods
'tickets and goods...
QtyDDL.Items.FindByValue(data.Quantity.ToString).Selected = True
Case Else
'non ticket or goods type, disable quantity selection
QtyDDL.Items.FindByValue("1").Selected = True
QtyDDL.Enabled = False
End Select
End Sub
_List View _
<asp:ListView ID="basketListView" runat="server">
<LayoutTemplate>
<table class="cart-table responsive-table">
<tr>
<th>Item</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th></th>
</tr>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
<table class="cart-table bottom">
<tr>
<th>
<asp:Button ID="ApplyDiscountCodeButton" runat="server" CssClass="button color pull-right" Text="Apply Code" />
<asp:TextBox ID="DiscountCodeTextBox" runat="server" CssClass="discount-tb pull-right" />
</th>
</tr>
</table>
<div class="clearfix"></div>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<img src="/images/shows/<%# Eval("imageURL")%>.jpg" alt="<%#Eval("BasketItemTitle")%>" class="basketimg" /></td>
<td class="cart-title">
<%#Eval("BasketItemTitle")%>
<br />
<%# String.Format("{0:dddd} {1} {0:MMMM yyyy} | {0:HH:mm}", Eval("PerformanceStarts"), Eval("OrdinalDay"))%>
<br />
<%# Eval("VenueTitle")%>
</td>
<td>
<%#Eval("PriceBandType")%>
<br />
# <%# String.Format("{0:c}", Eval("PriceBandValue"))%>
</td>
<td>
<asp:DropDownList ID="TicketQuantityDDL" runat="server" CssClass="qtyddl" AutoPostBack="true" ClientIDMode="Static" />
</td>
<td class="cart-total"><%#String.Format("{0:c}", Eval("BasketItemTotalValue"))%></td>
<td>
<asp:LinkButton ID="RemoveLinkBtn" runat="server" CssClass="cart-remove" CommandName="RemoveBasketItem" CommandArgument='<%#Eval("BasketItemID")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Since the Dropdown in inside the repeater, you can try the following Option instead.
Add OnItemCommand to the repeater. This should definitely Trigger the Event on selection Change in the Dropdown. Then in the OnItemCommand you Need to cast the Sender to DropDownList to be able get the selected value of the dropdown
You have to register the dropdown list event like this.
protected virtual void OnRepeaterItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList dropdown = (DropDownList)e.Item.FindControl("TicketQuantityDDL");
dropdown.SelectedIndexChanged += TicketQuantityDDL_SelectedIndexChanged;
}
And also add this piece of code in your repeater.
OnItemCreated="OnRepeaterItemCreated"
And then you can do the selected index changed event like this.
protected void TicketQuantityDDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList mydropdownlist = (DropDownList)sender;
Response.Write(mydropdownlist.SelectedValue);
}
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlListFind = (DropDownList)sender;
ListViewItem item1 = (ListViewItem)ddlListFind.NamingContainer;
DropDownList getDDLList = (DropDownList)item1.FindControl("dropdownlist1");
Label lblMessage = (Label)item1.FindControl("lblMsg");
lblMessage.Visible = true; lblMessage.Text = "dropDown text is : " + getDDLList.SelectedItem.Text + " and value is : " + getDDLList.SelectedItem.Value;
}

textChanged event doesn´t fire and i have autopostback=true

I have a textbox event declarated but it doesn´t fire. I have seen in SO other answers but all of them say that autopostback property has been true and i have it
my aspx
<asp:ScriptManager ID="ScriptManager2" runat="server" />
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox" OnTextChanged="txtDia_TextChanged"/>
<Juice:Datepicker ID="Datepicker2" runat="server" TargetControlID="txtDia"
DateFormat="dd/mm/yy"
MonthNames="Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre"
MonthNamesShort="Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic,"
AutoPostBack="True" /></td>
and my aspx.vb
Protected Sub txtDia_TextChanged(sender As Object, e As System.EventArgs) Handles txtDia.TextChanged
CargarDatos()
End Sub
You should define your textbox like this (see OnTextChanged="txtDia_TextChanged" being added):
<asp:TextBox OnTextChanged="txtDia_TextChanged"
runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox"/>
And remember that this event will rise onblur (focus removed from that textbox) only.
In your aspx you should write
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px"
AutoPostBack="True" CssClass="textbox" OnTextChanged="txtDia_TextChanged"/>
The event is not triggered if you overwrite the text in codebehind. So for example if you databind the control where the TextBox sits in.
You should so that only If Not IsPostBack:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
DataBindAllControls(); // including your textbox
}
}
Edit: Sorry, here VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
DataBindAllControls() ' including your textbox
Else
End Sub
What FAngel said is correct.
I've had the same issue when validating dates and using jquery datepicker. I used the below to fire the validation routines after the box had been populated. So technically they fired twice, once after the textbox had lost focus to the datepicker, then again when my code fired after the datepicker had populated the textbox. It would have been better to disable the onblur event and call directly from the below, but wasn't possible in my project.
$('.datePicker').each(function () {
$(this).datepicker({
onSelect: function () {
$(this).trigger('blur');
}
});
});
You can use a variation on this by disabling the auto-postback and manually triggering it via the onSelect event.
You need to specify the method to be called when the event is fired like so:
<asp:TextBox runat="server" ID="txtDia" Width="120px" Height="20px" OnTextChanged="txtDia_TextChanged" AutoPostBack="True" CssClass="textbox"/>
Note:
OnTextChanged="txtDia_TextChanged"

How to clear exisiting dropdownlist items when its content changes?

ddl2 populates based on ddl1 selected value successfully.
My issue is the data that is already present in ddl2 does not clear before appending the new data so ddl2 content just continues to grow every time ddl1 is changed.
<asp:DropDownList ID="ddl1" RunAt="Server" DataSourceID="sql1" DataValueField="ID1" DataTextField="Name2" AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Text="ALL" Selected="True" Value="0"/>
</asp:DropDownList>
<asp:DropDownList ID="ddl2" RunAt="Server" DataSourceID="sql2" DataValueField="ID2" DataTextField="Name2" AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Text="ALL" Selected="True" Value="0"/>
</asp:DropDownList>
<asp:SqlDataSource ID="sql1" RunAt="Server" SelectCommand="sp1" SelectCommandType="StoredProcedure"/>
<asp:SqlDataSource ID="sql2" RunAt="Server" SelectCommand="sp2" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Type="Int32" Name="ID1" ControlID="ddl1" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:SqlDataSource>
I have tried re-databinding in code behind on selected index change and also items.clear with little success.
Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
ddl2.Items.Clear()
ddl2.DataSource = sql2
ddl2.DataBind()
End Sub
QUESTION
How to get items present in an asp:dropdownlist to clear before new values are populated when the dropdownlists content is dependent on another dropdownlists selected value?
Please post any code in VB
Using ddl.Items.Clear() will clear the dropdownlist however you must be sure that your dropdownlist is not set to:
AppendDataBoundItems="True"
This option will cause the rebound data to be appended to the existing list which will NOT be cleared prior to binding.
SOLUTION
Add AppendDataBoundItems="False" to your dropdownlist.
Now when data is rebound it will automatically clear all existing data beforehand.
Protected Sub ddl1_SelectedIndexChanged(sender As Object, e As EventArgs)
ddl2.DataSource = sql2
ddl2.DataBind()
End Sub
NOTE: This may not be suitable in all situations as appenddatbound items can cause your dropdown to append its own data on each change of the list.
TOP TIP
Still want a default list item adding to your dropdown but need to rebind data?
Use AppendDataBoundItems="False" to prevent duplication data on postback and then directly after binding your dropdownlist insert a new default list item.
ddl.Items.Insert(0, New ListItem("Select ...", ""))
You should clear out your listbbox prior to binding:
Me.ddl2.Items.Clear()
' now set datasource and bind
Please use the following
ddlCity.Items.Clear();
Just 2 simple steps to solve your issue
First of all check AppendDataBoundItems property and make it assign false
Secondly clear all the items using property .clear()
{
ddl1.Items.Clear();
ddl1.datasource = sql1;
ddl1.DataBind();
}
just compiled your code and the only thing that is missing from it is that you have to Bind your ddl2 to an empty datasource before binding it again like this:
Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal
e As EventArgs)
//ddl2.Items.Clear()
ddl2.DataSource=New List(Of String)()
ddl2.DataSource = sql2
ddl2.DataBind() End Sub
and it worked just fine

Dropdown autopost inside updatepanel never enters code behind SelectedIndexChanged event

This is a difficult scenerio to explain, so I coded up a simple example.
<asp:UpdatePanel runat="server" ID="upTest" ChildrenAsTriggers="true"
UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true"></asp:DropDownList>
<br /><br />
In page: <asp:TextBox runat="server" ID="txtTest" Columns="50" Text="OnLoad</asp:TextBox>
<br />
<br />
<asp:Button runat="server" ID="btnTest" Text="Click it" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlTest" />
</Triggers>
</UpdatePanel>
Code-Behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
BindDropDown(Request.Form(ddlTest.UniqueID))
Else
BindDropDown(0)
End If
End Sub
Public Sub BindDropDown(val As Integer)
ddlTest.Items.Add(New ListItem("", 0))
ddlTest.Items.Add(New ListItem("One", 1))
ddlTest.Items.Add(New ListItem("Two", 2))
ddlTest.Items.Add(New ListItem("Three", 3))
ddlTest.Items.Add(New ListItem("Four", 4))
ddlTest.SelectedValue = val
End Sub
Private Sub ddlTest_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlTest.SelectedIndexChanged
txtTest.Text = "Dropdown changed"
End Sub
Private Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click
txtTest.Text = "Button clicked"
End Sub
Also to note, I have EnableViewState="false" and ClientIdMode="Static" for the page.
When I click the button the partial postback occurs and the textbox has the expected value of 'Button Clicked'
When I change the dropdown the partial postback occurs, however the textbox does not have the expected value of 'Dropdown changed' because the code in ddlTest_SelectedIndexChanged is never hit (breakpoint placement on that line of code also never hits)
In the larger scope of my project this is the crux of the problem and I can't determine why this event is never hit (the core problem is when I load a dynamic user control, the data in the control is bound correctly, but then is overridden with the pre-post data after the user control is loaded - the override is occurring somewhere in the post back events). It appears to me it has something to do with the binding of the dropdown and where it happens in the page cycle, but I haven't been able to nail anything down, nor come up with google solutions.
One odd thing I did notice when looking at the post in Firebug - the EVENTTARGET value for the dropdown was the UniqueId of the dropdown, but the EVENTTARGET value is empty for the button click. My brain is thinking there is some connection here.
FYI - I have come up with a work around that seems to do the trick. I set autopostback on dropdown to false, and using jQuery I assign the change event on the dropdown to fire the button click - which causes the postback and fires btn_click event in code.
I solved the initial issue here by moving the dropdown fill to the init event and then letting .NET load the selected value from view state
However in doing this, now the selectedindex event fires on EVERY postback.

populating a gridview with a button's click event

I have a page that has two dropdownlists(one for the locations, and the other for departments), an employee search textbox and a button. On the other page, I have a gridview. Now, what I want to achieve is that when a user types an employee's name in the textbox control, selects a location from the location dropdownlist, and a department from the departments dropdownlist, and click the button(search), the gridview on the other page must show the required information of a SINGLE employee. Only one row must show.
I have created a database for the employees. I know how to do this with the autopostback but i have not tried it using a button's click. NB: the gridview should show only one row of a selected employee. I'm using ASP.NET VB
Your help will high appreciated.
Try this
<asp:Button ID="srchButton" Text="BindData" runat="server" OnClick="BindData" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
protected void BindData(object sender, EventArgs e)
{
Gridview1.Datasource = YourDataSource;
GridView1.DataBind()
}
OK, cross-page postbacks in ASP.NET. Here we go.
Start with your search page, which we'll call search.aspx - this has your dropdownlists, textbox and button.
Employee Name: <asp:TextBox runat="server" ID="SearchTextBox" />
<br />
<asp:DropDownList runat="server" ID="LocationDropDownList">
<asp:ListItem Text="Springfield" Value="Springfield" />
<asp:ListItem Text="Shelbyville" Value="Shelbyville" />
</asp:DropDownList>
<br />
<asp:DropDownList runat="server" ID="DepartmentDropDownList">
<asp:ListItem Text="Nuclear Power" Value="Power" />
<asp:ListItem Text="Dr. Frink's Lab" Value="Research" />
<asp:ListItem Text="Mr. Burn's Office" Value="Management" />
</asp:DropDownList>
<br />
<asp:Button runat="server" ID="SearchButton" Text="Search" PostBackUrl="~/SearchResults.aspx" />
Note that the button has a PostBackUrl attribute - this is what posts the request off to the results page. We also need to change the search.aspx.designer.vb so that the dropdownlists and textbox are public properties, not protected.
Public WithEvents SearchTextBox As Global.System.Web.UI.WebControls.TextBox
The results page, which will be searchresults.aspx, has the GridView on it.
<asp:gridview runat="server" id="SearchResultsGridView" />
Now, how to handle the cross-page postback in the code. In the Page_Load event for searchresults.aspx, we check the PreviousPage property. PreviousPage could be Nothing (if, say, the user typed in searchresults.aspx directly), so if it is we'll redirect back to search.aspx. If PreviousPage is something, then we can check the IsCrossPagePostback property. If this is True, then we've probably got here from our search.aspx page (this may not be a completely valid assumption, but it's good enough for right now). If this is the case, then we can cast PreviousPage to the underlying class of search.aspx, and since we made the dropdownlist and textbox controls public, we can then access them as properties in our code here.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim employeeName As String
Dim department As String
Dim location As String
Dim searchPage As Search
If PreviousPage Is Nothing Then
Response.Redirect("search.aspx")
Else
If PreviousPage.IsCrossPagePostBack Then
searchPage = DirectCast(PreviousPage, Search)
employeeName = searchPage.SearchTextBox.Text
department = searchPage.DepartmentDropDownList.SelectedValue
location = searchPage.LocationDropDownList.SelectedValue
Call bindData(employeeName, department, location)
End If
End If
End Sub
Private Sub bindData(ByVal employeeName As String, ByVal locationName As String, ByVal departmentName As String)
With searchResultsGridView
.DataSource = 'Some code that passes the search parameters to the database
.DataBind()
End With
End Sub
As for your requirement that the search results should only show a single row, consider whether or not it is possible to have two employees with the same name, in the same department, in the same location. It might be unlikely, but I don't think it's impossible and I'm not sure you should have a restriction that you shouldn't show it. If this was, say, a payroll system, you could end up with a record you'd never be able to get to through the UI, so you'd never be able to stop paying that particular employee - probably not what you'd want!

Resources