Wednesday, March 28, 2012

PostBack Problem, Need Help.

I have a page with a drop down that drives a gridview. Now whatever is selected the drop down will drive whats shown in the gridview. The page loads fine and the default value in the drop down loads the gridview fine. Now on each row in the gridview I have an edit button to edit that row. I use a modal popup to edit that row and save it. The modal works great and updates a row. Here is where the problem happens, if I change the drop down to a different value the gridview updates, I edit the row and the modal pop is ok and saves...but without changing the drop down again if I try to edit another row I get this error...

--------
Windows Internet Explorer
--------
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@dotnet.itags.org. Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
--------
OK
--------

Now I have tried adding that attrbute to the page tag and it still does not work...it gives me a similar error than the above one. I assume it has something to do with having all these postbacks and the modal is causing one while so is the edit button and they are conflicting somehow...anyway, any direction or solution would great. Let me know if anyone needs to see some code.

Hi Horizon,

In your modal popup, do you make partial or full postback when you update the datagrid row?

If you make partial postback, there is chance the server does not know all about this partial update, especially if you modify or update portions of your page, so by the next time you try to issue partial postback the server will complain about invalid postback event arguments.

It works if you change the dropdown because the dropdown issues full postback so the server now knows about all modifications on your page.

Is it an option to issue a full postback when you update the rows in the datagrid?

If your application does not require high grades of security, you could turn off the enableEventValidation attribute globally for your application (in web.config file).

Probably some ASPX and code-behind code would be helpful here to see how exactly you do the postbacks and updates on your page...

Kind regards,

sbogus.


Thanks you lead me on the right path, I added this to the load event...

ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnSaveRankings);

What this does is cause a full post back on the page now when I click btnSaveRankings which is in my modal panel. Though know its not saving state on my drop down...it reverts it back to the value as if you just came into the site...I am going to post my aspx and aspx.cs file...

<%@. Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="PowerRankingsAjax.admin.index" %><%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Add/Edit Power Rankings</title> <link href="../ModalDialogs.css" rel="stylesheet" type="text/css" /></head><body> <form id="form1" runat="server"> <div> <asp:Label ID="lblError" runat="server" Text=""></asp:Label> <ajaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxControlToolkit:ToolkitScriptManager> Edit Past Issues <br /> Select Issue: <asp:DropDownList ID="ddlIssue" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlIssue_SelectedIndexChanged"></asp:DropDownList> <br /> <asp:UpdatePanel ID="uEditPanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="EditPowerRankings" runat="server" AutoGenerateColumns="false" OnRowCreated="EditPowerRankings_OnRowCreated" > <Columns><asp:TemplateField ShowHeader="False"><ItemTemplate> <asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="btnEdit_Click" CausesValidation="false" /></ItemTemplate></asp:TemplateField><asp:BoundField DataField="pid" /><asp:BoundField DataField="teamabbr" /><asp:BoundField DataField="team" HeaderText="Team" SortExpression="team" /><asp:BoundField DataField="rank" HeaderText="Rank" SortExpression="rank" /><asp:BoundField DataField="comment" HeaderText="Comment" SortExpression="comment" /></Columns> </asp:GridView><!--cheesy button for the modal popups target control--> <asp:Button ID="btnHiddenRankings" runat="server" style="display: none" /> <ajaxControlToolkit:ModalPopupExtender ID="mpeRankings" runat="server" TargetControlID="btnHiddenRankings" PopupControlID="pnlRankings" CancelControlID="btnCancelRankings" BackgroundCssClass="modalBackground" Drag="true" > </ajaxControlToolkit:ModalPopupExtender> <asp:Panel ID="pnlRankings" runat="server" CssClass="modalBox" Style="display: none;" Width="500px"><asp:Panel ID="RankingsCaption" runat="server" CssClass="caption" Style="margin-bottom: 10px;">Edit Rankings</asp:Panel> <asp:HiddenField ID="hidRankEditIndex" runat="server" Value="-1" /><div class="divCol"></div><div class="divColLast"> <asp:Image ID="imgTeam" runat="server" />  <asp:Label ID="lblTeamName" runat="server" Text=""></asp:Label></div><div class="clearer"></div><div class="divCol">Rank:</div><div class="divColLast">   <asp:DropDownList ID="ddlEditRank" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> <asp:ListItem>7</asp:ListItem> <asp:ListItem>8</asp:ListItem> <asp:ListItem>9</asp:ListItem> <asp:ListItem>10</asp:ListItem> <asp:ListItem>11</asp:ListItem> <asp:ListItem>12</asp:ListItem> <asp:ListItem>13</asp:ListItem> <asp:ListItem>14</asp:ListItem> <asp:ListItem>15</asp:ListItem> <asp:ListItem>16</asp:ListItem> <asp:ListItem>17</asp:ListItem> <asp:ListItem>18</asp:ListItem> <asp:ListItem>19</asp:ListItem> <asp:ListItem>20</asp:ListItem> <asp:ListItem>21</asp:ListItem> <asp:ListItem>22</asp:ListItem> <asp:ListItem>23</asp:ListItem> <asp:ListItem>24</asp:ListItem> <asp:ListItem>25</asp:ListItem> <asp:ListItem>26</asp:ListItem> <asp:ListItem>27</asp:ListItem> <asp:ListItem>28</asp:ListItem> <asp:ListItem>29</asp:ListItem> <asp:ListItem>30</asp:ListItem> </asp:DropDownList></div><div class="clearer"></div><div class="divCol">Comments:</div><div class="divColLast"> <asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" MaxLength="500" Width="480" Height="200"></asp:TextBox></div><div class="clearer"></div><div style="white-space: nowrap; text-align: center;"> <asp:Button ID="btnSaveRankings" runat="server" Text="Save" OnClick="btnSaveRankings_Click" CausesValidation="true" /> <asp:Button ID="btnCancelRankings" runat="server" Text="Cancel" CausesValidation="false" OnClick="btnCancelRankings_Click" /></div></asp:Panel> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="ddlIssue" EventName="SelectedIndexChanged" /> </Triggers> </asp:UpdatePanel> </div> </form></body></html>

cs file...

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace PowerRankingsAjax.admin{public partialclass index : System.Web.UI.Page { powerrankings oPowerrankings =new powerrankings(); DataSet ds =new DataSet();public String strWeek;public int intCountVal;protected void Page_Load(object sender, EventArgs e) { ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnSaveRankings);if (oPowerrankings.ConnectToDatabase(lblError) ==true) {if (!Page.IsPostBack) {//GetWeek GetWeek(); BuildEditList(); BuildEditGrid(strWeek); } } ds.Dispose(); }protected void GetWeek() {//Current Week strWeek = oPowerrankings.GetMaxWeek(ds, lblError); }protected void BuildEditList() { ddlIssue.DataSource = ds.Tables[oPowerrankings.GetAllWeeks(ds, lblError).ToString()]; ddlIssue.DataTextField ="week"; ddlIssue.DataValueField ="week"; ddlIssue.DataBind(); intCountVal = oPowerrankings.GetHolder(ds, lblError);if(intCountVal > 0) { ListItem li =new ListItem("Edit","edit"); ddlIssue.Items.Add(li); }else { ListItem li =new ListItem("New","new"); ddlIssue.Items.Add(li); } lblError.Text = intCountVal.ToString();//ddlIssue.SelectedValue = strWeek; }protected void BuildEditGrid(string strWeek) { EditPowerRankings.DataSource = ds.Tables[oPowerrankings.GetEditPowerRankings(strWeek, ds, lblError).ToString()]; EditPowerRankings.DataBind(); }protected void ddlIssue_SelectedIndexChanged(object sender, EventArgs e) { strWeek = ddlIssue.SelectedValue;if (oPowerrankings.isNumeric(strWeek, System.Globalization.NumberStyles.Integer)) { BuildEditGrid(strWeek); }else { } }protected void EditPowerRankings_OnRowCreated(object sender, GridViewRowEventArgs e) { e.Row.Cells[1].Visible =false; e.Row.Cells[2].Visible =false; }protected void btnSaveRankings_Click(object sender, EventArgs e) { oPowerrankings.UpdatePowerRankings(Convert.ToInt16(hidRankEditIndex.Value), Convert.ToInt16(ddlEditRank.SelectedValue), txtComment.Text, lblError); mpeRankings.Hide(); strWeek = ddlIssue.SelectedValue; BuildEditList(); BuildEditGrid(strWeek); uEditPanel.Update(); }protected void btnCancelRankings_Click(object sender, EventArgs e) { mpeRankings.Hide(); }protected void btnEdit_Click(object sender, EventArgs e) { Button btnEdit = senderas Button; GridViewRow row = (GridViewRow)btnEdit.NamingContainer; hidRankEditIndex.Value = row.Cells[1].Text; imgTeam.ImageUrl ="../images/" + row.Cells[2].Text +".gif"; lblTeamName.Text = row.Cells[3].Text; ddlEditRank.SelectedValue = row.Cells[4].Text; txtComment.Text = row.Cells[5].Text; uEditPanel.Update(); mpeRankings.Show(); } }}

No comments:

Post a Comment