Setting the Default Button for a TextBox in ASP.NET


Monday, August 31, 2009

Hello blogger's,

Hitting the enter key in a TextBox can sometimes have undesired effects like the wrong submit Button being “clicked“. The method described below allows you to specify a default Button to submit when the user hits the enter key in a TextBox.

When you press a key on your keyboard, the js OnKeyPress event is fired. This calls a function to which we pass the id of the button associated with the TextBox. The function gets a reference to the button and simuilates a mouse-click on the Button. We perform a browser detection because IE and Netscape have different event models. The function finally returns false so that the keypress event is cancelled (otherwise a second form submit will be raised). This works with newer versions of IE/Netscape.

function clickButton(e, buttonid){
var evt = e ? e : window.event;
var bt = document.getElementById(buttonid);
if (bt){
if (evt.keyCode == 13){
bt.click();
return false;
}
}
}


//code behind
TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

The code behind generates the following code:
<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'Button1')">

This causes web control Button1 to be clicked when the enter key is hit inside TextBox1.

Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

Disable Browser Back Button Functionality using JavaScript


Friday, August 28, 2009

Hi,Please find the code to disable browser back button,it can be helpfull during
online examination.





<script type = "text/javascript" >
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

Code to truncate long string in grid column


Thursday, August 27, 2009

Hi blogger's,

Please copy this below code in you code behind and make changes according to
your requirement.






protected void GrdEastablishment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label objlblans = new Label();
Label objlblsubject = new Label();

objlblans = (Label)e.Row.FindControl("lbledit");
objlblsubject = (Label)e.Row.FindControl("lblsubject");

string strans = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "sDetail"));
if (strans != "")
{
string ans = (string)DataBinder.Eval(e.Row.DataItem, "sDetail");
if (ans.Length > 28)
{
objlblans.Text = ans.ToString().Substring(0, 28).ToString() + "...";
}
else
{
objlblans.Text = ans.ToString();
}
}
else
{
objlblans.Text = "No Text";
}


string strsubject = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "sSubject"));
if (strsubject != "")
{
string ans = (string)DataBinder.Eval(e.Row.DataItem, "sSubject");
if (ans.Length > 15)
{
objlblsubject.Text = ans.ToString().Substring(0, 15).ToString() + "...";
}
else
{
objlblsubject.Text = ans.ToString();
}
}
else
{
objlblsubject.Text = "No Text";
}
}
}


Thankyou
Rajesh Singh
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com


How to use "If Condition" with <%#Eval("")%> Function


Wednesday, August 26, 2009

Sometimes you might need to use if condition in Eval function,

But if condition is not support by Eval
Not able to use if condition with <%#Eval("sPhone")%>

see this code


-----------IN HTMl code---------------

<%#ReplaceField(Eval("sWebSite").ToString())%>


-----------IN code behind code--------
public string ReplaceField(string stat)
{
string newValue = "";
if (stat == "N/A")
{
newValue=""+stat+"";
}
else
{
newValue = "" + stat + "";
}

return newValue;
}

How to use "If Condition" with <%#Eval("")%> Function




Hello,

Sometimes you might need to use "if condition" in Eval function,

But if condition is not supported by Eval, So here is one of the way, you can
use if condition in Eval Function.

Paste this code in your HTML code,
Here ReplaceField() function will be called from code behind,

-----------IN HTMl code---------------
<td align="left">
<%#ReplaceField(Eval("sWebSite").ToString())%>
</td>


You need to paste this below code in your code behind.

-----------IN code behind code--------
public string ReplaceField(string stat)
{
string newValue = "";
if (stat == "N/A")
{
newValue="<asp:Lable runat='server'>"+stat+"</asp:Lable>";
}
else
{
newValue = "<a href='" + stat + "'>" + stat + "</a>";
}

return newValue;
}


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

Add first item as "Select Country" in your DropDownList


Tuesday, August 25, 2009

Hi,

By Using simple one line code,

ddlcases.Items.Insert(0,new ListItem("Select Country","0"));


For More details contact,
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com

How to use IF Condition in sql query


Saturday, August 22, 2009

By using below code you can see, how to use if condition in sql query directly.

"if condition" in sql query

CASE WHEN SNY_Role.bAdd='True' THEN 'Yes' ELSE 'No' END AS RoleAdd

Please put your comment below of this web page,
You can also visit
DotNet Climber Our Website

Thankyou
Rajesh Singh
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

How to Handle NULL value in SQL server 2005




To handle NULL value in the sqlserver 2005

By using ISNull function you can handle null value
SELECT @BeginDate = ISNull(@BeginDate, '1900-01-01')

How to Handle NULL value in SQL server 2005




To handle NULL value in the sqlserver 2005

By using ISNull function you can handle null value .

SELECT @BeginDate = ISNull(@BeginDate, '1900-01-01')



Hope this post will help you,
Rajesh Singh
ASP.Net Developer
Indianic Infotech ltd (India)
rajesh@indianic.com

Store Url, which user tried to Open before Login


Friday, August 21, 2009

First let me explain you the condition in which you can use this code.

Follow this steps
1.if you copy and paste some internal link to browers address bar.
And if that internal link required login authentication.

2.So the page will redirect you to login page and what our code will here is it will copy your first internal link ,which you were trying to open.

3.Now if you provide login details then our code will redirect to your internal link,which you have provided.

Please find below code in ASP.NET
------------------------------------------------
On click event
---------------
If Session("MemberID") = Nothing Then
Session("URL") = Request.Url.AbsoluteUri
Response.Redirect(Application("LinkURL") & "auction/login.aspx")
End If
Session.Remove("URL")

On login page
--------------
If Session("URL") <> Nothing Then
Response.Redirect(Session("URL"))
End If


Please find below code in C#
------------------------------------------------
On click event
---------------
if (Session["UserID"] == null)
{
Session["URL"] = Request.Url.AbsoluteUri;
Response.Redirect("login.aspx");
}
Session.Remove("URL");

On login page
--------------
if (Session["URL"] != null )
{
string redir = (string)Session["URL"];
Response.Redirect(redir);
}

For More details contact,
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com

How to use Data Table




Below code will teach you how to use DataTable in C#

To create datatable and add table to dataset

DataSet ds1=new DataSet();
DataTable table=ds1.Tables.Add("Table");

to add columns in table

table.Columns.add("dDatePlayed");
keep adding..........

to create new row in the datatable

DataRow row=table.NewRow();

to add item in the itemarray of the table and you can add later all together in table

row["dDatePlayed"]=ds.Tables[0].Rows[i]["dDatePlayed"].ToString();

Add complete row form itemarray of table to actual table

table.Rows.Add(row);


For More details contact,
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com

Prefix of Asp.Net Controls


Thursday, August 20, 2009

This post can be help full for the Asp.Net Begineer,
This post suggest you to follow the prefix for Asp.Net Controls in your code to follow the codeing standard.

Control(Prefix)
Label(lbl)
TextBox(txt)
DataGrid(dg)
Button(btn)
ImageButton(ibtn)
Hyperlink(lnk)
DropDownList(ddl)
ListBox(lst)
DataList(dlst)
Repeater(rep)
Checkbox(chk)
CheckBoxList(chk)
RadioButton(rdo)
RadioButtonList(rdo)
Image(img)
Panel(pan)
PlaceHolder(plh)
Calender(cal)
Adrotator(adr)
Table(tbl)
[All] Validators(val)
ValidationSummary(vals)



For More details contact
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com

Create Setup file with Merge Module selected




You Might come across this problem,that you need some software to be installed before
your destop application installed in that computer ,software like windows installer etc.

so you can follow below step, from which you will be able to add software in setup file itself ,which will result ,when you will click on your .exe file ,the setup will first install all the required software then it will install you application.

Steps
1. Select “Release” in Solution configuration Dropdown in toolbar.
2. Open the project for which you want to create the Setup.
3. File->Add->New Project
4. Select “Other Project Types” in Project Types tab and select “Setup and Deployment”.
5. Select “Merge Module Project” give the path and click on Ok.
6. It will open one window. Right click on “Module Retargetable Folder” at the right side of the window
7. select Add->File
8. And add three files (if .Net framework not installed in machine then it will install automatically)
a. Dotnetfx
b. Instmsia
c. WindowsInstaller-KB893803-v2-x86
9. Open Solution Explorer and right click on your created Merge Module and select Build.
10. Repeat step 1 to 3.
11. Select “Setup Wizard”. Give the path and click on Ok
12. Click on Next in Setup Wizard 1.
13. Select “Create a setup for a Windows application” in Choose a project type window and Click on Next.
14. Check the “Primary output from” in Choose project outputs to include.
15. If any extra files or DLL you used in application then add it by click on Add button in Choose files to include window and click on Next.
16. Click Finish.
17. It will open one window.
18. Select Application Folder at the left side in window.
19. Right click on “Primary output from oDeskClient(Active)“ and choose “Create shortcut to Primary output from oDeskClient(Active)” and give the proper name of the shortcut.
20. Drag this shortcut in User’s Desktop folder at the right side of the window. It will create shortcut on your Desktop when u run the setup.
21. Open Solution Explorer and right click on your created Setup and select Properties.
22. Click on Prerequisites button and it will open new window.
23. Select prerequisites you used in application.
24. Select “Download prerequisites from the same location as my application” in specify the install location for prerequisites tab. And click on Ok. Click Apply and after that click on Ok,
25. Right click on Setup and select Add->Merge Module.
26. Select the .msm file which will created in your Merge Module folder -> Release. And click open
27. Open Solution Explorer and right click on your created Setup and select Build.
28. Once run the application.


For More details contact
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com

Create Setup file for destop application in VB.Net




By following below steps, you can create .exe file for your VB.Net Application.

1. Select “Release” in Solution configuration Dropdown in toolbar.
2. Open the project for which you want to create the Setup.
3. File->Add->New Project
4. Select “Setup Wizard”. Give the path and click on Ok
5. Click on Next in Setup Wizard 1.
6. Select “Create a setup for a Windows application” in Choose a project type window and Click on Next.
7. Check the “Primary output from” in Choose project outputs to include.
8. If any extra files or DLL you used in application then add it by click on Add button in Choose files to include window and click on Next.
9. Click Finish.
10. It will open one window.
11. Select Application Folder at the left side in window.
12. Right click on “Primary output from oDeskClient(Active)“ and choose “Create shortcut to Primary output from oDeskClient(Active)” and give the proper name of the shortcut.
13. Drag this shortcut in User’s Desktop folder at the right side of the window. It will create shortcut on your Desktop when u run the setup.
14. Open Solution Explorer and right click on your created Setup and select Properties.
15. Click on Prerequisites button and it will open new window.
16. Select prerequisites you used in application.
17. Select “Download prerequisites from the same location as my application” in specify the install location for prerequisites tab. And click on Ok. Click Apply and after that click on Ok,
18. Open Solution Explorer and right click on your created Setup and select Build.
19. Once run the application.

For More details contact
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com

Show Date and Time like in orkut scrap




Copy Below code in .cs file

//---------------------Code Start ---------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime dt = Convert.ToDateTime(DateTime.Now);
DateTime dt1 = Convert.ToDateTime("3/19/2009 4:46:08 PM");
TimeSpan ts = dt1.Subtract(dt);

if (Math.Abs(ts.Days) == 0)
{
string shortmonth = Convert.ToString(CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[(dt1.Month - 1)].ToString());
Response.Write(shortmonth + " " + dt1.Day + " (" + Math.Abs(ts.Minutes) + " Minutes ago)");
}
else
{
if (Math.Abs(ts.Days) < 7)
{
string shortmonth = Convert.ToString(CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[(dt1.Month - 1)].ToString());
Response.Write(shortmonth + " " + dt1.Day + " (" + Math.Abs(ts.Days) + " Days ago)");
}
else
{
string shortmonth = Convert.ToString(CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[(dt1.Month - 1)].ToString());
Response.Write(shortmonth + " " + dt1.Day + "," + dt1.Year);
}
}
}
}

//---------------------Code End -----------------------------------------

For More details contact
Rajesh Singh
Asp.Net Developer
Indianic infotech ltd (India)
rajesh@indianic.com