Posts Tagged ‘ASP.NET’
Invalid Postback or Callback argument error in ASP.Net
First of all I will give a brief overview of Postback and Callback. When ever you register a control’s server side event, ASP.Net register a JavaScript which will submit the form’s details and on the server it fires the event which is registered for the control.
ASP.net register __doPostBack javascript function. Syntax for the script is __doPostBack(‘Event Target’,'Event Argument’); Here Event Target can be a control or it can be a function created by developer. You can also generate a postback script reference for a control by using GetPostBackScript function. Now ASP.Net page always handle the PostBack event by it self.
For Callback you have to impliment a ICallBackEventHandler interface. After implimenting ICallBackEventHandler a page must contain two events, RaiseCallBackEvent with return type void and GetCallBackResult with return type string. Your code logic will be contained in the RaiseCallBackEvent function code block and the result of the Callback will be contained returned by GetCallBackResult. Function GetCallBackResult will call the client side callback result function. I will explain CallBack in detail in my future post.
Many of ASP.Net users are facing a problem with invalid Postback or Callback argument error. Invalid PostBack or CallBack argument error is basically raise because of Event Validation feature. The EventValidation feature is a new feature in ASP.NET 2.0, and provides an additional level of checks to verify that a postback from a control on the client is really from that control and not from someone malicious using something like a cross-site script injection to try and manipulate things. It is part of our overall strategy of increasingly adding security in depth levels to the programming model — so that developers can be secure by default even if they forget to add security checks of their own.
Now, Invalid PostBack or CallBack argument error may occur when you are firing click event and the object is rebinding or its properties are changed in Page_Load event or someone is trying to hack into your system with cross site scripting. Each time .Net Framework render a page then it associate a unique Guid for all the controls. When binding a gridview or repeater, on each databind framework will associate a new guid for the contorl. So every time when you are firing event make sure Page_Load event does not change the control, because if the control changed the it will have a different Guid which have acutally fired the event for postback. Here are some scenario with this error.
1) Invalid Postback or Callback argument in GridView Problem may be: You are binding data in Page_Load event with either Object Data Source or Manual Binding with function call. This will make your GridView bind data on every event fire of any control. When you are firing any GridView command with OnRowCommand, before RowCommand fire your GridView will rebind and all control within it will be assigned to new id. So RowCommand could not get the item which have fired the event. for Invalid Postback or Callback argument in GridView: You can bind your data within this if condition
if (!IsPostBack)
{
//Your code for Bind data
}
This code will definitely give you solution if this not work then check whether any other control is not giving error.
2) Invalid Postback or Callback argument while submitting form. Problem may be: You have many textboxes and textareas and when user is entering “<” or “>” char then it is giving error. This is because of .Net framework is giving facility to validate request. This function will allow user not to submit script or html code directly so it is blocked. Solution for Invalid Postback or Callback argument while submitting form: You can replace this char with javascript before submitting the form replace “<” with “<” and “>” with “>” the javascript code is here
function ReplaceChar(obj)
{
//Here obj is your textbox object
var Textvalue = obj.value;
Textvalue = Textvalue.replace("<","<");
Textvalue = Textvalue.replace(">",">");
obj.value = Textvalue;
}
The other solution for both of this issue is to set enableEventValidation=false You can set this option declaration <@ Page > or even you can put this code in your web. config file in block
//set it true if you want to validate your each request. If you do not validate the request then your security of data will be decrease so this is not the perfect solution If both of this solution not work then you can contact me on my email and if you find any new solution please post it comment.
Customizing the Calendar control in ASP.NET
Recently I had to work on customizing an ASP.NET Calendar control by adding text to the day cells for my team’s Logon Message project. This proved to be not as straight-forward as I thought so here is what I did in case someone else might want to do the same.
The best place to modify the text in a cell seems to be the DayRender event handler. So the first thing I tried was to just modify the e.Cell.Text property like this:
void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
e.Cell.Text += “My text”;
}
The main problem was that after adding text to a day cell in the control I wasn’t able to select that day by clicking on the day number. Here is what you can do if you still want the select day functionality:
void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
AddTextToDayCell(e, Datetime.Today, “MyText“);
}
void AddTextToDayCell(DayRenderEventArgs e, Datetime d, string text)
{
if(e.Day.Date == d.Date)
{
string ID = ((System.TimeSpan)(e.Day.Date - new DateTime(2000, 1, 1))).Days.ToString();
e.Cell.Text = "<a href=\"javascript:__doPostBack('Calendar1','" + ID + "')\" style=\"color:#663399\">" + e.Day.DayNumberText; //assuming the name of the calendar control is Calendar1.
e.Cell.Text +=text;
}
}
If you want your new text to act as a link to some other URL, you could modify the AddTextToCell function as follows:
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
AddTextToDayCell(e, DateTime.Today, "MyText", "<a href="http://fathir.com/">http://fathir.com</a>"); //this will add the MyText link to <a href="http://fathir.com/">http://fathir.com</a> to the current day's cell
}
void AddTextToDayCell(DayRenderEventArgs e, DateTime d, string text, string URL)
{
if(e.Day.Date == d.Date)
{
string ID = ((System.TimeSpan)(e.Day.Date - new DateTime(2000, 1, 1))).Days.ToString();
e.Cell.Text = "<a href=\"javascript:__doPostBack('Calendar1','" + ID + "')\" style=\"color:#663399\">" + e.Day.DayNumberText;
e.Cell.Text += "<br /> <a href=\""+ URL + "\">" + text;
}
}
Hope you’ll find this helpful.
How to Minimize an Application to the Taskbar Tray in C#
One very convenient features in windows is the Taskbar. To make it even better, those little icons can make something so “out of the way” that you can forget what is even down there. But, sometimes you want your applications to hang down there, out of the way, doing something that requires little attention. You can even make some notification bubbles show up if you want.
Today I am going to be using Visual Studio Express 2008, and luckily this makes things really easy. I am assuming that you know how to create a new project, so once you have one open, we can get started. I named my “HideTaskBar”, but as always, any name is fine.
This whole process revolves around an object named NotifyIcon. Like most .NET objects, this one is designed to make the job easier. With it we can give our application its very own cute icon in the taskbar, and notify our users of important information. Of course, to start you need to add the object to your form, so go ahead and do that. The object is a common control.
The NotifyIcon’s Properties
The first thing you absolutely must do is set the Icon property. This can be found in the properties window when you have the NotifyIcon object selected. If you don’t, nothing will show up in the taskbar tray.
Moving on to our code, the first thing we need to do is “hide” the form when we minimize it. To do this, we simply tie the action to the resize event. This is crude at best, but for this tutorial it gets the point across:
Hide();
As I stated above, this simple code goes in the resize event of the form. We are checking to see if the form is minimized, if it is, we hide it. It’s that simple. Now we have to setup an “un-minimize” event that will show our app when we double click the icon. If you take a look at the NotifyIcon object, you will notice a DoubleClick event. How convenient, huh?
What we have to do on the event is show the form, then set its WindowState to normal:
WindowState = FormWindowState.Normal;
Again, that simple. But, we can do a little more. How about adding a some notifications? Yeap, the NotifyIcon object can do that as well. Windows calls them bubbles, and you can access them through the object. Let’s go ahead and add one to notify us of the minimization of the app:
{
Hide();
notifyIcon1.BalloonTipTitle = “APP Hidden”;
notifyIcon1.BalloonTipText = “Your application has been minimized to the taskbar.”;
notifyIcon1.ShowBalloonTip(3000);
}
This will make a balloon tip pop up and notify us from the taskbar. As you can imagine the possibilities are pretty endless as far as the NotifyIcon object goes. You can use this pretty much anywhere, so any action can have a balloon tip. In fact, no one even said you have to use the notification icon for minimizing.
Now we know what is going on.
Running multiple versions of the Framework in ASP.NET
Like any good technology, ASP.NET continues to evolve as new versions are released. But, like anything else, this brings with it a number of considerations.
Microsoft has done a great job of allowing multiple versions of the framework to run side by side. Version v1.0, v1.1 and v2.0 can all run together at the same time on the same server. Each site, or even a vdir (Virtual Directory) within a site can have its own version of the framework installed. Simply install the framework version on your server and it will be available to you. The install itself is quite small, for example the v2.0 download is 22MB.
The Microsoft homepage for the .NET Framework is:
http://msdn.microsoft.com/netframework/downloads/updates/default.aspx
There are a couple gotchas to consider with running multiple versions of the framework side by side. First, let’s dig into IIS a bit. Following is a snapshot of Windows Task Manager on an IIS5 (Windows 2000) server:

Task Manager IIS 5
Notice the 3 processes called aspnet_wp.exe. There is one per version of the framework. (v1.0, v1.1 and v2.0) If a process for a particular version of the framework doesn’t exist, as soon as it’s needed, a new process will be spun up. This allows multiple versions of the framework to live beside each other in IIS5. No effort, no pain . . . it just works.
Now consider the following IIS6 (Windows Server 2003) screenshot:

Task Manager IIS 6
Notice that there aren’t any aspnet_wp.exe processes anymore, but there are w3wp.exe processes instead. IIS6 was an impressive upgrade that brought with it some new concepts. One key new concept is Application Pools. A system administrator is able to create groups of sites and place each site in its own group. Whenever a site needs to run, a w3wp.exe process will start for its application pool if it hasn’t already started. This brings with it a number of welcome security, performance and management advantages. You are now able to specify your own Identity User which can be unique per Application Pool.
In IIS6, the aspnet_wp.exe process is done away with, and the work that it did is now done within each w3wp.exe process. This has the same advantages I mentioned previously, but it has one big gotcha!
You cannot run more than one version of the framework in the same application pool in IIS6.
While multiple versions of the framework can co-exist on the same server, they can’t co-exist in the same process. If you attempt to run multiple versions of the framework at the same time in the same process, the 2nd version that tries to run will fail with the following error:
Server Application Unavailable
The web application you are attempting to access on this web server is currently unavailable. Please hit the “Refresh” button in your web browser to retry your request.
Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.
You will also receive Event ID 1062 in Event Viewer that says:
“It is not possible to run two different versions of ASP.NET in the same IIS process. Please use the IIS Administration Tool to reconfigure your server to run the application in a separate process.”
What to do
Fortunately, the solution is easy enough. Simply create a new application pool and move the site that you will be upgrading to that pool. You can even base it off of the existing one if you don’t have the password memorized for the existing one. This is all done within IIS. Once you have placed the site or vdir in its own application pool, then you are ready to upgrade to the new framework version.
I’ll cover the different ways to move between different versions of the framework in another blog within the next few days, but the key thing to walk away with now is that multiple versions of the framework cannot co-exist in the same worker process at the same time. IIS5 didn’t have any issue with this, but IIS6 requires that each version be in its own app pool.
source : http://weblogs.asp.net/owscott/archive/2006/01/26/436607.aspx
How To: Use Regular Expressions to Constrain Input in ASP.NET
If you make unfounded assumptions about the type, length, format, or range of input, your application
is unlikely to be robust. Input validation can become a security issue if an attacker discovers that you
have made unfounded assumptions. The attacker can then supply carefully crafted input that
compromises your application by attempting SQL injection, cross-site scripting, and other injection
attacks. To avoid such vulnerability, you should validate text fields (such as names, addresses, tax
identification numbers, and so on) and use regular expressions to do the following:
- Constrain the acceptable range of input characters.
- Apply formatting rules. For example, pattern-based fields, such as tax identification numbers, ZIP Codes, or postal codes, require specific patterns of input characters.
- Check lengths.
Regular expression support is available to ASP.NET applications through the
RegularExpressionValidator control and the Regex class in the System.Text.RegularExpressions
namespace.
File Download : How To: Use Regular Expressions to Constrain Input in ASP.NET