Databound Dropdown List Does Not Update When Control Parameters Change

I have two databound DropDownLists un an UpdatePanel that do not update when the Control Parameters change: 

<asp:UpdatePanel runat="server" RenderMode="Inline" >
	<Triggers>
		<asp:AsyncPostBackTrigger ControlID="txt_startdate" />
		<asp:AsyncPostBackTrigger ControlID="txt_enddate" />
		<asp:AsyncPostBackTrigger ControlID="ddl_status" />
		<asp:AsyncPostBackTrigger ControlID="ddl_category" />
	</Triggers>
	<ContentTemplate>
		Start Date:&nbsp;
		<asp:TextBox runat="server" id="txt_startdate" columns="10" AutoPostBack="true" Font-Size="10px" />
		<ajaxToolkit:CalendarExtender ID="ajx_startdate" runat="server" TargetControlID="txt_startdate" Format="MM/dd/yyyy" />
		<asp:CompareValidator id="cv_edt_startdate" runat="server" ControlToValidate="txt_startdate" operator="DataTypeCheck" Type="Date" ErrorMessage="Please enter a valid Start Date (mm/dd/yyyy)." EnableClientScript="False" Display="Dynamic" Text="<span <span class='boldred'>*</span>" />&nbsp;&nbsp;
		
		&nbsp;&nbsp;End Date:&nbsp;
		<asp:TextBox runat="server" id="txt_enddate" columns="10" AutoPostBack="true" Font-Size="10px" />
		<ajaxToolkit:CalendarExtender ID="ajx_enddate" runat="server" TargetControlID="txt_enddate" Format="MM/dd/yyyy" />
		<asp:CompareValidator id="cv_edt_enddate" runat="server" ControlToValidate="txt_enddate" operator="DataTypeCheck" Type="Date" ErrorMessage="Please enter a valid End Date (mm/dd/yyyy)." EnableClientScript="False" Display="Dynamic" Text="<span <span class='boldred'>*</span>" />&nbsp;&nbsp;

		&nbsp;&nbsp;Project Status:&nbsp;
		<asp:DropDownList runat="server" ID="ddl_status" AppendDataBoundItems="true" AutoPostBack="true" Font-Size="10px" DataSourceID="SqlDataSource6" DataValueField="id" DataTextField="status" >
			<asp:ListItem Value="%" Text="Select a Status..." />
		</asp:DropDownList>&nbsp;&nbsp;

		&nbsp;&nbsp;Project Category:&nbsp;
		<asp:DropDownList runat="server" ID="ddl_category" AppendDataBoundItems="true" AutoPostBack="true" Font-Size="10px" DataSourceID="SqlDataSource7" DataValueField="id" DataTextField="category" >
			<asp:ListItem Value="%" Text="Select a Category..." />
		</asp:DropDownList>&nbsp;&nbsp;
	</ContentTemplate>
</asp:UpdatePanel>

<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:cs_cfa_r %>" EnableViewState="false" >
	<SelectParameters>
		<asp:ControlParameter ControlID="txt_startdate" type="DateTime" Name="startdate" />
		<asp:ControlParameter ControlID="txt_enddate" type="DateTime" Name="enddate" />
	</SelectParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:cs_cfa_r %>" EnableViewState="false" >
	<SelectParameters>
		<asp:ControlParameter ControlID="txt_startdate" type="DateTime" Name="startdate" />
		<asp:ControlParameter ControlID="txt_enddate" type="DateTime" Name="enddate" />
	</SelectParameters>
</asp:SqlDataSource>

(The Select Commands are set in the Page_Load event). Short of manually rebuilding the DropDownLists on the TextChanged events for the Control Parameters is there a way to fix this?

Call TextChanged event of TExtBoxes and in that event call

ddl_status.DataBind() and  ddl_category.DataBind() methods

Like

<asp:TextBox runat="server" ID="txt_startdate" Columns="10" AutoPostBack="true" Font-Size="10px"
    OnTextChanged="txt_startdate_TextChanged" />

and

protected void txt_startdate_TextChanged(object sender, EventArgs e)
{
    ddl_status.DataBind();
    ddl_category.DataBind();
}

Nasser Malik

Call TextChanged event of TExtBoxes and in that event call

ddl_status.DataBind() and  ddl_category.DataBind() methods

Unfortunately, that does not work  — the contents of the DropDownList is appended to — probably because of the AppendDataBoundItems="true" parameter.

Why is the command set in page_load? Also put a breakpoint in your code to make sure the textbox(es) are in fact causing a postback.

MetalAsp.Net

Why is the command set in page_load? Also put a breakpoint in your code to make sure the textbox(es) are in fact causing a postback.

The postback is most definitely working because there is also a GridView on the page (also tied to the text boxes) and the GridView works fine.

The Command and Connection strings are set in Page_Load because the application determines which database and tables to use at run time. I should add that I did put the Connection String and the Select Command parameters in the Markup — removing them from
the Page_Load event – and I still had the same problem.

Leave a Reply