Access an ASP.NET control on a MasterPage using jQuery

I must have been asked this question numerous times -- How do I access an asp.net control kept in the master page using jQuery? Here’s one of the ways of how to populate the value of an ASP.NET TextBox kept in a MasterPage using jQuery

MasterPage

<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>Access Control using jQuery</title>
<
asp:ContentPlaceHolder id="head" runat="server">
</
asp:ContentPlaceHolder>

<
script src="Scripts/jquery-1.3.2.js"
type="text/javascript"></script>
<
script type="text/javascript">
$(document).ready(function() {
$("input[id$='txtName']").val("Text Added using jQuery");
});
</script>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>
<
asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

</
asp:ContentPlaceHolder>
</
div>
</
form>
</
body>
</
html>



ContentPage

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"

AutoEventWireup="true" CodeFile="MasterPage.aspx.cs" Inherits="MasterPage"%>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</
asp:Content>

<
asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"

Runat="Server">

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

</
asp:Content>

Note how we selected the MasterPage using the $("input[id$='txtName']") . This is because the TextBox in the MasterPage gets renamed as shown below:

<input name="ctl00$ContentPlaceHolder1$txtName" type="text" id="ctl00_ContentPlaceHolder1_txtName" />

On running the page, the textbox gets populated using jQuery

image

Check out my other ASP.NET jQuery articles.

Convert all Links to Open in a New Page

I was working on an existing website recently. The website had a few pages with a couple of hyperlinks leading to Customers websites. Clicking on the hyperlink would renders the content in the same window. The client now wanted that without any code change, all the links should open in a new window, so that the user remains on the main site, as well as also view the Customers website.

Here’s how this requirement can be achieved using jQuery

<!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>
<
title>Open all Links In New Window</title>

<
script src="Scripts/jquery-1.3.2.js"
type="text/javascript"></script>
<
script type="text/javascript">
$(document).ready(function() {
$("a").each(function() {
$(this).attr("target", "_blank");
});
});
</script>
</
head>
<
body>
<
div>
<
a href="http://www.dotnetcurry.com">DotNetCurry</a><br />
<
a href="http://www.sqlservercurry.com">SQL Server</a><br />
<
a href="http://www.devcurry.com">DevCurry</a><br />
</
div>
<
br />
</
body>
</
html>


Observe that using jQuery, we set the target property of all the hyperlink to _blank, which renders content in a new window. Here are the values of the Target property

_blank - Renders the content in a new window without frames.

_parent - Renders the content in the immediate frameset parent.

_self - Renders the content in the frame with focus.

_top - Renders the content in the full window without frames

If you want to achieve the same in an ASP.NET page, just replace the links with an ASP.NET Hyperlink.

3 Column CSS Layout with Header and Footer

I am not a designer. But that should not stop me from trying when I come across a designing requirement. I helped a colleague of mine with a designing requirement where he needed to design a 3 Column Layout using CSS that had a header and footer. One of the main requirements was that the layout should automatically get adjusted (flow) to the contents. We took tips from loads of articles on the same topic and here’s what we came up with

<!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>
<
title>3 Column CSS Layout with Header and Footer</title>
<
style type='text/css'>
.header{
position: relative;
float: left;
width: 100%;
background-color: #356aa0
}
.container{
position: relative;
float: left;
width: 100%;
background-color: #cccccc
}
.left{
position: relative;
float: left;
width: 20%;
background-color: #a2b5cd
}
.center{
position: relative;
float: left;
left: 1%;
right:1%;
width: 58%;
background-color: #a2b5cd
}
.right{
position: relative;
float: right;
width: 20%;
background-color: #a2b5cd
}
.footer{
position: relative;
float: left;
width: 100%;
background-color: #356aa0
}
body {
padding: 0px;
margin: 2%;
background-color: #e7e7de
}
</style>
</
head>
<
body>
<
div class="header">
Header Text
</div>
<
div class="container">
<
div class="left">
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left
left left left left left left left left left left left left

</div>
<
div class="center">
center
</div>
<
div class="right">
right
</div>
</
div>
<
div class="footer">
Footer Text
</div>
</
body>
</
html>



OUTPUT

image

Suggestions are welcome!

Pressing Enter Key in a Multiline TextBox causes a Postback in FireFox

One of our http://www.dotnetcurry.com/ authors Malcolm Sheridan informed me that when he is replying to a viewer who has left comments and enters text in the Comment field, he can't hit the enter key to start a new paragraph or a new line because doing so causes a postback in FireFox. The Comment field happens to be a Multiline TextBox and the issue is seen in the FireFox browser.

Well if you are facing a similar issue and are looking out for a solution, here it is.

<asp:TextBox ID="txtBody" onkeypress="event.cancelBubble=true;"
runat="server" Text='<%# Bind("Body") %>' TextMode="MultiLine"
Rows="5" Width="100%"></asp:TextBox>

Observe how I have used event.cancelBubble=true on the keypress event. This property determines whether the current event should bubble up the hierarchy of event handlers. Setting it to true disables bubbling for the event and does not cause a postback when the Enter key is pressed.

Read more on the cancelBubble Property over here

Seperating the Integral and Fractional portion of a Decimal using C# or VB.NET

In one of my recent visits to a client, I saw developers working on a functionality where they were separating the Integer and fractional portions of a Decimal. The code used by them was more than it was actually required.

Keeping note of the handy functions in the .NET framework can be extremely useful in such situations and can save you a lot of efforts. One similar function is the Math.Truncate() function. Here’s how to achieve the requirement using this function

C#

class Program
{
static void Main(string[] args)
{
decimal num = 55.32m;
decimal integral = Math.Truncate(num);
decimal fractional = num - integral;
Console.WriteLine("Integral Part is {0} and Fractional Part is {1}",
integral, fractional);
Console.ReadLine();
}
}

VB.NET

Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim num As Decimal = 55.32D
Dim integral As Decimal = Math.Truncate(num)
Dim fractional As Decimal = num - integral
Console.WriteLine("Integral Part is {0} and Fractional Part is {1}", _
integral, fractional)
Console.ReadLine()
End Sub
End Class

Output

image

All you need to do is now create a seperate function and add this code over there.

Hide a Table Column with a Single line of jQuery code

In one of my previous articles, Using jQuery to Delete a Row in a Table by just Clicking on it I showed you how to delete a Table Row. Continuing my ‘Less is More’ journey with jQuery, today I will show you how to Hide a Column with a Single line of jQuery code

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title></title>
<
script src="Scripts/jquery-1.3.2.js"
type="text/javascript"></script>
<
script type="text/javascript">
$(document).ready(function() {
$('#btnHide').click(function() {
$('td:nth-child(2)').hide();
// if your table has header(th), use this
//$('td:nth-child(2),th:nth-child(2)').hide();
});
});
</script>
</
head>
<
body>
<
table id="tableone" border="1">
<
tr class="del">
<
td>Row 0 Column 0</td>
<
td >Row 0 Column 1</td>
<
td >Row 0 Column 2</td>
</
tr>
<
tr class="del">
<
td>Row 1 Column 0</td>
<
td>Row 1 Column 1</td>
<
td>Row 1 Column 2</td>
</
tr>
<
tr class="del">
<
td>Row 2 Column 0</td>
<
td>Row 2 Column 1</td>
<
td>Row 2 Column 2</td>
</
tr>
<
tr class="del">
<
td>Row 3 Column 0</td>
<
td>Row 3 Column 1</td>
<
td>Row 3 Column 2</td>
</
tr>
<
tr class="del">
<
td>Row 4 Column 0</td>
<
td>Row 4 Column 1</td>
<
td>Row 4 Column 2</td>
</
tr>
<
tr class="del">
<
td>Row 5 Column 0</td>
<
td>Row 5 Column 1</td>
<
td>Row 5 Column 2</td>
</
tr>
</
table>
<
input id="btnHide" type="button" value="Hide Column 2"/>

</
body>
</
html>

Read more about the nthchild selector over here Selectors/nthChild


Before hiding Column 2, the table appears as shown below:

image

After clicking the button ‘Hide Column 2’, the table appears as shown below:

image

See a Live Demo

You can also check plenty of other jQuery Tips here

Using jQuery to Delete a Row in a Table by just Clicking on it

Here’s a very simple way of deleting a row in a table, when a user clicks on it. With jQuery, the efforts required to achieve this requirement is just 2 lines of code.

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title></title>
<
script src="Scripts/jquery-1.3.2.js"
type="text/javascript"></script>
<
script type="text/javascript">
$(document).ready(function() {
$('table tr.del').click(function() {
$(this).remove();
});
});
</script>
</
head>
<
body>
<
table border="1">
<
tr class="del">
<
td>Row 0 Column 0</td>
<
td >Row 0 Column 1</td>
</
tr>
<
tr class="del">
<
td>Row 1 Column 0</td>
<
td>Row 1 Column 1</td>
</
tr>
<
tr class="del">
<
td>Row 2 Column 0</td>
<
td>Row 2 Column 1</td>
</
tr>
<
tr class="del">
<
td>Row 3 Column 0</td>
<
td>Row 3 Column 1</td>
</
tr>
<
tr class="del">
<
td>Row 4 Column 0</td>
<
td>Row 4 Column 1</td>
</
tr>
<
tr class="del">
<
td>Row 5 Column 0</td>
<
td>Row 5 Column 1</td>
</
tr>
</
table>
</
body>
</
html>

When you preview the code, you see a screen similar to the following :

image

Now click on a Row , let us say Row 3. What do you observe? Well it gets deleted!

image

If you want to achieve the same in an ASP.NET GridView, check out my article over here

Some ASP.NET GridView UI Tips and Tricks using jQuery

Silverlight 3 Released

Microsoft Silverlight developers and designers have a reason to rejoice! Silverlight 3 has been released (RTW) and here are some important links:

Silverlight 3 Runtime – Version is Silverlight 3 RTW (3.0.40624.0)

Silverlight 3 SDK - provides libraries and tools for developing Silverlight applications

Silverlight 3 Tools for Visual Studio 2008 SP1- Add-on for Visual Studio 2008 SP1 for developing Silverlight 3 applications

Microsoft Expression Blend 3 + SketchFlow RC - Create Windows Presentation Foundation (WPF) applications for the desktop and Microsoft Silverlight 3 applications for the web

Deep Zoom Composer - Prepare images for use with the Deep Zoom feature in Silverlight 3

Ironically there has been no official announcement about the release yet as of this writing, however the download links are active !

Update: To find out more on the new features of Silverlight 3 and Blend 3, check ScottGu's post over here