since u r populating cboProduct in page_load inside !IsPostBack... it should not revert the selected value by user....
it might be that, once u change cboProduct and populate some other dropdown on its change event.... after that in change event of these other ddl or in some other button click event, u r redirecting to same page again using response.redirect
in that case, this will considered as fresh request(not postback) and !IsPostBack would be true... and will populate cboProduct again
hope this helps...
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
since u r populating cboProduct in page_load inside !IsPostBack... it should not revert the selected value by user....
it might be that, once u change cboProduct and populate some other dropdown on its change event.... after that in change event of these other ddl or in some other button click event, u r redirecting to same page again using response.redirect
in that case, this will considered as fresh request(not postback) and !IsPostBack would be true... and will populate cboProduct again
hope this helps...
There is no redirect in the code, no button click. Its simply a case of the dropdown reverting back to the top of the list when its fires off the function with "autopostback=true" selected.
Try to persist the selected value of the dropdown in a HiddenField and reasign the same.
I have thought of using a hiddenfield and while this could work I feel as if I shouldn't have to revert to this when I think im missing something, and/or its not functioning correctly
I still can't see any reason for the behaviour you are experiencing. If GetProductDropDown was being called multiple times your combo contents would be growing as you have no Items.Clear for cboProduct. Weird...
No, from my little experience I couldn't see why. The GetProductDropDown is definitely not being called. I've put a break point in when debugging and it never even gets to it...
JayMarvels
Member
9 Points
29 Posts
DropDown Behaviour when posting back
May 02, 2012 03:13 PM|LINK
Hi,
I'll try and explain as best as i can, I am a "noob" so please bare with me.
I have a dropdown, I have set:
<asp:DropDownList CssClass="LogCboWidth" ID="cboProduct" AutoPostBack="true" OnTextChanged="GetSaveTypeDropDown" runat="server" TabIndex="5">
</asp:DropDownList>
So a function fires off when the DropDown Changes
C# protected void GetSaveTypeDropDown(object sender, EventArgs e) { cboType.Items.Clear(); cboRetained.Items.Clear(); cboReason.Items.Clear(); RetentionEntities myentities = new RetentionEntities(); ObjectResult<SaveTypeSelect_Result> SaveType = myentities.SaveTypeSelect(Convert.ToInt32(cboProduct.SelectedValue)); cboType.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var type in SaveType) { cboType.Items.Add(new ListItem(type.SaveType.ToString(), type.SaveType.ToString())); } ObjectResult<RetainedListSelect_Result> RetList = myentities.RetainedListSelect(Convert.ToInt32(cboProduct.SelectedValue)); cboRetained.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var ret in RetList) { cboRetained.Items.Add(new ListItem(ret.Retained.ToString(), ret.Retained.ToString())); } ObjectResult<ReasonsSelect_Result> reasons = myentities.ReasonsSelect(Convert.ToInt32(cboProduct.SelectedValue)); cboReason.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var reas in reasons) { cboReason.Items.Add(new ListItem(reas.Reason.ToString(), reas.Reason.ToString())); } }I'm using the entity framework along with stored procedures.
My page load populates "cboProduct":
if (!IsPostBack) { GetProductDropDown(); .... .... .... } protected void GetProductDropDown() { RetentionEntities myentities = new RetentionEntities(); ObjectResult<ProductSelect_Result> products = myentities.ProductSelect(); cboProduct.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var product in products) { cboProduct.Items.Add(new ListItem(product.Product.ToString(), product.ProductKey.ToString())); } }The function works in the sense that when "cboProduct" changes it postbacks and runs the function, therefore populating the other dropdowns.
The issue is that the selected text in cboProduct always reverts back to the top selection when selecting one.
EG:
cboProducts may have:
Home
Motor
Pet
Travel
I chose "Pet" the other drops down populate correctly with Pet selected, but afterwards (i think once its posted back) "cboProduct" displays "Home".
I have tried to put in some breaks to try and find whats happenning with no luck.
Any assistance would be great.
kedarrkulkar...
All-Star
34545 Points
5554 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:32 PM|LINK
since u r populating cboProduct in page_load inside !IsPostBack... it should not revert the selected value by user....
it might be that, once u change cboProduct and populate some other dropdown on its change event.... after that in change event of these other ddl or in some other button click event, u r redirecting to same page again using response.redirect
in that case, this will considered as fresh request(not postback) and !IsPostBack would be true... and will populate cboProduct again
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
Bimalvv
Contributor
2356 Points
478 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:33 PM|LINK
Try to persist the selected value of the dropdown in a HiddenField and reasign the same.
Bimal
JayMarvels
Member
9 Points
29 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:35 PM|LINK
There is no redirect in the code, no button click. Its simply a case of the dropdown reverting back to the top of the list when its fires off the function with "autopostback=true" selected.
frez
Contributor
5418 Points
913 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:36 PM|LINK
JayMarvels
Member
9 Points
29 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:37 PM|LINK
I have thought of using a hiddenfield and while this could work I feel as if I shouldn't have to revert to this when I think im missing something, and/or its not functioning correctly
JayMarvels
Member
9 Points
29 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:39 PM|LINK
Here is my code in full, please do not mock me as I'm very new, in essance I've been thrown into the deep end:
Mark Up:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AfterSalesLogging.aspx.cs" Inherits="Test.Consultants.WebForm2" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> <script type="text/javascript" src="../Scripts/datetimepicker_css.js"></script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>After Sales Logging</h2> <fieldset style="width:553px; margin-left:2px;"> <legend>User & Policy Details</legend> <div class="backgroundDiv" id="PolDets" style="height:150px; width: 550px; margin-top:-15px;"> <asp:Table ID="TblLogging" runat="server" Width="390px"> <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:Label CssClass="LblLogging" ID="LblUser" runat="server" Text="User"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox CssClass="LogBoxWidth" ID="TxtUser" runat="server" Enabled="False"></asp:TextBox> </asp:TableCell> <asp:TableCell runat="server"> <asp:Label CssClass="LblLogging" ID="LblTLID" runat="server" Text="TL ID"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox CssClass="LogBoxWidth" ID="TxtTLID" runat="server" Enabled="False"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:Label ID="LblPolNum" runat="server" Text="Policy Number"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox CssClass="LogBoxWidth" ID="TxtPolNum" runat="server" TabIndex="1"></asp:TextBox> </asp:TableCell> <asp:TableCell runat="server"><asp:Label ID="LblDate" runat="server" Text="Date"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox CssClass="LogBoxWidth" ID="TxtDate" runat="server" TabIndex="2"></asp:TextBox> </asp:TableCell> <asp:TableCell><img src="/images/cal.gif" onclick="javascript:NewCssCal('<%=TxtDate.ClientID %>','ddMMyyyy','arrow',true,'24',true,'past')" style="cursor:pointer"/></asp:TableCell> </asp:TableRow> <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:Label ID="LblCallType" runat="server" Text="Call Type (IVR)"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList CssClass="LogCboWidth" ID="CboCallType" runat="server" TabIndex="3"> </asp:DropDownList> </asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="LblPartner" runat="server" Text="Partner"> </asp:Label></asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList CssClass="LogCboWidth" ID="CboPartner" runat="server" TabIndex="4"> </asp:DropDownList> </asp:TableCell> </asp:TableRow> <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:Label ID="LblProduct" runat="server" Text="Product"> </asp:Label></asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList CssClass="LogCboWidth" ID="cboProduct" AutoPostBack="true" OnTextChanged="GetSaveTypeDropDown" runat="server" TabIndex="5"> </asp:DropDownList> </asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="LblType" runat="server" Text="Save Type"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList CssClass="LogCboWidth" ID="cboType" runat="server" TabIndex="6"> </asp:DropDownList> </asp:TableCell> </asp:TableRow> <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:Label ID="LblRetained" runat="server" Text="Retained"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList CssClass="LogCboWidth" ID="cboRetained" runat="server" TabIndex="7"> </asp:DropDownList> </asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="LblReason" runat="server" Text="Reason for Call"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:DropDownList CssClass="LogCboWidth" ID="cboReason" runat="server" TabIndex="8"> </asp:DropDownList> </asp:TableCell> </asp:TableRow> <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:Label ID="LblOrgPrice" runat="server" Text="Orginal Price"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox CssClass="LogBoxWidth" ID="TxtOrgPrice" runat="server" TabIndex="9"></asp:TextBox> </asp:TableCell> <asp:TableCell runat="server"> <asp:Label ID="LblAccprice" runat="server" Text="Accepted Price"></asp:Label> </asp:TableCell> <asp:TableCell runat="server"> <asp:TextBox CssClass="LogBoxWidth" ID="TxtAccPrice" runat="server" TabIndex="10"></asp:TextBox> </asp:TableCell> </asp:TableRow> </asp:Table> <asp:Button ID="ButSub" runat="server" Text="Submit" style="margin-left:430px" /> </fieldset> </div> <asp:Label runat="server" ID="LabelError" style="color:Red"></asp:Label> </asp:Content>And C#:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Data.Objects; using System.Security; using System.Security.Principal; using System.Web.UI.WebControls; namespace Test.Consultants { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TxtDate.Text = System.DateTime.Now.ToString("G"); if (!IsPostBack) { GetBrandDropDown(); GetProductDropDown(); GetCallTypeDropDown(); TxtDate.Text = System.DateTime.Now.ToString("G"); { TxtUser.Text = User.Identity.Name.ToUpper(); String Userx = TxtUser.Text.Trim(); try { RetentionEntities myentities = new RetentionEntities(); ObjectResult<Staff_TLSelect_Result> TLID = myentities.Staff_TLSelect(Userx); Staff_TLSelect_Result TLIDfirst = TLID.First(); TxtTLID.Text = TLIDfirst.CICSID; } catch (Exception ex) { LabelError.Text = "Error grabbing TL ID, please contact MI Development. Error Message:. Error Message:" + ex.Message; } } } } protected void GetBrandDropDown() { RetentionEntities myentities = new RetentionEntities(); ObjectResult<GetBrands_Result> brands = myentities.GetBrands(); CboPartner.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var brand in brands) { CboPartner.Items.Add(new ListItem(brand.BrandName.ToString(), brand.BrandName.ToString())); } } protected void GetProductDropDown() { RetentionEntities myentities = new RetentionEntities(); ObjectResult<ProductSelect_Result> products = myentities.ProductSelect(); cboProduct.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var product in products) { cboProduct.Items.Add(new ListItem(product.Product.ToString(), product.ProductKey.ToString())); } } protected void GetCallTypeDropDown() { RetentionEntities myentities = new RetentionEntities(); ObjectResult<CallTypeSelect_Result> CallType = myentities.CallTypeSelect(); CboCallType.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var type in CallType) { CboCallType.Items.Add(new ListItem(type.CallType.ToString(), type.CallType.ToString())); } } protected void GetSaveTypeDropDown(object sender, EventArgs e) { cboType.Items.Clear(); cboRetained.Items.Clear(); cboReason.Items.Clear(); RetentionEntities myentities = new RetentionEntities(); ObjectResult<SaveTypeSelect_Result> SaveType = myentities.SaveTypeSelect(Convert.ToInt32(cboProduct.SelectedValue)); cboType.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var type in SaveType) { cboType.Items.Add(new ListItem(type.SaveType.ToString(), type.SaveType.ToString())); } ObjectResult<RetainedListSelect_Result> RetList = myentities.RetainedListSelect(Convert.ToInt32(cboProduct.SelectedValue)); cboRetained.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var ret in RetList) { cboRetained.Items.Add(new ListItem(ret.Retained.ToString(), ret.Retained.ToString())); } ObjectResult<ReasonsSelect_Result> reasons = myentities.ReasonsSelect(Convert.ToInt32(cboProduct.SelectedValue)); cboReason.Items.Add(new ListItem(string.Empty, string.Empty)); foreach (var reas in reasons) { cboReason.Items.Add(new ListItem(reas.Reason.ToString(), reas.Reason.ToString())); } } } }frez
Contributor
5418 Points
913 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 03:56 PM|LINK
JayMarvels
Member
9 Points
29 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 04:14 PM|LINK
frez
Contributor
5418 Points
913 Posts
Re: DropDown Behaviour when posting back
May 02, 2012 06:03 PM|LINK
Is there any code in the master page that might be causing it?