In This Post I'll Show you How You Can show a Confirm MessageBo using JavaScript Using Asp.Net.
I will use Javascript's confirm function which shows a Confirm MessageBox to the user. Clicking Ok will cause a postback to the Webserver and clicking Cancel will close the MessageBox and won't cause a Postback.
First of All add a Asp.Net Button to the Web Form, Now add the following markup to the OnClientClick property of the Button Control.
OnClientClick = "javascript:return confirm('Are You Sure You Want to Continue???');"
Here is the Complete markup of the Button Control.
<asp:Button ID="Button1" runat="server" OnClientClick="javascript:return confirm('Are You Sure You Want to Continue???');" Text="Button" />
Run the Web Application and Click the Button.
It will show the Confirm MessageBox to the User against which the user can act accordingly.
Friday, March 5, 2010
How to make a Watermark TextBox in Asp.net
In this post i'll show you how we can make a Watermark TextBox Asp.Net.
We Can use Either a HTML TextBox or a Asp.Net TextBox Server Control and make it a Watermark TextBox.
Now a Watermark TextBox reacts as follows:
Converting a Asp.Net Server Control to Watermark TextBox.
Firstly Drag and Drop a Asp.Net TextBox on to the Web Form and set its Text Property to the Default value that should be shown when the web page is rendered.
To make this kind of effect we can use two attributes of Textbox i.e. onfocus and onblur.
In the Form_Load Handler Type the following Code:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onfocus", "if(this.value=='Find...')value='';");
TextBox1.Attributes.Add("onblur", "if(this.value=='')value='Find...';");
}
It will Render the Asp.Net TextBox as a Watermark TextBox.
Converting a HTML Server Control to Watermark TextBox.
Firstly Drag and Drop a HTML TextBox on to the Web Form and set its value attribute to the Default value that should be shown when the web page is rendered.
To make this kind of effect we can use two attributes of HTML Textbox i.e. onfocus and onblur.
So, here are the attributes markup of the HTML Watermark TextBox. Just Set these three attributes in the Html Markup of your HTML Textbox control.
value="Find..."
onfocus="if(this.value=='Find...')value='';"
onblur="if(this.value=='')value='Find...';"
It will show a Watermark TextBox to the user.
We Can use Either a HTML TextBox or a Asp.Net TextBox Server Control and make it a Watermark TextBox.
Now a Watermark TextBox reacts as follows:
- If the Watermark TextBox gets focus, It's default text gets dissappeared making the textbox empty.
- When the Watermark TextBox looses focus, If it contains any value, it will be displayed in the Textbox. If it doesn't contains any value, It will show the Default value.
Converting a Asp.Net Server Control to Watermark TextBox.
Firstly Drag and Drop a Asp.Net TextBox on to the Web Form and set its Text Property to the Default value that should be shown when the web page is rendered.
To make this kind of effect we can use two attributes of Textbox i.e. onfocus and onblur.
In the Form_Load Handler Type the following Code:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onfocus", "if(this.value=='Find...')value='';");
TextBox1.Attributes.Add("onblur", "if(this.value=='')value='Find...';");
}
It will Render the Asp.Net TextBox as a Watermark TextBox.
Converting a HTML Server Control to Watermark TextBox.
Firstly Drag and Drop a HTML TextBox on to the Web Form and set its value attribute to the Default value that should be shown when the web page is rendered.
To make this kind of effect we can use two attributes of HTML Textbox i.e. onfocus and onblur.
So, here are the attributes markup of the HTML Watermark TextBox. Just Set these three attributes in the Html Markup of your HTML Textbox control.
value="Find..."
onfocus="if(this.value=='Find...')value='';"
onblur="if(this.value=='')value='Find...';"
It will show a Watermark TextBox to the user.
Saturday, January 30, 2010
How to add Mouse Hover Effect on Grid View
Recently, One of My Friend asked me that how can we add Mouse Hover Effect on Grid View. So I want to share it with you too.
Firstly, This is the Sql Table Structure:
Create a New Website by clicking File->New Website.
Then,
Add a connection String to the Web.config file
Then, Add a GridView to the .aspx page
Now Add a style tag in the head tag of the .aspx page. Here is the .aspx page.
Now, In the Code Behind file
Add a class level variable to store Connection String and in the pageload event retrieve the connection string that we stored in the web.config file. For eg:
static string constring;
protected void Page_Load(object sender, EventArgs e)
{
constring = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
BindData();
}
protected void BindData()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
SqlDataAdapter adap = new SqlDataAdapter("Select * from MyCustomers", con);
DataSet ds = new DataSet();
adap.Fill(ds, "MyCustomers");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
foreach (GridViewRow row in GridView1.Rows)
{
row.Attributes.Add("onmouseover", "className = 'rowStyle'");
row.Attributes.Add("onmouseout", "className = ''");
}
}
And run the Web site, Output will be shown like this
Conditional Formatting on GridView
In this post i'll show you how we can do conditional formatting on GridView Web Server Control.
Firstly, This is the Sql Table Structure:
Create a New Website by clicking File->New Website.
Then,
Add a connection String to the Web.config file
Then, Add a GridView to the .aspx page and change the DataKeyNames attribute to the ID Column
Now, In the Code Behind file
Add a class level variable to store Connection String and in the pageload event retrieve the connection string that we stored in the web.config file. For eg:
static string constring;
protected void Page_Load(object sender, EventArgs e)
{
constring = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
BindData();
}
protected void BindData()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
SqlDataAdapter adap = new SqlDataAdapter("Select * from MyCustomers", con);
DataSet ds = new DataSet();
adap.Fill(ds, "MyCustomers");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
In the RowCreated event of the Grid View add the following code:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
DataRowView gridrow = (DataRowView)e.Row.DataItem;
string sex = Convert.ToString(gridrow["Country"]);
if (sex.Trim() == "India")
{
e.Row.BackColor = System.Drawing.Color.LightBlue;
}
}
}
And run the Web site, Output will be shown like this
Saturday, January 16, 2010
How to find Duplicate Records from a SQL Table
In this post i'll show you how can we retrieve Duplicate Records from a SQL Table.
The syntax is
Select Column_Name,COUNT(*) from Table_Name GROUP BY Column_Name HAVING COUNT(*)>1 ORDER BY COUNT(*) DESC
WHERE Column_Name is the name of the column in which we are finding the duplicate records.
I am taking Titles table in pubs database as an example.
there are many titles which are published by different different Publishers. So, Suppose we want to find out how many titles have been published by a particular Publishers. To retrieve the desired information we have to write the query below.
select pub_id,COUNT(*) from titles group by pub_id having COUNT(*)>1 order by COUNT(*) desc
this will yield the result as:
The syntax is
Select Column_Name,COUNT(*) from Table_Name GROUP BY Column_Name HAVING COUNT(*)>1 ORDER BY COUNT(*) DESC
WHERE Column_Name is the name of the column in which we are finding the duplicate records.
I am taking Titles table in pubs database as an example.
there are many titles which are published by different different Publishers. So, Suppose we want to find out how many titles have been published by a particular Publishers. To retrieve the desired information we have to write the query below.
select pub_id,COUNT(*) from titles group by pub_id having COUNT(*)>1 order by COUNT(*) desc
this will yield the result as:
Hope this will help you.
Subscribe to:
Posts (Atom)