Last post Jan 21, 2017 04:33 AM by Cathy Zou
Jan 10, 2017 07:17 PM|0belix|LINK
I have a masterpage that contains a DropDownList that gets populated dinamically.
There is no issue with the initial binding of values to it, but when i change its selected value and the page gets posted back, all the values disabear leaving it empty.
Here is the markup:
<asp:DropDownList ID="myDDL" DataTextField="Title" DataValueField="Id" CssClass="change" runat="server" AutoPostBack="true" />
And this is the VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack() Then
End IfEnd Sub
Private Sub LoadDDLValues()
'--- my database call goes here along with the binding of the "allValues" DataTable
myDDL.DataSource = allValues.DefaultView
***Edited the name of the Sub which was different because of the decaracterization needed to post here on the forum***
Jan 10, 2017 08:02 PM|Nilishere|LINK
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack() Then
LoadDDLValues() // only bound the dropdown if it is not a post back
This is because each time you are changing the value in dropdown autopostback occurs, which means the page load function is called again. Inside the function you are binding the dropdown only at the initial page load and not in every post back, hence the
values are not bound to the dropdown. How are you calling the function LoadActiveCurrencies()
because in the page_load i can see you are using a different function name. Are you trying to call the function on selectedindexchanged event of dropdown?
Jan 11, 2017 12:52 AM|oned_gk|LINK
ReBinding ddl will reset ddl items and selection.
Seem like you rebind the ddl in postback.
The empty ddl items because new datasource is empty.
Jan 11, 2017 01:10 AM|markfitzme|LINK
You're only binding on initial load. Remove your postback conditional check and see if that helps.
Jan 11, 2017 10:48 AM|0belix|LINK
Thx for the replies guyz,
First off all, let me inform that the Sub name was mistakenly left the original, and i have now decaracterized it to match the call made on the page load.
Second, if i let the DDL be rebound upon postback, i'll lose the selected value, so i can only bind it on first load. It should retain the values initialy bound, so that the selected item also remains selected upon posting the page via the autopostback property
allowing me to capture the selected value afterwards.
Jan 11, 2017 11:11 AM|Cathy Zou|LINK
From your description, I make a sample .
However, everything work well:
Code as below:
Master page named DDLMasterPage:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="DDLMasterPage.master.cs" Inherits="WebApplication1.Test.DDLMasterPage" %>
<asp:ContentPlaceHolder ID="head" runat="server">
<form id="form1" runat="server">
<asp:DropDownList ID="myDDL" DataTextField="Title" DataValueField="Id" CssClass="change" runat="server" AutoPostBack="true"></asp:DropDownList>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
protected void Page_Load(object sender, EventArgs e)
protected void LoadActiveCurrencies()
DataTable table = new DataTable();
myDDL.DataSource = table;
<%@ Page Title="" Language="C#" MasterPageFile="~/Test/DDLMasterPage.Master" AutoEventWireup="true" CodeBehind="DropDownList on MasterPage.aspx.cs" Inherits="WebApplication1.Test.DropDownList_on_MasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
Output screenshot as below:
Jan 11, 2017 11:51 AM|0belix|LINK
Thx for the reply.
Yes, your example matches my code perfectly, the only main difference is that i'm using a website and your using a webapplication, on the rest, its seems pretty close. However, on my side, it is not working properly as it is on yours, and as is should :/
I also have an event and event handler on the masterpage, but its got nothing to do with the DDL in question, so i would think its got no influence.
Jan 11, 2017 01:11 PM|0belix|LINK
I managed to solve this.
Had to move the call to the LoadDDLValues() sub into the Page_Init event from the Page_Load and then also remove the page.isPostBack
Thx all for the help
Jan 21, 2017 04:33 AM|Cathy Zou|LINK
Congratulations and thanks for your sharing.