Saturday, March 24, 2012

updatepanel and dropdownlist: invalid postback

This just isnt my day, i just took a whole 15 minutes to write down my problem and the stupid site comes up wioth a oops something went wrong (we lost your post)Angry. I'll be real quick on explaining this time then...

If you look here: http://forums.asp.net/t/1112220.aspx

That's what happened to me today, I'm forced to put update panels everywhere so my data keeps displaying on postbacks. I have a drowpdownlist in an updatepanel in a formview with autopostback set to true that triggers the onselectedindexchanged event. I basically just load some info according to the selected item in a css info box there, so no big deal. If I change my selection once, it works fine, however, after that ALL postback events on my page come up with a javascript alert beginning with "Invalid postback or callback." and then tells me to register the data to the event or something like that. I have tried registering for asynchroneous postback, I tried clientscript.registerforeventvalidation... Nothing works.

I read that I had to register for all possible data that could be displayed in my dropdownlist. However, the dropdownlist content is loaded once (when the button to display the formview is clicked) and won't ever change back until the user cancels or reloads another one. so it shouldnt be a problem...

what am I missing? I don't want to turn enabledeventvalidation to false cuz the way my application is built, it would cut out validation from 80% of my pages or so, which isnt cool. I could take out ALL the update panels, but that's not really a good idea either. So please help, any input is appreciated.

Thanks a lot for your time guys.

You can turn off validation for just that one page. You don't have to turn it off for the whole site. Also the cause of this error can be a number of problems from multiple form tags ect. so check everything.


ok now thats kinda rough... I tried turning the event validation to off for just the page... and now no matter what I do, I can't seem to get my dropdownlist to fire its selectedindexchanged event anymore. I tried adding the event handler in the page load, I tried moving the handler in the code, I turned the validation back on. Nothing works anymore...

I made a small test by moving the dropdown outside of the formview. It works -_-' (ok so i get a bunch of object reference not set to an instance of an object because of how i was getting the reference to my dropdownlist in the formview (findcontrol...), but the event is fired.) Knowing it worked before this morning, even though i was getting this error, what in the world happened? I tried

protected void Page_Load(object sender, EventArgs e)
{
//...
if (MultiView1.ActiveViewIndex == 0 && fvAddEditCat.CurrentMode == FormViewMode.Insert)
{
//add the event handler to the ddl DropDownList ddl = (DropDownList)fvAddEditCat.FindControl("ddlCategories");
ddl.SelectedIndexChanged +=new EventHandler(ddlCategory_SelectedIndexChanged);
}
}

protected void ddlCategory_SelectedIndexChanged(object sender, System.EventArgs e)
{
LoadCategoryInfo();
}

protected void LoadCategoryInfo()
{ ... }


and here is the markup of the formview and the insertitemtemplate

<asp:FormView ID="fvAddEditCat" runat="server"
DataKeyNames="Data_Element_Category_Id" DataSourceID="odsDataElemCat"
DefaultMode="Edit" OnItemInserting="fv_ItemInserting" Visible="false"
OnItemCommand="fv_ItemCommand" OnItemInserted="fv_ItemInserted"
EnableViewState="False" OnItemUpdated="fv_ItemUpdated">
<InsertItemTemplate>
<table class="DataEntryForm">
<tr class="DataEntryTitle">
<td colspan="2">
Data Element Category
</td>
</tr>
<tr>
<td>
<span style="color:Red;">*</span>Category
</td>
<td>
<ajax:UpdatePanel ID="upnl2" runat="server" UpdateMode="Conditional"><ContentTemplate>
<asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="true" />
<a href="#" class="info"> ? <span class="info" style="width: 320px;">
<table>
<tr>
<td>
Description:
</td>
<td><asp:Label ID="lblCatDesc" runat="server" /></td>
</tr>
<tr>
<td>
Memo:
</td>
<td><asp:Label ID="lblCatMemo" runat="server" /></td>
</tr>
<tr>
<td>
Status Code:
</td>
<td><asp:Label ID="lblCatStatusCode" runat="server" /></td>
</tr>
<tr>
<td>
Creation Date:
</td>
<td><asp:Label ID="lblCatCreateDate" runat="server" /></td>
</tr>
</table>
</span></a>
</ContentTemplate></ajax:UpdatePanel>
</td>
</tr>
<tr>
<td>
Memo
</td>
<td>
<asp:TextBox ID="txtMemo" runat="server" TextMode="multiline" Columns="40" rows="3" text='<%# Bind("Data_Element_Category_Memo")%>'/>
</td>
</tr>
<tr>
<td style="width: 150px;"><span style="color:Red;">*</span>Status Code:</td>
<td style="width: 200px;">
<asp:TextBox ID="txtStatusCode" runat="server" Text='<%# Bind("Status_Code")%>' />
<asp:RangeValidator id="RangeValidator2" runat="server" controlToValidate="txtStatusCode"
ErrorMessage="Data Element Category Sstatus Code must be a number between 0 and 255, or nothing."
Display="Dynamic" CssClass="MsgVal" ValidationGroup="EditDEC" MinimumValue="0" MaximumValue="255" Type="Integer" />
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" controlToValidate="txtStatusCode"
ErrorMessage="Status Code must be entered."
Display="Dynamic" CssClass="MsgVal" ValidationGroup="EditDEC" />
</td>
</tr>
</table>
<asp:Button ID="btnInsert" runat="server" CommandName="Insert" Text="Insert" ValidationGroup="EditDEC" />
<asp:Button ID="btnCancel" runat="server" CommandName="Cancel" text="Cancel" CausesValidation="false" />
</InsertItemTemplate>
note that it all worked until i added that dropdownlist event and had to wrap the ddl and the label into their own updatepanel because otherwise the postback would clear the update panel it was into.

the event name is still in the onselectedindexchanged in the markup... I don't have any secondary form tags or the likes... All I have is a serious feeling that formviews are more trouble than they seem to be useful. I built a few forms with the same kind of functionality before (without the formview) and although the object assignation from all the textboxes is tedious, it works and i don't get stupid ajax errors amd i dont lose my events all the time.

I'm goign to try to remove the event handler and the update panel and rework it from scratch... i've had to do that with another control I worked on and "ajaxified", and if it still doesn't work, I'll probably just give up and use a normal form, unless someone here comes up with the miracle answer im looking for.

Thanks for trying to help.


I think I need vacations... This was too simple a change so i could think of it before now (15 minutes before I wouldve decided to go back to the normal form). Basically, I have the formview static on the page, however, its set to invisible on the first page load, and is only made visible through a button click. However, when I change the dropdownlist index, this button is not clicked again, and so, even if the formview's insert template stays there thanks to the update panel, the code doesnt think it exists. The result: the dropdownlist's event handler is nothing then too. Now, on to how I fixed it? I called the function that shows the form on the page_load when the mode is on insert.

Talk about losing my time on stupid problems... Once again, thanks for trying to help ^^ (I still don't know how it worked before, and what exactly was causing this ajax error... but it works now so lets just hope i never come accross it again)

No comments:

Post a Comment