Solved

Invalid postback or callback argument. error on DetailsView control update event

Posted on 2008-10-30
2
1,421 Views
Last Modified: 2013-12-17
Hi,

I'm hoping you will be able to help me.  I have a detail view as part of a user administration system.  It takes the username as a query string and retrieves and binds a detailview to a MembershipUser when I edit the view and then click the update button, I get the following message:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ 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.

I don't understand why this is happening.  I've attached the code below
<%@ Page Language="C#" MasterPageFile="4guys.master"  Inherits="AdminBasePage" %>
 
<script runat="server">
	string username;
 
	    int totalUsers;
	MembershipUser user;
	
	private void Page_Load()
	{
		username = Request.QueryString["username"];
		if (username == null || username == "")
		{
			Response.Redirect("../users.aspx"+this.InitialiseSessionParameters());
		}
		//user = Membership.GetUser(username);
   user = this.AdminMembershipProvider.GetUser(username,false);
        System.Collections.Generic.List<MembershipUser> userlist = new System.Collections.Generic.List<MembershipUser>();
        userlist.Add(user);
		UserInfo.DataSource =userlist ;
		UserInfo.DataBind();
       		
		UserUpdateMessage.Text = user.UserName;
		
	}
 
    protected void UserInfo_ModeChanging(object sender,DetailsViewModeEventArgs e)
    {
        UserInfo.ChangeMode(DetailsViewMode.Edit);
        UserInfo.DataBind();
    }
 
	protected void UserInfo_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
	{
		//Need to handle the update manually because MembershipUser does not have a
		//parameterless constructor  
 
	    user.Email = (string)e.NewValues[0];
		user.Comment = (string)e.NewValues[1];
		user.IsApproved = (bool)e.NewValues[2];
 
		try
		{
			// Update user info:
			Membership.UpdateUser(user);
			
			// Update user roles:
			UpdateUserRoles();
			
			UserUpdateMessage.Text = "Update Successful.";
			
			e.Cancel = true;
			UserInfo.ChangeMode(DetailsViewMode.ReadOnly);
		}
		catch (Exception ex)
		{
			UserUpdateMessage.Text = "Update Failed: " + ex.Message;
 
			e.Cancel = true;
			UserInfo.ChangeMode(DetailsViewMode.ReadOnly);
			
		}
	}
 
	private void Page_PreRender()
	{
        
		// Load the User Roles into checkboxes.
		UserRoles.DataSource = this.AdminRoleProvider.GetAllRoles();
		UserRoles.DataBind();
 
		// Disable checkboxes if appropriate:
		if (UserInfo.CurrentMode != DetailsViewMode.Edit)
		{
			foreach (ListItem checkbox in UserRoles.Items)
			{
				checkbox.Enabled = false;
			}
		}
		
		// Bind these checkboxes to the User's own set of roles.
        string[] userRoles = this.AdminRoleProvider.GetRolesForUser(username);
		foreach (string role in userRoles)
		{
			ListItem checkbox = UserRoles.Items.FindByValue(role);
			checkbox.Selected = true;
		}
	}
	
	private void UpdateUserRoles()
	{
		foreach (ListItem rolebox in UserRoles.Items)
		{
			if (rolebox.Selected)
			{
                if (!AdminRoleProvider.IsUserInRole(username, rolebox.Text))
				{
                    string[] usernames = { username.ToString()};
 
                    string[] roles = { rolebox.Text };
                    this.AdminRoleProvider.AddUsersToRoles(usernames, roles);
				}
			}
			else
			{
                if (this.AdminRoleProvider.IsUserInRole(username, rolebox.Text))
				{
                    string[] usernames = { username.ToString() };
 
                    string[] roles = { rolebox.Text };
                    this.AdminRoleProvider.RemoveUsersFromRoles(usernames, roles);
				}
			}
		}
	}
 
    protected override void Render(HtmlTextWriter writer)
    {
        ClientScript.RegisterForEventValidation(UserInfo.UniqueID);
        base.Render(writer);
    }   
	private void DeleteUser(object sender, EventArgs e)
	{
		////Membership.DeleteUser(username, false); // DC: My apps will NEVER delete the related data.
		//Membership.DeleteUser(username, true); // DC: except during testing, of course!
        this.AdminMembershipProvider.DeleteUser(username, true); 
		Response.Redirect("../users.aspx"+this.InitialiseSessionParameters());
	}
 
	private void UnlockUser(object sender, EventArgs e)
	{
		// Dan Clem, added 5/30/2007 post-live upgrade.
		
		// Unlock the user.
		user.UnlockUser();
		
		// DataBind the GridView to reflect same.
		UserInfo.DataBind();
	}
</script>
 
<asp:Content ID="Content1" ContentPlaceHolderID="c" Runat="Server">
 
<!-- #include file="_nav.aspx -->
 
<table class="webparts">
<tr>
	<th>User Information</th>
</tr>
<tr>
<td class="details" valign="top">
 
<h3>Roles:</h3>
<asp:CheckBoxList ID="UserRoles" runat="server" />
 
<h3>Main Info:</h3>
<asp:DetailsView AutoGenerateRows="False"   OnModeChanging="UserInfo_ModeChanging"
  ID="UserInfo" runat="server" 
  >
  
<Fields>
	<asp:BoundField DataField="UserName" HeaderText="User Name" ReadOnly="True" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem">
	</asp:BoundField>
	<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem"></asp:BoundField>
	<asp:BoundField DataField="Comment" HeaderText="Comment" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem"></asp:BoundField>
	<asp:CheckBoxField DataField="IsApproved" HeaderText="Active User" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem" />
	<asp:CheckBoxField DataField="IsLockedOut" HeaderText="Is Locked Out" ReadOnly="true" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem" />
	
	<asp:CheckBoxField DataField="IsOnline" HeaderText="Is Online" ReadOnly="True" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem" />
	<asp:BoundField DataField="CreationDate" HeaderText="CreationDate" ReadOnly="True"
	 HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem"></asp:BoundField>
	<asp:BoundField DataField="LastActivityDate" HeaderText="LastActivityDate" ReadOnly="True" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem">
	</asp:BoundField>
	<asp:BoundField DataField="LastLoginDate" HeaderText="LastLoginDate" ReadOnly="True" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem">
	</asp:BoundField>
	<asp:BoundField DataField="LastLockoutDate" HeaderText="LastLockoutDate" ReadOnly="True" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem"></asp:BoundField>
	<asp:BoundField DataField="LastPasswordChangedDate" HeaderText="LastPasswordChangedDate"
	ReadOnly="True" HeaderStyle-CssClass="detailheader" ItemStyle-CssClass="detailitem"></asp:BoundField>
	<asp:CommandField ButtonType="button" ShowEditButton="true" EditText="Edit User Info" />
</Fields>
</asp:DetailsView>
<div class="alert" style="padding: 5px;">
<asp:Literal ID="UserUpdateMessage" runat="server">&nbsp;</asp:Literal>
</div>
 
 
<div style="text-align: right; width: 100%; margin: 20px 0px;">
<asp:Button ID="Button1" runat="server" Text="Unlock User" OnClick="UnlockUser" OnClientClick="return confirm('Click OK to unlock this user.')" />
&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Text="Delete User" OnClick="DeleteUser" OnClientClick="return confirm('Are Your Sure?')" />
</div>
 
 
<asp:ObjectDataSource ID="MemberData1" runat="server" DataObjectTypeName="System.Web.Security.MembershipUser" SelectMethod="GetUser" UpdateMethod="UpdateUser" TypeName="System.Web.Security.SqlMembershipProvider">
	<SelectParameters>
		<asp:QueryStringParameter Name="username" QueryStringField="username" />
		<asp:Parameter Name="userIsOnline"  DefaultValue="false" Type="Boolean" />
	</SelectParameters>
</asp:ObjectDataSource> 
</td>
 
</tr></table>
 
 
 
</asp:Content>

Open in new window

0
Comment
Question by:indy28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
dampsey earned 500 total points
ID: 22842247
change the following line
UserInfo.DataBind();


to
if (!Page.IsPostBack)
  UserInfo.DataBind();


I hopw this will help.

If it solves your problem, you can enlarge the if block
0
 

Author Comment

by:indy28
ID: 22842884
Thanks very much for that! It worked, but I'm getting another problem In my Updating method I'm getting an Index Out of range message where e.NewValues.Count is 0

Can you tell me why this might be happening?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question