How to get row data of html table using jquery

In this jquery tutorial you will learn how to get the row data of html table. To manipulate and filter data is now a days common task for web developers. I will present two scenarios in front of you that will cover how to get the row data of both static table and the table that is generated dynamically. let's have a look over how to do so

First Scenario:-

Getting the row data of html static table using jquery.



Let's suppose, You have static html table and you want to get the data of your desired row. For this you have to copy/paste this example.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
$(document).ready(function()

{

$("#tblTest td:nth-child(1)").click(function(event){

//Prevent the hyperlink to perform default behavior
event.preventDefault();
//alert($(event.target).text())

var $td= $(this).closest('tr').children('td');


var sr= $td.eq(0).text();

var name= $td.eq(1).text();

var city= $td.eq(2).text();

alert(sr);

alert(name);

alert(city);
}

);

});

</script>
And then i draw html table
<table id="tblTest">
<tr>
<td>
<a href="#">1</a>
</td>
<td>
Adeel Fakhar
</td>
<td>
Pakistan
</td>
</tr>
<tr>
<td>
<a href="#">2</a>
</td>
<td>
Mark David
</td>
<td>
America
</td>
</tr>
</table>
So now, if you will click the hyperlink of first row, you will get the data of whole row. First you will get the data or inner html of table’s first cell then second and then you will get the data or inner html of table’s last cell.

But what if you want to get the row data of html table that is generated dynamically?

Second Scenario:-

Getting the row data of dynamic html table using jquery


Yeah, to get the row data of table that is generated dynamically, containing the values from database in it's cell. you have to use the jquery built-in live() function
$(document).ready(function()

{

$("table[id$='tblDisplay'] td:nth-child(1)").live('click',function(event)

{
//Prevent the hyperlink to perform default behavior
event.preventDefault();
var $td= $(this).closest('tr').children('td');
var sr= $td.eq(0).text();

var name= $td.eq(1).text();

var city=$td.eq(2).text();

alert(sr);

alert(name);

alert(city);

//$("#txtName").val(name); // assigning the name to the textbox

}

);

});
The reason why we need live() function is that it allows you to set the function to not only current elements but also future elemnents, That's why all your page elements will stay live even after you have made an ajax call.

live () function Binds a handler to an event (like click) for all current and future matched elements. It can also bind custom events.

This function is similar to the .click() but .click() will only match elements that are created via the XHTML with DOM is loaded. With that said, using .live() is very useful for dynamic websites, where you are creating and removing elements on your website

.click() function does not work on dynamically created items.

You can adopt same method to get the row data of gridview because when asp.net page renders then gridview becomes <table>.

So this is the way to get the row data of html table in jquery.

Read more...

how to embed a flash movie in asp.net masterpage

In this tutorial we will learn how to embed a flash movie in asp.net masterpage. To embed a flash movie in asp.net master page is not a rocket science but still most of developers found some problems while doing so. In this tutorial we will cover those two common problems that come while embedding a flash movie in asp.net master page. Let's read both case scenarios.

How to embed a flash movie in asp.net masterpage

Case Scenario 1
Let's suppose you have a masterpage and you have embeded the flash movie in it . Flash movie is located in let's suppose images folder that is placed on same level as of masterpage. In that scenario following code will be used to embed the flash movie in asp.net masterpage


<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/
shockwave/cabs/flash/swflash.cab#version=9,0,28,0"
width="925" height="300"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/
width="925" height="300">
<param name="movie" value='/images/yourflashmovie.swf' />
<param name="quality" value="high" />

<embed src='/images/yourflashmovie.swf' quality="high" pluginspage=http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash type="application/x-shockwave-flash" width="925" height="300"></embed>
</object>
Note:-
version=9,0,28,0 is the version of flash player that I am using.

Now let's suppose you have welcome.aspx page also placed on same folder of .masterpage and when you will run this welcome.aspx page, you will see flash movie on welcome.aspx page but this flash movie will not come on all other .aspx pages of your website that are located in other nested folders, the hierarchy other than .masterpage. For example you will not see flash movie in your Comments/comments.aspx webpage.

Now let's understand the reason why the flash movie is not visible in http://yourwebsite.com/Comments/comments.aspx and visible in http://yourwebsite.com/Welcome.aspx. As you have embedded the flash movie in your .masterpage, now please look at the code of object tag that will embed the flash movie in masterpage. Look at the following line codes between the <object> </object> tags

<param name="movie" value='/images/yourflashmovie.swf' />


This line causes your flash movie not to visible in all the .aspx pages placed on nested folders or placed on hierarchy other than .masterpage. When you will run the webpages such as http://yourwebsite.com/Comments/comments.aspx the path of flash movie will become Comments/images/yourflashmovie.swf which is totally wrong as we don’t have any images folder inside the comments folder. Actually the page tries to find the images folder in the same hierarchy where Comments.aspx page exists and when it fails to find any images folder then obviously it will not show required flash movie in your aspx page.

Now let’s have a look over how to resolve this issue so that once you embed the flash movie in .masterpage then it will be visible to all other webpages no matter where they exist, but inheriting that masterpage. Here’s the code that will solve your problem
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=http://download.macromedia.com/pub/shockwave/
cabs/flash/swflash.cab#version=9,0,28,0 width="925" height="300">
<param name="movie" value='<%= ResolveUrl("~/images/yourflashmovie.swf") %>' />

<param name="quality" value="high" />
<embed src='<%= ResolveUrl("~/images/yourflashmovie.swf") %>' quality="high" pluginspage=http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash type="application/x-shockwave-flash" width="925" height="300">
</embed>
</object>
ResolveUrl() plays the vital role to solve your problem.

Syntax:-
C#
public string ResolveUrl(
string relativeUrl
)

Parameters:-
relativeUrl
Type: System..::.String
The URL associated with the TemplateSourceDirectory property.

Return Value
Type: System..::.String
The converted URL.

ResolveUrl() converts a URL into one that is usable on the requesting client. If the relativeUrl parameter contains an absolute URL, the unchanged URL is returned. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL. This method uses the TemplateSourceDirectory property to resolve to the absolute URL. The returned URL is for client use.

Case
Scenario 2
Once I was developing a website in which I was using css dropdown menu. Now problem was that for some reason css dropdown menu appears behind the embeded flash movie. I was very upset due to that.

Now let's have a look over how I solved that issue. Simply I add wmode attribute to the <param> inside the <object></object>. Let’s have a look over the code

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0 width="925" height="300">

<param name="movie" value='<%= ResolveUrl("~/images/yourflashmovie.swf") %>' />

<param name="quality" value="high" />

<param name="wmode" value="transparent" />

<embed src='<%= ResolveUrl("~/images/yourflashmovie.swf") %>' quality="high"
wmode="transparent" pluginspage=http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash type="application/x-shockwave-flash" width="925" height="300"></embed>
</object>

Please add the wmode attribute inside the <embed> because incase if <object> is not executed then <embed> will be executed.

wmode attribute set the background of Flash movie to transparent. It allows the background color or image of the HTML page that contains the Flash movie to show through and allows the layering of Flash content with DHTML content. For further reference please visit How to make a flash movie with a transparent background
So these are the ways to embed a flash movie in asp.net masterpage properly to get rid of various problems.


Happy Coding!!!

Read more...