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...

PHP performance tips and tricks

In this php tutorial we will learn different PHP performance tips and tricks that will enhance the performance of development using php.

1) Don't use varaibles when there is no need of them

Most of the times the php developer try to clean his/her code by assigning the values of predefined variables to the variable created by him/her of shorter name. This can also become the way of better understanding of php code but it double consume the memory without any need becuase first you will declare the variable and then you will assign the values of predefined variables to those custom variables. Let's have a look over the example to better understand what i mean.

Wrong
$comments=$_POST['comments'];
echo $comments;

Correct
echo $_POST['comments'];

In the above example if the malicious user had inserted the spam text in the comments textarea of 512KB worth of characters then it would use the memory of 1MB without any need.

2) Make habit to Use single-quotes for strings

In PHP you can use both single and double-quotes for string variables but there are some differences between both of them. Using double-quotes for strings tell the engine of php to first read the content of string and then look for variables and then replace variables with their values. So if the string contains no variable then please use single-quote instead of double-quote. Let's have a look over the example

Wrong
$name="My name is Adeel Fakhar";

Correct
$name='My name is Adeel Fakhar';

It is better to use concatenation then double-quotes for string. Let's have a look over the example

Wrong
$name='Adeel Fakhar';
$myname="My name is $name";

Correct
$name='Adeel Fakhar';
$myname='My name is'.$name.' Ok';

Make habit to use echo to print

Use echo to print the results in better readability and better performance.

Wrong
print $name;

Correct
echo $name;

I will also write a post for difference between echo and print.

Don't try to use the concatenation with echo

Most of the novice php programmers don't know that they can pass multiple variables to the echo, seprating with commas, instead of concatenating them first.

Wrong
$firstname='Adeel';
$lastname='Fakhar';

echo 'My name is'.$firstname.$lastname.' OK my dear';

Correct
$firstname='Adeel';
$lastname='Fakhar';

echo 'My name is',$firstname,$lastname,' OK my dear';

Try to use switch/case instead of if/else

If there is a single variable in your php code for comparison purpose then please try to use switch/case instead of if/else for better performance and readability. Let's have a look over the example

Wrong

if($_POST['action'] == 'sum')

{

sumFunction();
}

elseif ($_POST['action'] == 'sub')

{
subFunction ();
}

elseif ($_POST['action'] == 'mul')

{
mulFunction ();
}

else {
defaultFunction();
}

Correct
switch($_POST['action'])

{
case 'sum':
sumFunction();
break;


case 'sub':
subFunction ();
break;


case 'mul':
mulFunction ();
break;


default:
defaultFunction();
break;
}

It’s my suggestion to first understand the language, clear your logic then apply these above mentioned steps,techniques,tips and tricks, what ever you say to enhance the performance of your development. As you have seen that the tutorials I have written in my blog, I haven’t follow any of these techniques because my purpose was to solve your problem quickly rather than to teach you how to write code but in professional coding life you must follow these steps in order to enhance your development performance.

If you like this post of PHP performance tips and tricks then you must thankful to code.google.com because I have read these performance enhancement techniques there and then I decided to write a post for this on my blog.

Happy Coding….. Keep Coding


Read more...

Export gridview to excel within an UpdatePanel in asp.net using c#

In this asp.net tutorial we will learn how to export gridview to excel within an UpdatePanel. Last couple of days I was working over a report in which there is a requirement to export gridview data to excel, I thought that it’s quite easy as I have done it before but I forgot that now this time the gridview is within ajax UpdatePanel. When I started to export then I found couple of errors like System.WebForms.PageRequestManagerParserErrorException exception etc.

Any how, when I search this over internet then I found couple of solutions that I will discuss on this post too but none of them worked in my case but still I become successful to achieve my target without removing update panel. Let’s see how to export gridview to excel within an update panel.

Before I start please find the code to export gridview to excel. Actually I have written a post to export gridview to excel without UpdatePanel so please use that code to export data. Actually that code will be responsible to export the data and in this current post i am just telling how to get rid of the errors that a programmer can face during exporting gridview to excel within an update panel. So let’s start


There are three solutions for export to work within an update panel; it’s depending upon your case which solution to be followed.

1) Remove UpdatePanel tag

The first solution is to simply remove the updatePanel tag but still there is a question that if you want to remove update panel tag then what are you doing here :) So to better understand let’s consider you have an asp button and you want to export the gridview data by clicking on it, so if you want to successfully export the data please close the UpdatePanel tag before the asp button and then again after asp button please insert the updatePanel and then close UpdatePanel in the end of the page.

2) Use PostBackTrigger
The second solution is to use PostBackTrigger before closing tag of </asp:UpdatePanel> and give asp button’s(That you want to use to export gridview data to excel) ID to the ControlID of asp:PostBackTrigger like this


………………………………………………………………
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

The whole aspx code will come here

<asp:Button ID="btn_export" runat="server" Text="Export to excel" onclick="btn_export_Click" />


</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_export" />
</Triggers>
</asp:UpdatePanel>

……………………………………………………………

It will work and you will achieve what you want but there is a case scenario that I had, please have a look over below image

Export gridview to excel within an updatePanel in asp.net using c#

Yeah in this case there is a dropdown list and I wanted this dropdown list to export the data but this dropdown list made me mad as every time it gives me error. First of all I attached the export to excel code to the OnSelectedIndexChanged event of asp dropdown list but I got this error

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '<style> .text { mso-'



Then I again put the code of exporting gridview data to the asp button’s onClick event and tried to call the onClick event of asp button over OnSelectedIndexChanged event of asp dropdown list but I got this same error

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '<style> .text { mso-'.


Then I tried to give the asp dropdown list’s ID to the ControlID of <asp:PostBackTrigger> but I got this error

A control with ID 'ddl_ExportGrid' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

I tried a lot to solve this error by providing lot of options to the ControlID of <asp:PostBackTrigger> such as

<asp:PostBackTrigger ControlID="UpdatePanel1:ddl_ExportGrid" />

<asp:PostBackTrigger ControlID="GridView1:ddl_ExportGrid" />

But still got “could not be found for the trigger” error.


Then I found a solution and its work 100%. The solution is to call a javascript function over client side onChange event of asp dropdown list and that's it. Please have a look over javascript function that you will call over onChange event of asp dropdown list.

function ExportGridviewtoExcel()

{
__doPostBack("<%=btn_export.UniqueID %>",'');
}

Note:- Before doPostBack() function there are two underscores(_)

I am using doPostBack built-in javascript function and in this function I am providing the asp button’s ID as a first argument to it. Now the code for asp dropdown list will be like this

<asp:DropDownList ID="ddl_ExportGrid" runat="server" style="vertical-align:middle;" onchange="ExportGridviewtoExcel();">
<asp:ListItem Value="0" Text="Select a format" />
<asp:ListItem Value="1" Text="Export to Excel" />
</asp:DropDownList> Export

Also please make the asp button invisible so that user can’t see it but please don’t remove it as the export gridview to excel code is written on asp button’s onClick() event and by using javascript function over onChange event of asp dropdown list actually we are calling this asp button. So in the end the code will be

……………………………………………………………………………………
<asp:Button ID="btn_export" runat="server" Text="Export to excel" onclick="btn_export_Click" Visible="false" />

</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_export" />
</Triggers>

</asp:UpdatePanel>


……………………………………………………………………………………

I try my level best to explain how to export gridview data to excel within an UpdatePanel and I hope these solutions will work for you.

Happy Coding…… Keep Coding….!

Read more...