Jon's Blog

.NET Development & More

ASP.NET: Fun with Paths and Directories

clock August 19, 2010 15:02 by author Jon

In the following table the page executing the code is at the following physical location on my machine:

C:\Users\me\Documents\Visual Studio 2010\Projects\TestStuff\TestStuff\Nested\Paths.aspx

Property/Method and Description Result
Request.ApplicationPath

Gets the root virtual path.

/TestStuff
Request.CurrentExecutionFilePath

Gets the virtual path to the current file.
Same as Request.Path and Request.FilePath.

/TestStuff/Nested/Paths.aspx
Request.CurrentExecutionFilePathExtension

Gets the file extension of the current file.

.aspx
Request.FilePath

Gets the virtual path to the current file.
Same as Request.CurrentExecutionFilePath and Request.Path.

/TestStuff/Nested/Paths.aspx
Request.MapPath("")

Gets the full physical path to the current directory. Seems to be the same as Server.MapPath("").

C:\Users\me\Documents\Visual Studio 2010\Projects\TestStuff\TestStuff\Nested
Request.Path

Gets the virtual path to the current file.
Same as Request.CurrentExecutionFilePath and Request.FilePath.

/TestStuff/Nested/Paths.aspx
Request.PhysicalApplicationPath

Gets the physical path to the root directory.
Seems to be the same as AppDomain.CurrentDomain.BaseDirectory.

C:\Users\me\Documents\Visual Studio 2010\Projects\TestStuff\TestStuff\
Request.PhysicalPath

Gets the full physical path to the current file.

C:\Users\me\Documents\Visual Studio 2010\Projects\TestStuff\TestStuff\Nested\Paths.aspx
AppDomain.CurrentDomain.BaseDirectory

Gets the physical path to the root directory.
Seems to be the same as Request.PhysicalApplicationPath.

C:\Users\me\Documents\Visual Studio 2010\Projects\TestStuff\TestStuff\
Server.MapPath("")

Gets the full physical path to the current directory.
Seems to be the same as Request.MapPath("").

C:\Users\me\Documents\Visual Studio 2010\Projects\TestStuff\TestStuff\Nested

 

Also, if you are joining a directory to a file/path I recommend using the Path.Combine method found in the System.IO namespace.  Here is an example:

using System.IO;

string fileName = "MyFile.pdf";
string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory
+ fileName);


Hiding ASP.NET Ajax Modal Popup Dialog Using JavaScript

clock July 16, 2010 13:22 by author Jon

Following is an easy way to hide the modal popup dialog extender from the AJAX control toolkit using JavaScript.  The key is to set a BehaviorID on the ModalPopupExtender.  Then you can use this ID to call the hide() method via Javascript like this:

function HideModal() {
  $find('modalPopupBehavior').hide();
}

In the full example below I am closing the modal popup dialog box when the user clicks on a link.

Full ASPX example:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtk" %>

<!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>My Page</title>
    <link href="css/Test.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="aspNetForm" runat="server">
    
<asp:ScriptManager ID="scriptMan" runat="server" />

<script type="text/javascript" language="javascript">
    function HideModal() {
        $find('modalPopupBehavior').hide();
    }
</script>

<asp:UpdatePanel ID="upnlMain" runat="server">
    <ContentTemplate>
    
        <div>
            <ajaxtk:ModalPopupExtender ID="modalPopup" runat="server" 
                BehaviorID="modalPopupBehavior"
                TargetControlID="btnPopup" PopupControlID="pnlPopup">
            </ajaxtk:ModalPopupExtender>
            
            <asp:Button ID="btnPopup" runat="server" Text="Show Popup" />
            
            <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" 
                style="width: 715px; display: none;">
                <div>
                    <p>
                    <a href="http://www.jonathanjungman.com/blog/" 
                        target="_blank" onclick="HideModal()">Hide Modal</a>
                    </p>
                </div>
            </asp:Panel>
        </div>
    
    </ContentTemplate>
</asp:UpdatePanel>
   
</form>
</body>
</html>


ASP.NET AJAX: UpdateProgress Displaying Behind DropShadowExtender Control

clock June 15, 2010 10:39 by author Jon

I ran into an issue the other day where my UpdateProgress control (an animated GIF) would display behind any div that was using the DropShadowExtender control from the AJAX Control Toolkit.  In order to fix this issue I set the CSS class on the div of the UpateProgress to "postion: fixed" and the "z-index" to an arbitrarily high number.

This is before the CSS change.  Notice the animated GIF just peeking out from the end of the drop shadow at the bottom right of this image.

And here is what it looks like after the CSS change.  The animated GIF now displays properly in front of the div using the drop shadow.

CSS:

div.divProgress { z-index: 1001; position: fixed; }

ASPX:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtk" %>

<ajaxtk:DropShadowExtender id="dse" runat="server" TargetControlID="divPanel"
TrackPosition="true" />
<div id="divPanel" runat="server" class="Box">
<div class="BoxHeader">This Is My Header</div>
<div>
<p>Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.
Text here. More Text. Whoa. Text here. More Text. Whoa. Text here. More Text. Whoa.</p>

<p>
<asp:Button ID="btnDoSomething" runat="server" Text="Do Something"
onclick="btnDoSomething_Click" />
</p>
</div>
</div>

<ajaxtk:AlwaysVisibleControlExtender ID="AlwaysVisibleControlExtender" runat="server"
TargetControlID="updateProgress" HorizontalSide="Center" VerticalSide="Middle" />
<asp:UpdateProgress ID="updateProgress" runat="server" DynamicLayout="true" >
<ProgressTemplate>
<div class="divProgress" id="divProgress" runat="server" align="center">
<img src="~/images/red_rotation.gif" id="imgUpdateProgress" runat="server" alt="Please Wait..." />
</div>
</ProgressTemplate>
</asp:UpdateProgress>


Visual Studio: System.Web.AspNetHostingPermission Failed

clock May 20, 2010 14:09 by author Jon

I got this error in Visual Studio when I was running one of our projects on a new development machine where I had just recently installed a fresh copy of Visual Studio 2008.  After installing the Microsoft .NET SDK, this error went away.  Hopefully this post helps someone else out there.



Removing Control From Tab Order

clock April 27, 2010 15:33 by author Jon

This one is simple but nice to know.  To remove a control from the tab order you can just set its TabIndex to -1.  I thought there might be a "turn tabbing off" property, but this seems to be the easiest way.

<asp:CheckBox ID="myCB" runat="server" Text="MyCB" TabIndex="-1" />


LinqDataSource: Efficient Custom Paging with LINQ

clock March 26, 2010 16:40 by author Jon

Creating a GridView (or other data control) with efficient paging is very easy with LINQ.  You can thank the Take and Skip operators for this; they allow you to only pull back the records you need.  In the simple example below we are using a LinqDataSource and handling its onselecting method to create our LINQ query and do our paging.  Set AutoPage to false since we are writing code to handle paging ourselves.  Also the PageSize property of the GridView control is being populated from a constant in the code-behind class.

ASPX:

<asp:LinqDataSource ID="linqDS" runat="server" AutoPage="false" 
    ContextTypeName="Namespace.MyDataContext" 
    onselecting="linqDS_Selecting" />
        
<asp:GridView ID="myGV" runat="server" DataSourceID="linqDS"
    AllowPaging="true" PageSize="<%# PAGE_SIZE %>"
    AutoGenerateColumns="false">
    <Columns>
        <!--Removed for simplicity -->
    </Columns>
</asp:GridView>

 

Code-behind:

// Const declared at top of code-behind
public const int PAGE_SIZE = 100;

protected void linqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    // LINQ query
    var query = from c in myDC.Class
                select c;
    
    // Set the total count     
    // so GridView knows how many pages to create    
    e.Arguments.TotalRowCount = query.Count();

    // Get only the rows we need for the page requested
    query = query.Skip(myGV.PageIndex * PAGE_SIZE).Take(PAGE_SIZE);

    e.Result = query;
}

 



GridView EmptyDataTemplate: Get Rid of That Annoying Border!

clock November 20, 2009 14:48 by author Jon

If you are like me then you may have a CSS table style for your GridView with a specific border.  When you use the the EmptyDataTemplate you may then have a border around your message.  I create a CSS class and then use the EmptyDataRowStyle and set its CssClass.  Note, in the GridView I am also setting the default border to 0.  Example:

CSS

table.myGridClass .empty td
{
border-style: none;
border-width: 0px;
background-color: #ffffdd;
}

 

ASPX

<asp:GridView ID="myGridView" runat="server" CssClass="myGridClass" BorderWidth="0">
<HeaderStyle CssClass="myHeaderStyle" />
<RowStyle CssClass="myRowStyle" />
<EmptyDataRowStyle CssClass="empty" />
<EmptyDataTemplate>Your message here.</EmptyDataTemplate>


ASP.NET AJAX Control Toolkit: Bug with Modal Popup Extender

clock November 20, 2009 13:46 by author Jon

I found a bug in the September 30, 2009 release of the AJAX Control Toolkit.  When you press a button that is inside of a Modal Popup dialog box it will cause a full page postback instead of a partial postback.  When I reverted to the May 13, 2009 release this did not occur.  It also looks like the AJAX Control Toolkit is being replaced with the ASP.NET Ajax Library (currently in Beta).

By the way, I also ran into another weird issue on the September 2009 release where random commas were being inserted into my text box on each postback.  If you are having this issue I also recommend reverting to the May 2009 release.



ASP.NET: Beware of Blank ImageUrl on ImageButton

clock September 22, 2009 16:11 by author Jon

Earlier today I was debugging a rather tricky issue.  It came down to the fact that an ImageButton control was not getting its ImageUrl property set in certain scenarios.  This would cause ASP.NET to output the following HTML for this control:

<input type="image" name="myName" id="myID" src="" />

 

This was causing the page to request Default.aspx since a src was not explicitly defined*.  So be sure to set the ImageButton control (or its container element) to Visible="false" if you are not setting its ImageURL property.  Otherwise you may have some code-behind logic on Default.aspx executed when you aren't expecting it!

* I assume this is because Default.aspx is set as the default document in IIS.



ASP.NET AJAX: Checking for Partial Postback - IsInAsyncPostBack

clock September 22, 2009 16:00 by author Jon

My initial thought is always to look for a property on the Page object, but this is actually a property on the ScriptManager.  So if you want to only execute code if it isn't a partial page post back you can do the following:

if (!scriptManagerInstance.IsInAsyncPostBack)
{
    // Do stuff here
}