Category Archives: Java Script

ASp.Net 2.0 GridView Delete Button Confirmation pop-up

While using GridView in pages wants to confirm the deletion form user. To do this we can take him to another page having GUI to confirm and then delete the record on that page.

Instead of doing so much hardwork you can achieve this on the same page with a little bit of extra code.


Add a client alert script to the delete button of every row.
In the delete event of GridView delete the record.


Page’s Aspx Design File Begins
<asp:GridView ID=”gvFaq” runat=”server” AutoGenerateColumns=”False” CellPadding=”4″
ForeColor=”#333333″ GridLines=”None” OnRowDataBound=”gvFaq_RowDataBound” OnRowDeleting=”gvFaq_RowDeleting”>
<FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
<asp:BoundField DataField=”slno” HeaderText=”Sl No.” />
<asp:BoundField DataField=”cHeading” HeaderText=”Question” />
<asp:BoundField DataField=”cPosition” HeaderText=”Position” />
<asp:CommandField HeaderText=”Manage” ShowSelectButton=”True” />
<asp:CommandField HeaderText=”Delete” ShowDeleteButton=”True” />
<PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
<HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<EditRowStyle BackColor=”#999999″ />
<AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
Page’s Aspx Design File Ends

Database Design FIle

Code Behind File Begins
1) Import Namespaces
using System.Data.SqlClient;

2) Declare global variables which will be used in page
DataSet ds = new DataSet();
SqlDataAdapter da;
SqlConnection myConnection;
String connStr = “your database connection string goes here”‘;
String sql = string.Empty;

3) Page Load event
protected void Page_Load(object sender, EventArgs e)
if (Page.IsPostBack == false)

4) GridView filling method
private void fillGridView()

myConnection = new SqlConnection(connStr);
sql = “select * from tblFaq order by slno desc”;

da = new SqlDataAdapter(sql, myConnection);
da.Fill(ds, “tblFaq”);

gvFaq.DataSource = ds.Tables[0];

catch (Exception ex)

5) In the RowDataBound event add a java script event to the delete cell.
protected void gvFaq_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Cells[4].Attributes.Add(“onClick”, “return confirm(‘Are you sure you want to delete the record?’);”);

6) Handle the RowDeleting event to delete the record of current row.

protected void gvFaq_RowDeleting(object sender, GridViewDeleteEventArgs e)
myConnection = new SqlConnection(connStr);
sql = “delete from tblFaq where slno = ” + Convert.ToInt32(gvFaq.Rows[e.RowIndex].Cells[0].Text);
SqlCommand oldcom = new SqlCommand(sql, myConnection);
Response.Write(“Record Deleted”);
catch (Exception ex)
lblError.Text = ex.Message;

Code Behind File Ends
The output will look like this

For Source Code visit

Show Message Box in

It’s not possible to show an alert message box in using server side code (like windows apps, msgbox).

But this can be achieved by generating a client side Java Script code in a server-side event.


Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
showMsg(“hey there”, Me)
End Sub

Public Sub showMsg(ByVal sMsg As String, ByVal frm As Web.UI.Page)

Dim sb As New System.Text.StringBuilder
Dim oFormObject As System.Web.UI.Control

sMsg = sMsg.Replace(“‘”, “\'”)
sMsg = sMsg.Replace(Chr(34), “\” & Chr(34))
sMsg = sMsg.Replace(vbCrLf, “\n”)
sMsg = “<script language=javascript>alert(“”” & sMsg & “””)</script>”

sb = New System.Text.StringBuilder

For Each oFormObject In frm.Controls
If TypeOf oFormObject Is HtmlForm Then
Exit For
End If
oFormObject.Controls.AddAt(oFormObject.Controls.Count, New LiteralControl(sb.ToString()))
End Sub


On the button click event i write a JavaScript method to the page, the JavaScript method is displaying the message your pass to the method  showMsg().

Please don’t pass special characters as the message, as they will conflict with the special characters of JavaScript and message won’t come properly.

For more articles updates and sourcecodes please visit dotnetcoderoom

Update database data using SQL Cursors based on conditions

Recently one of my MSSQL database got SQL Injection attacks.
Hacker put some JavaScript code in all row data to get hits on his website (which is apparently blocked, and soon my site will also be got banned if it keep happening for a long time).

Because the database has a huge amount of data, i can’t update table data using ExecuteNonQuery commands as it will result in a great overload on database and network resources.

So I decided to make something logical which is fast and logical way to handle this situation, and at last i ended up with writing a cursor.

This cursor finds a specific string and replaces it with null.


Create a table with sample data

create table tblDataUpdateTest
slno int identity(1,1),
cName varchar(50),
cRemarks varchar(50),

insert into tblDataUpdateTest (cName, cRemarks) values (‘name1′,’some text here BADCODE’)
insert into tblDataUpdateTest (cName, cRemarks) values (‘name2′,’raBADCODEhul’)
insert into tblDataUpdateTest (cName, cRemarks) values (‘name3′,’dj BADCODEis devil’)

select * from tblDataUpdateTest

Create Cursor to update database table

Declare @@counter int
set @@counter=0
Declare @@slno int
Declare @@cRemarks varchar(100)

Declare tmepTbl cursor
Select slno,cRemarks from tblDataUpdateTest

Open tmepTbl /* Opening the cursor */

fetch next from tmepTbl
into @@slno,@@cRemarks

while @@fetch_Status-1

Update tblDataUpdateTest
set cRemarks = Replace(@@cRemarks,’BADCODE’,”)
where slno = @@slno

fetch next from tmepTbl
into @@slno, @@cRemarks

set @@counter=@@counter+1
close tmepTbl
Deallocate tmepTbl


By running this cursor all the occurrences of BADCODE will be eliminated from the specified table.

I used word BADCODE here but in my actual data it was a java script tag which was creating all the problem.

For more articles updates and sourcecodes please visit dotnetcoderoom

Change fore color of control using Java Script

Many times we need to set the fore color or back color of controls (e.g. label, button etc).

This can be achieved using CSS (supposing all of you know that) also but we can also do it using Java Script.

This is a simple java script trick so you can use it with html controls as well as server controls.

Here is an example for doing this :


<a href=”contact.aspx” class=”mainMenuNormalStyle” onmouseover=”style.color=’#FFB537′;”

<a href=”contact.aspx” class=”mainMenuNormalStyle” onmouseover=”style.backgroundcolor=’#FFB537′;”

In the above code example i set the value of style collection’s color property on the “onmouseover” event and reversed on “onmouseout” event
For more articles updates and sourcecodes please visit dotnetcoderoom

Disable the right click on a web page

Some website has right-click disabled so that user cant copy paste using mouse, or cant save images contained into webpage.

Copy paste this code

<body oncontextmenu=”return false;”>
Html page content here

**End of article**

For more articles updates and sourcecodes please visit dotnetcoderoom