User Tools

Site Tools


notes:asp.net:miscellaneous

ASP.NET Miscellaneous Topics

HttpModules hook into application level events to provide supporting features such as:

  • OutputCache
  • Session
  • WindowsAuthentication
  • FormsAuthentication
  • DefaultAuthentication

HttpHandlers are selected and executed by ASP.NET to generate a response for a request.

A few handy code snippets:

// Show the name of the currently logged in user.
if (HttpContext.Current.User != null &&
    HttpContext.Current.User.Identity != null)
    Debug.WriteLine(HttpContext.Current.User.Identity.Name);
 
// Decode a query string parameter.
if (Request.QueryString["par"] != null)
{
    string str = Convert.ToString(Request.QueryString["par"]);
    string decoded = Server.HtmlDecode(str);
}
 
// Convert a string to an HTML-encoded string.
string encoded = System.Web.HttpUtility.HtmlEncode(str);
 
// Redirect to the main page.
Response.Redirect(Request.ApplicationPath); 
 
// Refresh the current page.
Response.Redirect(Request.Url.LocalPath);
 
// Set the width of a TableCell in pixels or as a percentage.
TableCell cell = new TableCell();
cell.Width = Unit.Pixel(80);
cell.Width = Unit.Percentage(80);
 
// A relative URL passed into a user control is resolved with respect to the directory 
// containing the user control. Use Page.ResolveUrl to resolve the URL with respected to a containing page.
<asp:Hyperlink id="link" runat="server" />
...
link.NavigateUrl = Page.ResolveUrl(url);
// Find an ASP.NET control recusively in a given container.
public Control FindControl(Control container, string controlId)
{
    Control control = null;
    foreach (Control ctrl in container.Controls)
    {
        if (control == null)
        {
            control = FindControl(ctrl, controlId);
            if (ctrl.ID == controlId)
                return control = ctrl;
        }
    }
    return control;
}

Session state snippets:

// Store an array in session state.
string[] arr = new string[3] { "A", "B", "C" };
Session["MyArray"] = arr;
 
// Retrieve an array from session state.
string[] books = (string[])Session["MyArray"];
 
// Access session state from code different than a web page.
var context = System.Web.HttpContext.Current;
context.Session["MyValue"] = "ABC"; // add value
context.Session.Remove("MyValue"); // remove value

Instantiate a user control programmatically:

<%@ Page Language="C#" %>
<%@ Reference Control="UserControls/MyControl.ascx" %>
 
<asp:PlaceHolder id="PlaceHolder1" runat="server" />
// LoadControl accepts a relative path to a user control.
MyControl ctrl = (MyControl)Page.LoadControl("UserControls/MyControl.ascx");
 
// Add the user control to the page.
PlaceHolder1.Controls.Add(ctrl);

Attach a CSS stylesheet dynamically:

Page.Header.Controls.Add(
    new LiteralControl("<link href='" + 
        ResolveClientUrl("~/Styles/main.css") + 
            "' type='text/css' rel='stylesheet' />"));
notes/asp.net/miscellaneous.txt · Last modified: 2016/11/23 by admin