Monday, May 19, 2008

Invalid postback or callback argument.

Getting an Invalid postback or callback argument can be a very frustrating issue. Especially when most of the articles out there discuss potential issues that are often irrelevant.

The error is caused because the page loading process gets interrupted. The interruption can be caused by an event being called prematurely. What then happens is that the salt hashes that protect your page from injection attacks are not generated and when the page goes to read them, they can't and throw the famous error.

This error can also be thrown when content on your page get changed on the client end and no longer reflects the salt hashes created by the server)

The following solution registers each control and has been reported to work with both regular asp.net sites as well as Ajax enabled sites:

 protected override void Render(HtmlTextWriter writer)
{
Register(this);
base.Render(writer);
}
private void Register(Control ctrl)
{
foreach (Control c in ctrl.Controls)
Register(c);
Page.ClientScript.RegisterForEventValidation(ctrl.UniqueID);
}

I placed my copy of the above in my master page's code behind.

Source: http://forums.asp.net/t/922994.aspx?PageIndex=10

As a side note, I think this may have solved one of my issues, but the particular code I am currently testing has other issues...  which may just be that I am testing locally and running out of memory...???  will write more when I figure it out or if I have different results on my server.  I am currently working on a project that is rather data intensive!

Friday, May 16, 2008

filtering data in templatefields using a code behind method

I needed to some quick and dirty filtering in a gridview (to manage html input radio buttons). After a few small headaches and attempts at different angles, I came up with the following. The point to remember is that the Eval("variableFromObjectDataSource") is an object datatype by default and if it is going to be used as a parameter it needs to be typed as a string (as that is what my method requires)

The following line would go in a template field:
<%# myFunction(Eval("variableFromObjectDataSource").ToString()) %>

and then the following method in the code behind:
protected string myFunction(string Variable)
{
string returnString = String.Empty;
returnString = Variable; // do something with the variable
return returnString;
}

Wednesday, April 30, 2008

A little note on setting Session Variables

If you get the following error message when trying to set a session variable...

"An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session.get'"
...make sure that the method is not 'static'

Thursday, April 17, 2008

Using custom methods to Populate DataGridViews

The software I am building uses gridviews to display lists of data. Until recently I have been using a SqlDataSource and on occasion an ObjectDataSource to populate the data into these lists.

My company wants to move to a tiered architecture (separation of the different layers to different physical machines) To do this they are implementing a web service to get and set data.

My task is to take that data and to integrate it with the existing application. Shouldn't be too hard to do and it gives me the opportunity to really explore several different methods for manipulating data.

One of my first tasks is being sure to understand their existing system which uses the DataGridView to display a simple rows of data.


I have simplified this GridView to reduce the clutter.

The key pieces to note in the GridView are:
  • DataSourceID="ObjectDataSource1"
    • This is the object that goes out and retieves the data you are seeking to display
  • AutoGenerateColumns="false"
    • If your object only shows the elements you want to show, then setting AutoGenerateColums to true will allow you to ignore setting up columns altogether. However if you are seeking to only create a GridView with a subset of those colums this will need to be set to false
  • asp:BoundField column elements (note the DataField="LastName" attribute)
    • The ObjectDataSource will call a method that will return an ArrayList of an object. This object will need to be coded and the elements of that object can be entered into the DataField attribute to map them accordingly in the GridView.

runat="server"
DataSourceID="ObjectDataSource1"
AutoGenerateColumns="false" >






The key pieces to note in the GridView are:
  • TypeName="UserBL"
    • refers to the class name that contains the SelectMethod
  • SelectMethod="GetUsersName"
    • The method that will return an ArrayList of an object

ID="ObjectDataSource1"
runat="server"
SelectMethod="GetUsersName"
TypeName="UserBL">




I will also be researching using an XML Data Source as that may simplify my processing...

note that this post needs to be cleaned up and tested to ensure that blogger included all of the necessary code elements...

Friday, April 11, 2008

DropDownList resources

The following lists several code samples on loading data into a dropdownlist control
http://www.devcity.net/Articles/68/1/aspnet_dropdown.aspx

The following article menthions pass-through attributes.
http://www.ironspeed.com/articles/Using%20Pass%20Through%20Attributes%20in%20Generated%20Applications/Article.aspx