Saturday, March 24, 2012

Dropdownlist not working within an UpdatePanel

I have a MasterPage with a ScriptManager EnablePartialRendering set to true.

On my AddDeceseadInfo.aspx page I have multipe textboxes and a dropdownlist all wrapped in a UpdatePanel. Also on the AddDeceasedInfo.aspx page (content) I have a ProxyScriptManager.

When I pick a value from the Dropdownlist the value is always "".

When I set AutoPostBack to true my ValidationControl fires and tells me the dropdownlist can not be empty which at that point it is.

When I set the AutoPostBack to false once a pick a value from the dropdownlist all the data in the dropdownlist disappears.

Can somebody help out a AJAX newbie............Cheers!

protectedvoid Page_Load(object sender,EventArgs e)

{

TextBoxFirstName.Focus();

if (!IsPostBack)

{

//Gets the languageID for the current user.

int languageID =LanguageUtility.GetLanguageIDByTwoLetterISOLanguageName();

DropDownListCauseOfDeath.DataSource = BLL.CauseOfDeath.GetCausesOfDeathByLanguageID(languageID);

//The field in the data source which provides the item text.

DropDownListCauseOfDeath.DataTextField ="CauseOfDeath";DropDownListCauseOfDeath.DataValueField ="CauseOfDeathID";

DropDownListCauseOfDeath.DataBind();

}

}

protectedvoid ButtonReset_Click(object sender,EventArgs e)

{

Utility.WebUtility.ClearAllTextBoxes(this);

}

protectedvoid ButtonSubmit_Click(object sender,EventArgs e)

{

if (Page.IsValid)

{

string firstName =HttpUtility.HtmlEncode(TextBoxFirstName.Text).Trim();

string middleName =HttpUtility.HtmlEncode(TextBoxMiddleName.Text).Trim();

string lastName =HttpUtility.HtmlEncode(TextBoxLastName.Text).Trim();

string nickName =HttpUtility.HtmlEncode(TextBoxNickName.Text).Trim();

DateTime dateOfBirth =Convert.ToDateTime(HttpUtility.HtmlEncode(TextBoxDateOfBirth.Text).Trim());

DateTime dateOfDeath =Convert.ToDateTime(HttpUtility.HtmlEncode(TextBoxDateOfDeath.Text).Trim());

int causeOfDeathID =Convert.ToInt32(DropDownListCauseOfDeath.SelectedValue);string birthPlace =HttpUtility.HtmlEncode(TextBoxBirthPlace.Text).Trim();

}

else

{

}

}

}

Set AutoPostBack to true and also set CauseValidation property to false


Set the EnableClientScript property to false on your validation control. Validators aren't supported in an UpdatePanel, this may be causing you issues,see Item #4 for more information

-Damien


I guess my post was not clear but the answers posted did not really help. My DropDownList is inside an UpdatePanel.

The ScriptManager is set to enable patial rendering.

AutoPostBack is set to true.

My UpdatePanel has UpdateMode set to Conditional

I populate the DropDownList with values from my database. When the page loads the values are there in the DropDownList. When I go and pick a value the data from the DropDownList disappears. Basically I end up with a empty DropDownList. Values gone................

When I set the AutoPostBack property to false the data stays in the DropDownList after I pick a value but when I hit the submit button the Validation control fires because the DropDownList is all of a sudden empty again.

I am running the latest Versions of ASP.Net Ajax and was under the impress the validator problems have been solved in the latest version. It seems to work on all my other froms that I use the UpdatePanel. The Validator controls and UpdatePanel work nicely together.

There seem to be no examples on the web where somebody has on DropDownList in an UpdatePanel and simple wants to get the value from that DropDownList and the additional TextBoxes once the Submit button is clicked.

I watched some of the ASP.Net videos and there are some Extenders that might be useful but it is too much work. Isn't there a why of making a simple DropDownList work within a UpdatePanel. So I can get the value from it.


How are you binding the list; are you possibly clearing it on the postback? Do you have ViewState enabled for the DropDownList?

Also, I'm using a DropDownList in an UpdatePanel successfully so it does work.

-Damien


Thanks for your reply, here is my Page_Load

protectedvoid Page_Load(object sender,EventArgs e)

{

TextBoxFirstName.Focus();

if (!IsPostBack)

{

//Gets the languageID for the current user.

int languageID =LanguageUtility.GetLanguageIDByTwoLetterISOLanguageName();

DropDownListCauseOfDeath.DataSource = BLL.CauseOfDeath.GetCausesOfDeathByLanguageID(languageID);

//The field in the data source which provides the item text.

DropDownListCauseOfDeath.DataTextField ="CauseOfDeath";DropDownListCauseOfDeath.DataValueField ="CauseOfDeathID";

DropDownListCauseOfDeath.DataBind();

}

}


If I comment if(!Ispostback) out then the form works as it should the only problem is the that the DropDownList gets bound again after I hit the submit button and therefore the the ID returned by the DropDownList is always 1. Somebody it sitting on my brain and I can't figure this out. Any ideas? Thanks, Newbie

protectedvoid Page_Load(object sender,EventArgs e)

{

TextBoxFirstName.Focus();

//if (!IsPostBack)

//{

//Gets the languageID for the current user.

int languageID =LanguageUtility.GetLanguageIDByTwoLetterISOLanguageName();

DropDownListCauseOfDeath.DataSource = BLL.CauseOfDeath.GetCausesOfDeathByLanguageID(languageID);

//The field in the data source which provides the item text.

DropDownListCauseOfDeath.DataTextField ="CauseOfDeath";DropDownListCauseOfDeath.DataValueField ="CauseOfDeathID";

DropDownListCauseOfDeath.DataBind();

//}

}


Just fixed the problem. I overrode the OnInit event like this

protectedoverridevoid OnInit(EventArgs e)

{

base.OnInit(e);

PopulateDropDownList();

}

and added a PopulateDropDownList() method

privatevoid PopulateDropDownList()

{

//Gets the languageID for the current user.

int languageID =LanguageUtility.GetLanguageIDByTwoLetterISOLanguageName();

DropDownListCauseOfDeath.DataSource = BLL.CauseOfDeath.GetCausesOfDeathByLanguageID(languageID);

//The field in the data source which provides the item text.

DropDownListCauseOfDeath.DataTextField ="CauseOfDeath";DropDownListCauseOfDeath.DataValueField ="CauseOfDeathID";

DropDownListCauseOfDeath.DataBind();

}

Set the AutoPostBack to false

EnableViewState to true

UpdatePanel to Conditional

ScriptManager EnablePartialRendering to true

And it is all working now like a charm. DropDownList works in UpdatePanel and so do the validation controls. Thanks Microsoft.....................


Thank you for posting the solution. You just made my day!

No comments:

Post a Comment