Lions Den

The Code and Times of Hanan Schwartzberg

About Hanan | Hanan's CV | Contact Hanan

January 16, 2016

ASP.NET – Invalid postback or callback argument (An unexpected cause)

Filed under: ASP.NET,C# — Tags: , , , — Hanan Schwartzberg @ 9:20 pm

The Issue

Causing a postback (full or partial) causes the error

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.

With nothing added to the page on the client side, it’s not clear what data is causing the error.

(more…)

November 18, 2013

ASP.NET Relative Urls (~/) Not Rendering Correctly in a HyperLink ImageUrl

Filed under: ASP.NET,C# — Tags: , , , , , , , , , — Hanan Schwartzberg @ 2:47 pm

The Issue

When using a HyperLink object on masterpage the relative url assigned to the ImageUrl property is not resolving correctly on certain pages. The problem appears to only affect pages where a url rewrite has been used and the level of directories does not match the actual level of directories of the page. For instance, if the url “http://www.mysite.com/categories/book.aspx” actually loads “http://www.mysite.com/products/categories/default.aspx?type=books”, the the below hyperlink will resolve as “../products/images/bbb_logo.gif”. On all other pages it will correctly resolve as “../images/bbb_logo.gif”.

<asp:HyperLink runat="server" ID="hlBBB" Target="_blank" 
        NavigateUrl="http://www.bbb.org" ImageUrl="~/Images/bbb_logo.gif" />

Attempting to resolve the url manually in the code behind doesn’t work either. The following code results in the same incorrect url.

hlBBB.ImageUrl = ResolveClientUrl("~/images/bbb_logo.gif");

The Solution

There are two ways to work around this.

  1. This bug seems to be exclusive to the HyperLink object. Remove the image from the HyperLink and the ImageUrl will resolve correctly. For instance, the above HyperLink should be rewritten as:
    <asp:HyperLink runat="server" ID="hlBBB"
        NavigateUrl="http://www.bbb.org">
        <asp:Image runat="server" ID="imgBBB" ImageUrl="~/Images/bbb_logo.gif" />
    </asp:HyperLink>
  2. If you really want to leave the image in the HyperLing object. You can work around the problem in the code behind. The ResolveClientUrl method doesn’t work because takes into account the page’s location in the site, which between the rewrite and the actual location of the page seems to be inaccurate. Instead use VirtualPathUtility.ToAbsolute to resolve the url. This method can be used anywhere on the site, not just on pages and user controls and will resolve the url with an absolute path (“/images/bbb_logo.gif”).

October 19, 2010

Visual Studio 2008 – ASP.NET “System.Runtime.InteropServices.COMException”

Filed under: ASP.NET,IIS7 — Tags: , — Hanan Schwartzberg @ 4:01 pm

The Issue

When openning an existing ASP.NET project for the first time in Visual Studio 2008 it returns the error “System.Runtime.InteropServices.COMException” without any HRESULT code. The problem I experienced was specificly on a Windows 7 machine, but the same solutions should work on Vista as well.

System.Runtime.InteropServices.COMException

(more…)

February 17, 2010

IIS7 Sessions Getting Crossed / Mixed Up / Copied

Filed under: ASP.NET,IIS7 — Tags: , , — Hanan Schwartzberg @ 10:25 pm


The symptom

Users reported seeing data that did not belong to them when they logged into their accounts. Logs showed that nearly 10% of our users were being assigned copies of other users’ sessions. The entire session object was copied, including the sessionVariables and sessionID. Once duplicated the individual sessions could be modified without affecting the other (i.e. abandoning one did not kill the other).

To spot instances of the problem I stored the Request.RemoteHost (The IP address of the computer making the request) in a sessionVariable. At the beginning of each request I checked to make sure the session’s IP matched the current request’s IP.


What is was not

It is possible that the IP would change naturally, most simply if a user reset his router/modem. This was not the case because it was happening way too often. Further, there were confirmed instances of one user’s data being crossed with another’s. Finally, some of the pairs of IPs weren’t just on different computer, but were in different countries.

It is also possible, and was often suggested, that sessionVariables can stop being unique if they are used with static/shared variables within the WebApp. This was not the case either, because the IP address I stored in the session was only written to once, from the request and afterwards was only read to compare back to the request. This was also ruled out because the SessionID was also duplicated and that is a read only value.


What is was

It is a feature/bug in IIS7. This latest version of IIS introduced some new caching features.

  1. IIS7 automatically caches static content, such as HTML pages, images, and style sheets.
  2. IIS7 now has the ability to cache dynamic content as well.

Caching dynamic content is great if it is a page such as a dynamically generated image gallery, or a page that is generated dynamically based on the browser’s culture. However, in this thread, http://forums.iis.net/t/1154347.aspx?PageIndex=2, Anil Ruia, a Senior Software Design Engineer on the IIS Core, explains, “You should not be enabling output caching for any response which depends on session state.”

If the page generating the content depends on the session state, it caches the session object along with the rest of the page. The next user to come through ends up pulling the cached session, instead of getting a new one. When I checked our settings I found it was set to cache all .aspx pages for three minutes, including many pages that access the session.


The solution

Edit Cache Rule

In IIS7, disable the caching for .aspx pages in any directory with an asp.net page that depends on the session state. To do this:

  1. Run the Server Management console.
  2. Navigate to Roles -> Web Server (IIS) -> Internet Information Services.
  3. Select the site you wish to modify.
  4. Select the folder that contains the .aspx pages you need to turn caching off for.
  5. In the Feature View, double-click “Output Caching”.
  6. If there is a rule there already for the .aspx extension double click it. Otherwise right click and select “Add…”
  7. Enter .aspx for the “File name extension”
  8. Check “User-mode caching”
  9. Select “Prevent all caching”
  10. Check “Kernel-mode caching”
  11. Select “Prevent all caching”
  12. Click OK
  13. Close the Server management Console

September 10, 2009

CalendarExtender Plus 1.0.2.0

Click Here to download the latest version of CalendarExtender Plus and documentation.

The CalendarExtender Plus is a modification of the calendar extender that ships as part of the Ajax Control Toolkit. There are two main features added to the control. First, you can limit the selectable dates to a specified date range. Second, there is now the option to show an alternate header. The new header includes two DropDown Lists, one to select the month and one to select the year.

(more…)

August 25, 2009

Show a Wait Message While Waiting for Postback to Complete

Filed under: ASP.NET,Client Side — Tags: , , , , — Hanan Schwartzberg @ 10:13 am

Scenario

Among the controls on a page is an update panel with a button that when clicked causes a partial postback that takes at least a few seconds. During this time you want to show the user a wait message that will disappear as soon as the postBack completes.

(more…)

August 12, 2009

Default a TabContainer to No Tabs Selected

Question

Under normal circumstances when a page loads with a tabContainer on it one tab must be selected. The selected tab can be specified via the ActiveTabIndex property. What if the tabContainer needs to default to an empty tab with none of the tabs selected?

Solution

Add a dummy tabPanel to the tabContainer and leave it blank. Use the pageLoad JavaScript function to hide the tab’s header

<cc1:TabContainer ID="TabContainer1" runat="server" 
    Height="200px" Width="200px" ActiveTabIndex="0">
    <cc1:TabPanel runat="server" ID="tpDummy" HeaderText="">
        <ContentTemplate>
        </ContentTemplate>
    </cc1:TabPanel>
    <cc1:TabPanel runat="server" ID="tpName" HeaderText="Name">
        <ContentTemplate>
            Name:
            <asp:TextBox runat="server" ID="txtName" />
        </ContentTemplate>
    </cc1:TabPanel>
    <cc1:TabPanel runat="server" ID="tpEmail" HeaderText="Email">
        <ContentTemplate>
            Email:
            <asp:TextBox runat="server" ID="txtEmail" />
        </ContentTemplate>
    </cc1:TabPanel>
</cc1:TabContainer>
function pageLoad(sender, e)
{
    var objTpDummy = 
        document.getElementById('<%= tpDummy.ClientID %>' + '_tab');
    objTpDummy.style.display = 'none';
}

August 10, 2009

Show a Wait Message While Generating a File for Download

Filed under: ASP.NET,C# — Tags: , , , , — Hanan Schwartzberg @ 1:31 pm

Scenario

A page has a list of images for the user to choose from. When a file is selected the request is processed via an AJAX call that directs the source of an iFrame to another page where the image is generated and passed back to the client.

Due to the size of some of the images, the generation can take a noticeable amount of time. To make sure the user does not think the site is frozen a wait message is needed. Since the generating page only returns the image, we need another way to notify the client when the file is generated and the wait message can be hidden.

(more…)

June 16, 2009

Introducing CalendarExtender Plus

Maximum Date - May 15th, 2009
Click Here to download the latest version of CalendarExtender Plus and documentation.

The CalendarExtender Plus is a modification of the calendar extender that ships as part of the Ajax Control Toolkit. There are two main features added to the control. First, you can limit the selectable dates to a specified date range. Second, there is now the option to show an alternate header. The new header includes two DropDown Lists, one to select the month and one to select the year.

The available dates can be limited using the MaxDate and MinDate properties.

<cc1:CalendarExtenderPlus runat="server" ID="cepExample" 
    TargetControlID="txtExample" 
    MaximumDate="01/01/2009" MinimumDate="5/15/2009 />

Setting the ShowDdlHeader to true will show the heading with dropDownLists to select the month and year.

<cc1:CalendarExtenderPlus runat="server" ID="cepHeaderExample" 
    TargetControlID="txtExample" ShowDdlHeader="true" />

DropDownList

The Calendar Extender Plus has a few other enhancements, including the ability to show or hide the arrows in the heading to move back and forth a month, and a property to show or hide today’s date at the bottom of the calendar.

Since the CalendarExtender Plus is a modification of the standard extender it has all the properties and functionality of the original. It can be skinned using the normal css, and automatically fills the textBox with no need for additional javascript.

The CalendarExtender Plus is free to download, use, and distribute with your project. Any feedback is greatly appreciated!

June 7, 2009

ComboBox ClientSide onChange Event

Filed under: Ajax Control Toolkit,ASP.NET,C#,Client Side — Tags: , , , , , , — Hanan Schwartzberg @ 4:57 pm

The Issue

The comboBox has a couple of server side events to deal with field changing.

  • OnSelectedIndexChanged
  • OnTextChanged

How can we catch the comboBox changing on the client side?

The Solution

Without access to these function on the client side, we need to use the client side events of the textBox that is contained within the comboBox. As with any textBox this one also has the onChange event, but the event does not run when the text was changed and the focus moved to another field. There are a few options:

  • onKeyPress
  • onKeyUp
  • onBlur

The problem with onKeyPress is when it gets to the javascript function the value of the textBox is has not changed yet, leaving onKeyUp and onBlur. While onKeyUp will give the desired effect when the user presses a key on the keyboard, part of the point of a comboBox is the ability to use the mouse, which will not be captured by this event, leaving onBlur as the remaining option. It’s still not perfect, since the event won’t run until the focus is moved off the comboBox, but it appears to be the best option. In some cases you may want to use both onKeyUp and onBlur to get the best of both events, but be careful because when the user uses the keyboard and the moves the focus your event will run twice.

In the example below I have a comboBox named cmbExample and a javascript function that accepts the textBox calling it as a parameter, cmbExample_OnBlur(textBox). I add the onBlur event to the textBox in the Page_Load method in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
    TextBox textBox = cmbExample.FindControl("TextBox") as TextBox;
 
    if (textBox != null)
    {
        textBox.Attributes.Add("onBlur", "cmbExample_OnBlur(this);");
    }
}
Older Posts »
Home | Site Design | Banner Design | Code Den | Offsite Posts | Downloads | Photography | About Hanan | Hanan's CV | Contact Hanan
Copyright © 2009 Hanan Schwartzberg. All rights reserved.