Friday, March 28, 2008

Catch Browser Closing Event

Catch Browser Closing Event


<html>
<head>
<title></title>
</head>
<body onbeforeunload="alert('Closing');">
</body>
</html>

Redirecting User To Secure Connection

Redirecting User To Secure Connection

if (!Request.IsSecureConnection)
{
// send user to SSL
string serverName;
serverName=HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);
string filePath = Request.FilePath;
Response.Redirect("https://" + serverName + filePath);}

How to convert an array into a comma-delimited string with String.Join

How to convert an array into a comma-delimited string with String.Join

Using the static String.Join method is a quick way to get a comma-delimited string. (I used to always run the array through a foreach loop tacking on a comma, and then removing the last comma outside the foreach loop--a bit messy). This code shows you how to take an array and convert it into a string delimited by commas in one nline. Of course, you can delimit your string with any character you want.


<%@ Page Language="C#" %><script runat="server">
void Page_Load(Object sender, EventArgs e)
{
string[] ids = {"2343","2344","2345"};
string idString = String.Join(",",ids);
Response.Write(idString);
}
</script>

Get Screen Size from javascript

Get Screen Size from javascript
<script language="javascript">
res = screen.width+"x"+screen.height+"&d="+screen.colorDepth
</script>

Tuesday, March 18, 2008

The Label control's AssociatedControlID property.

When you add a Label control to your page you can associate the label to a control, for example to a TextBox or CheckBox etc. If you use the AssociatedControlID and associate a control to the label, the runtime will automatically render the “for” attribute to the label element (The “for” attribute is use to specify which control the label is associated to).
<asp:textbox id="TextBox1" runat="server"<
<asp:label id="Label1" associatedcontrolid="TextBox1" text="Label" runat="server"<>/asp:label<
<asp:textbox id="TextBox1" runat="server"<
The control above will be genereated to the following code at runtime:
<label id="Label1" for="TextBox1">Label</label>
<input id="TextBox1" name="TextBox1"></asp:textbox>
<asp:textbox runat="server">
If the “for” attribute is added to a label element, you can click the mouse button on the label and the associated control (in the above example the TextBox) will be selected. If you associate a checkbox to a label, the checbox will be selected or deslected if you click on the label assocated for the checkbox.

How to determine if a control has a child control

Check the .Controls.Count property of the appropriate control. If it is greater than 0, it has child controls and is a container.