Trim() function in javascript

In this javascript tutorial you will learn how to remove whitespace before /after the string using trim() function in javascript.

Let's have a look over below mentioned javascript custom function that will remove whitespace before/after the string.

function TrimString(str)

{

return str.replace(/^\s+|\s+$/g,"");

}

The calling of this function is very simple, you just have to provide string argument to this function.

I have developed an html web page in which I have a custom function for Trimming which is TrimString() and to call this TrimString() function I have made another function TrimMyString() and I am calling this TrimMyString() function via onclick event of hyperlink.

In this function first of all I declared a variable Myname and assign a string value to it but one thing must be noticed that there are some whitespace before/after the string value and then I calculate the length of that string and store the length in another variable len and then alert the length of the string before the trimming process. After this I used the function TrimString() to trim the string so that whitespace before/after string value can be removed and then again I calculate the length of string after trimming and then alert the length.

Now this time you will find the huge difference between two lengths before and after the trimming process and that difference will tell you how effective and efficient this simple javascript custom function is. Actually this custom function is using regular expression for trimming the whitespace before/after the string, means this function is capable of both ltrim and rtrim.

<html>

<head>

<title>Trim() function in javascript</title>

<script language="javascript" type="text/javascript">

function TrimString(str)

{

return str.replace(/^\s+|\s+$/g,"");

}

function TrimMyString()

{

var Myname,len;

Myname=" Adeel Fakhar ";

len=Myname.length;

alert(len);

Myname=TrimString(Myname);

len=Myname.length;

alert(len);

}

</script>

</head>

<body>

<a href="javascript:TrimMyString();" > Call Trim function</a>

</body>

</html>

So this is the way to trim the string in javascript using trim() function.

Read more...

Passing QueryString from asp.net hyperlink in asp.net using c#

In this asp.net tutorial you will learn how to pass QueryString from asp.net hyperlink in asp.net using c#. Let's have a look over how to do this. In first example we will look how to pass single QueryString from asp.net hyperlink and then in second example we will look how to pass multiple QueryString params from asp.net hyperlink.

First Example:-

<asp:TemplateField HeaderText="Case No">

<ItemTemplate>

<asp:HyperLink ID="hyp_Caseno" Text='<%# Bind("Case_No")%>' runat="server" NavigateUrl='<%# "yourpage.aspx?caseno=" + DataBinder.Eval(Container.DataItem,"Case_No")%>'></asp:HyperLink>

</ItemTemplate>

</asp:TemplateField>

Note:- The Case_No in DataBinder.Eval is the column name of your database’s table. Actually we are getting values of the column from database’s table whenever grid will bind.

Now let’s have a look over how to pass multiple QueryString Params from asp.net hyperlink

Second Example:-

<asp:TemplateField HeaderText="Case No">

<ItemTemplate>

<asp:Hyperlink ID="hyp_Caseno" Text='<%# Bind("Case_No")%>' runat="server" NavigateUrl='<%# "yourpage.aspx?caseno=" + DataBinder.Eval(Container.DataItem,"Case_No") + "&productid=" + DataBinder.Eval(Container.DataItem,"Product_ID")%>' />

</ItemTemplate>

</asp:TemplateField>

So this is the way to pass single and multiple QueryString params from asp.net hyperlink.

Happy Coding

Keep Coding

Read more...

How to fire onClick event of LinkButton inside gridview in asp.net using c#

In this asp.net tutorial you will learn how to fire onclick event of LinkButton inside gridview in asp.net using c#. Normally you can't fire the onclick event of LinkButton which lies inside gridview but in this tutorial we will look how to do this.

Let's have a look over .aspx page in which we have gridview control

.aspx page

<asp:gridview ID="GridView1" runat="server" Width="100%" EnableTheming="false" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField HeaderText="Customer ID">

<ItemTemplate>

<asp:LinkButton ID="lnk_CustomerID" Text='<%# Bind("Customer_ID")%>' runat="server" OnClick="lnk_CustomerID_Click"></asp:LinkButton>

</ItemTemplate>


</asp:TemplateField>

<asp:TemplateField HeaderText="Customer Name">

<ItemTemplate>

<asp:Label ID="lbl_CustomerName" Text='<%# Bind("Customer_Name")%>' runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Customer Age">

<ItemTemplate>

<asp:Label ID="lbl_CustomerAge" Text='<%# Bind("Customer_Age")%>' runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:gridview>

As you can see that I have Link button inside the gridview that in mycase contains the unique values which is Customer ID. Now I want to fire the onclick event of LinkButton to execute some other code, which is to store the Text of LinkButton in a session and then want redirection to the Customer Detail page. Let’s have a look how it can be done.

.cs page

protected void lnk_CustomerID_Click(object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
GridViewRow gv = (GridViewRow)(link.Parent.Parent);
LinkButton CustomerID = (LinkButton)gv.FindControl("lnk_CustomerID");
Session["customerid"] = CustomerID.Text;
Response.Redirect("customer_detail.aspx");
}
So this is the way to fire the onclick event of LinkButton inside gridview in asp.net using c#.

Happy Coding...!

Keep Coding...!
Read more...

Highlight gridview row on mouse over using javascript and css in asp.net using c#

In this asp.net tutorial you will learn how to highlight gridview row on mouse over using javascript and css in asp.net using c#. There are couples of methods to highlight rows of gridview on mouse over. I preferred first one which is OnRowDataBound event of GridView.

So let's have a look over .aspx code


Highlight_gridview_row.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="highlight_gridview_row.aspx.cs" Inherits="MainFolder_SubFolder_highlight_gridview_row" %>

<!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>Highlighting gridview row on mouse over using css and javascript in asp.net and c#</title>
<style type="text/css">
.highlightrow
{
background-color:#e5e5e5;
}
.normal
{
background-color:white;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server" width="100%" EnableTheming="false" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" CssClass="aLinkButton" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="lbl_RecordNotFound" Text="No Record Found" runat="server" Font-Size="Larger" ForeColor="maroon" ></asp:Label>
</EmptyDataTemplate>
</asp:gridview>

</div>
</form>
</body>
</html>

I have created two internal css classes that will play their role for highlighting the rows of gridview, these are

.highlightrow
{
background-color:#e5e5e5;
}
.normal
{
background-color:white;
}

I will call highlightrow class for onmouseover client side event and second class for onmouseout client side event.


Highlight_gridview_row.aspx.cs

First Method:-

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.className='highlightrow'"); e.Row.Attributes.Add("onmouseout", "this.className='normal'");

}
}

Or you can do this without defining internal css classes in this way

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#e5e5e5'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
}
}


Second Method:-

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.className='highlightrow'");
e.Row.Attributes.Add("onmouseout", "this.className='normal'");
}

}

Once again I m saying that I prefer First Method which is to highlight gridview row on mouse over by using OnRowDataBound event because why should we create the OnRowCreated event of Gridview, if we can do this work in OnRowDataBound event. One thing more, the code written in Second Method can also run without OnRowCreated event of Gridview but once again why should we write extra function, if we we can write that code in OnRowDataBound event of gridview.

So these are the ways to highlight gridview row on mouse over using css and javascript in asp.net using c#.

Happy Coding…..

Keep Coding…….
Read more...