Web Form

Project Folder

A Beginner's Guide to ASP.NET Application Folders

Connected Services

Add Connected Service

App_Browsers folder

The App_Browser folder contains browser information files (.browser files). These files are XML based files which are used to identify the browser and browser capabilities.

You can find existing browser definitions (machine level) at,

%SystemRoot%\Microsoft.NET\Framework\[FRAMEWORK VERSION]\Config\Browsers\,

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Browsers

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\Browsers

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\Browsers

If you want to change a .browser file, just copy the file to the App_Browser folder and change it. You can create new browser files by just clicking on Add New Item of the App_Browser folder.

When a Request arrives at an ASP.NET application, the ASP.NET run time reads the information in the Request header to determine the client browser and it's capabilities. Now, based upon the browser information [From the browser definition file], ASP.NET server control adapters might generate different XHTML markups so that the final content is rendered correctly on the browser.

App_Code folder

The App_Code Folder stores classes, typed data sets, etc. All the items that are stored in App_Code are automatically accessible throughout the application.

To mix C# and VB classes in the App_Code Folder

to configure the web.config

App_Data folder

The App_Data folder is used as a data storage for the web application. It can store files such as .mdf, .xsd and XML.Any file you place there won't be downloadable. The connection string is,

An error will be generated if a log file exists in the same directory as the data file and the 'database' keyword is used when attaching the primary data file. In this case, remove the log file. Once the database is attached, a new log file will be automatically generated based on the physical path.

To get the value of |DataDirectory|

AppDomain.CurrentDomain.GetData("DataDirectory")

To set the value of |DataDirectory|

AppDomain.CurrentDomain.SetData("DataDirectory", newpath)

App_GlobalResources & App_LocalResources floders

Local Resources:

Local resource is specific to particular webpage. All the resource files related to particular webpage must be placed in a special .Net folder named App_LocalResources. This folder must be in the same folder of the webpage that you want to localize. So App_LocalResources can be subfolder for any folder of your website.

Global Resources:

They are the resource files for the entire web application. They are not specific to any page. They can be accessed from the entire web application. Global resource files must be located in a special .Net folder named App_GlobalResources. App_GlobalResources must be at the root of the web application.

Understanding Globalization and Localization in .NET

App_Start folder

App_Start folder can contain class files which will be executed when the application starts. Typically, these would be config files like AuthConfig.cs, BundleConfig.cs, FilterConfig.cs, RouteConfig.cs.

Do you use startup tasks in the ~/App_Start folder instead of putting code in Global.asax?

Content folder

Content folder contains static files like css files, images and icons files.

Scripts folder

Scripts folder contains JavaScript or VBScript files for the application.

Files

Global.asax

Default Global.asax for web form

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Packages.config

file is managed by NuGet to keep track of what packages and versions you have installed in the application.

Web.config

Web.config file contains application level configurations.

ViewSwitcher.ascx

A file of ASP.NET FriendlyUrls NuGet package.

URL Routing

Default Global.asax

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        ...
    }
}

Default RouteConfig

public static void RegisterRoutes(RouteCollection routes)
{
    var settings = new FriendlyUrlSettings();
    settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(settings);
}

URL Rooting in ASP.NET (Web Forms)

URL Routing

ClientScript.GetPostBackEventReference

function Recaptcha_ClientValidate(sender, e) {
    var response = grecaptcha.getResponse();
    e.IsValid = response.length != 0;
}
protected void Recaptcha_ServerValidate(object source, ServerValidateEventArgs args)
{
    string recaptchaResponse = Request.Form["g-Recaptcha-Response"];
    // When the response is a null string, recaptcha fails.
    args.IsValid = (!string.IsNullOrWhiteSpace(recaptchaResponse));         
}

Web Methods

You cannot define a web method in a user control. Instead, you have to define a web method in a web form,

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return PeelRegionalPolice.PONPlus.Web.UserControls.TestUC.GetCurrentTime(name);
}

Web method to return JSON data

[WebMethod]
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
[ScriptMethod(UseHttpGet = true)]
public string GetSurvey() {
    return "Question: Who is Snoopy?";
}

Generic Handlers (.ashx)

Default

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

Server Control

TextBox

Limitations:

  • No value will be returned when the control is set to ReadOnly.

JavaScript

Accessing web controls with jQuery

<asp:TextBox runat="server" ID="myTextBox" />

The above aspx code when rendered on a page changes to

<input type="text" id="ctl00_Main_myTextBox" name="ctl00$Main$myTextBox" />

Option 1: To access the web control with jQuery

$('#<%= myTextBox.ClientID %>')

Option 2

$('[id$=myTextBox]') // id which ends with the text 'myTextBox'

$('[id*=myTextBox]') // id which contains the text 'myTextBox'

Option 3: Use CSS class

<asp:TextBox runat="server" ID="myTextBox" CssClass="myclass" /> //add CssClass

$('.myclass') //selector

Option 4: Use ClientIDMode="Static"

<asp:TextBox runat="server" ID="myTextBox" ClientIDMode="Static"  /> //add ClientIDMode

$('#myTextBox') //use the normal ID selector

AJAX

ASP.NET AJAX Control Toolkit

https://www.devexpress.com/Products/AJAX-Control-Toolkit/

AjaxControlToolkit - Nuget

ASP.NET AJAX Control Toolkit v18.1.0

ScriptManager (ToolkitScriptManager obsolete)

The ToolkitScriptManager has been removed in v15.1.

Extenders

AutoCompleteExtender

Sample code to support key value pairs

[WebMethod]
public static IEnumerable<string> GetOffenses(string prefixText, int count)
{
    var offenses = StandardChargeRepository.GetStandardCharges();
    var q = from offense in offenses
            where (string.IsNullOrWhiteSpace(prefixText) || offense.LabelInLowerCase.StartsWith(prefixText.ToLower()))
             select AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(offense.Label, offense.Summary);
    var result = q.Take(count).ToList();
    return result;
}
<div>
    Offense: <asp:TextBox ID="txtOffense" runat="server" Width="580" AutoCompleteType="None"></asp:TextBox>

    <ajaxToolkit:AutoCompleteExtender ID="aceOffense" runat="server" TargetControlID="txtOffense" 
    ServiceMethod="../TicketInfo.aspx/GetOffenses" CompletionSetCount="15" 
        MinimumPrefixLength="3" FirstRowSelected="true" EnableCaching="false" 
    OnClientItemSelected="setOffense" ></ajaxToolkit:AutoCompleteExtender>
</div>

<script type="text/javascript">
    function setOffense(source, eventArgs)
    {
        alert(" Key : " + eventArgs.get_text() + "  Value :  " + eventArgs.get_value());
    }
</script>

Pass Additional Parameter to WebMethod using ContextKey

results matching ""

    No results matching ""