Saturday, March 24, 2012

ASP.NET AJAX : Get SelectedValue from AJAX-populated DDL inside an UpdatePanel after PostB

I think I accidentally posted this message in the wrong section, so I'm trying again:

I have a single AJAX UpdatePanel with 2 DropDownLists. The first one is populated from aDataBind on PageLoad (works fine). When the user selects an item from the first, the OnSelectedIndex posts back, and the second DropDownList is populated through AJAX with relevant information (also works fine). This gives me aCategory > SubCategory style for the DropDowns, and everything up until then works splendidly. The problem comes when I perform a regular (not-asynch) postback (which I do to retrieve the data from all the other controls on the page), the "SelectedValue" of the second DropDownList is always empty, regardless of what was selected.

I've tried catching the value in thePage_Init,Page_Load, andPage_PreRender, and it always shows up blank. Is there no way for a regular PostBack to retrieve data that was added to the page from an Asynchronous PostBack? I've got a very long page of controls, and I don't want to add them all to an UpdatePanel just to access this single control. Perhaps I'm just doing something stupid? Gurus?

I simplified the code to the problem areas, but I can post the whole deal if necessary. Thanks in advance!

Here's my ASCX:

1<asp:ScriptManager runat="server" EnablePartialRendering="true"></asp:ScriptManager>>23<asp:UpdatePanel runat="server" ID="CategoryUpdatePanel" UpdateMode="Conditional">4<Triggers>5<asp:AsyncPostBackTrigger ControlID="fld_Category" EventName="SelectedIndexChanged" />6</Triggers>7<ContentTemplate>8<asp:DropDownList ID="fld_Category" runat="server" AppendDataBoundItems="true" DataTextField="Title" DataValueField="CategoryId" AutoPostBack="true" OnSelectedIndexChanged="Category_SelectedIndexChanged" />9<asp:DropDownList ID="fld_SubCategory" runat="server" AppendDataBoundItems="true" DataTextField="Title" DataValueField="SubCategoryId" />10</ContentTemplate>11</asp:UpdatePanel>1213<asp:Button runat="server" Text="Perform Postback" OnClick="NormalPostBack" />

And my C#:

1protected void Page_Load(object sender, EventArgs e)2{3////// All This Works Fine4fld_Category.DataSource = Categories.GetAllCategories(); fld_Category.DataBind();5}67protected void Category_SelectedIndexChanged(object sender, EventArgs e)8{9////// All this works fine too.10fld_SubCategory.DataSource = Categories.GetSubCategories( fld_Category.SelectedValue );11fld_SubCategory.DataBind();12}1314protected void NormalPostBack(object sender, EventArgs e)15{16//////// This line always returns as empty, no matter what items were in it, or what was selected.17Response.Write("SubCategory selected value is : " + fld_SubCategory.SelectedValue);18}

I would wrap the Page_Load databinding code in a !IsPostBack conditional. That is still executed, even in partial postbacks.

What's probably happening is when you submit the form, fld_Category is changed back to its default value due to the re-databinding. Then, that fires Category_SelectedIndexChanged, which rebinds fld_SubCategory based on the default value of fld_Category. Or, something along those lines.

Also, you might be interested in using the toolkit'sCascadingDropDown Extender instead of reinventing the wheel. It'll take care of all this and will be faster too, since it doesn't use partial postbacks.


In your Page_Load event you are binding the dropedownList. But on every postBack it is rebinded

So value set to default. Asgt1329a suggested write your code under if(!Page.IsPostBack)


Thanks for the tips - I gave the CascadingDropDown Extender a try some months ago but wasn't able to find too many usage examples. Maybe I'll have an easier time with it this time around. Thanks!


Okay, here's a follow up question: I've got the CascadingDropDownList up and running on a page, but I can't seem to integrate it as a User Control without receiving the "Method 500" error. I've tried calling the data population method as a Web Service, and an in-page method (on the user-control side), but both return errors. I've seen some conflicting posts about this issues. Can a CDDL be used inside a User Control? Or does it have to be explicitly tied to a page?

Thanks for your help!


What happens if you call your service method manually? Usually when you get a error 500 from the CDDL, that's just being bubbled up from the web service itself, which is throwing an error.

Okay, I've figured it out. Turns out I was disabling the ViewState is another user control on the page. Once the ViewState was back in place, everything ran peachy. Thanks for your help, and sorry for wasting your time!


hello ,

protected void Page_Load(object sender, EventArgs e)
2{
3////// All This Works Fine
4fld_Category.DataSource = Categories.GetAllCategories(); fld_Category.DataBind();
5}
insted of above code you can write like this it will work fine:- 
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
{

fld_Category.DataSource = Categories.GetAllCategories();
 fld_Category.DataTextFeild="yourcolumnname"
 fld_Category.DataValueFeild="yourcolumnname"
 fld_Category.DataBind();

 }
}
protected void fld_Category_SelectedIndexChanged(object sender, EventArgs e)
{
 fld_SubCategory.DataSource = Categories.GetSubCategories( fld_Category.SelectedValue );
fld_SubCategory.DataTextFeild="yourcolumnname"
fld_SubCategory.DataValueFeild="yourcolumnname"
fld_SubCategory.DataBind(); 
} 
paste this code in your project it will work fine
NOTE:- Set property of fld_Category:- Autopostback=true; 
 

No comments:

Post a Comment