Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Delete MutiRows with mouse drawing using ASP.NET and VB
Delete MutiRows with mouse drawing using ASP.NET and VB


ASP.NET Controls Tutorial

This tutorial will show you how to delete multi-rows by drawing mouse.

This tutorial will show you how to delete multi-rows by drawing mouse. First, you will need to import the System.Data.SqlClient namespace for binding data to WebDataGrid1.

Imports System.Data.SqlClient

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Function DataBind() will bind data to WebDataGrid1,and create WebDataGrid1 layout.

Private Sub DataBind()
Dim sql As String = "select * from ContactInfo"
Dim ds As DataSet = GetDataSet(sql)
Dim cols((ds.Tables(0).Columns.Count - 1)) As String
Dim i As Integer
For i = 0 To ((ds.Tables(0).Columns.Count) - 1)
cols(i) = ds.Tables(0).Columns(i).ColumnName
Next i
'string[] width = new string[]{"0","40","15","15","15","15"};
Me.WebDataGrid1.ColumnsField = cols
'this.WebDataGrid1.ColumnsWidth = width;
Me.WebDataGrid1.GridID = "WebDataGrid1"
Me.WebDataGrid1.GridDataSource = ds.Tables(0)
Me.WebDataGrid1.GridBind()
End Sub 'DataBind

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Function DataGrid1_ItemDataBound define mouse even, those even will carry out mouse drawing.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Header Then
e.Item.Attributes.Add("style", "BACKGROUND-IMAGE: url('headeImage.jpg')")
e.Item.ID = "Header"
Else
e.Item.Attributes.Add("onclick", "_SelectTheRow(this)")
e.Item.Attributes.Add("onmousedown", "_OnMouseDown(this)")
e.Item.Attributes.Add("onmouseup", "_OnMouseUp(this)")
e.Item.Attributes.Add("onmouseover", "_OnMouseOver(this)")
e.Item.Attributes.Add("onmouseout", "_OnMouseOut(this)")
e.Item.Style.Add("background-color", "#ffffff")
End If
e.Item.Cells(0).Style.Add("display", "none")
End Sub 'DataGrid1_ItemDataBound

We use the btnDelete event to do multi-rows delete.  Mouse drawing will collect all rows information, and pass those information to delete even.

Private Sub delete(ByVal id As String)
Dim conn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
Dim sql As String = "declare @sql nvarchar(400)" + ControlChars.Cr + ControlChars.Lf + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + "set @sql = 'delete from ContactInfo where ID in('+@ID+')'" + ControlChars.Cr + ControlChars.Lf + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + "exec( @sql)"
Dim comm As New SqlCommand(sql, conn)
Dim parm1 As New SqlParameter("@ID", SqlDbType.VarChar, 20)
parm1.Value = id
comm.Parameters.Add(parm1)
conn.Open()
comm.ExecuteNonQuery()
conn.Close()
DataBind()
End Sub 'delete

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The front end DatagridheadersortCsharp.aspx page looks something like this:

<asp:Button id="btnDelete" style="Z-INDEX: 102; LEFT: 11px; POSITION: absolute; TOP: 206px" runat="server" Text="Delete" onclick="btnDelete_Click"></asp:Button>
<br />
<br />
<br />
<br />
<uc1:WebDataGrid id="WebDataGrid1" runat="server"></uc1:WebDataGrid>
</fieldset>

<SCRIPT type="text/javascript"><!--
function _GoToPageDetail()
{
var childs = document.getElementById("WebDataGrid1_DataGrid1").rows;
var url = "";
for(var i=1;i<childs.length;i++)
{
var child = childs[i];
if(child.style.backgroundColor=='lavender')
{
var tds = child.children;
url = "detatil.aspx?id="+tds[0].innerText;
break;
}
}
if(url!="")
{
window.location.href = url;
}
else if(childs.length>1)
{
var child = childs[1];
var tds = child.children;
window.location.href ="detatil.aspx?id="+tds[0].innerText;
}
}

function Delete()
{
SelectDeleteRows();
var hid = document.getElementById("WebDataGrid1_hiddenDelete");
if(hid=="")
alert('You need to select a row in the list before selecting Delete.\r\nPlease select a row and try again.');
else if(window.confirm("Are you sure to delete all these?"))
{
document.getElementById("btnDelete").click();
}
else
{
return false;
}
}

function _DoubleTheRow(obj)
{
var childs = obj.children;
var td1 = childs[0];
var txtID = td1.innerText;
window.location.href ="detatil.aspx?id"+txtID
}
--></SCRIPT>

The flow for the code behind page is as follows.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient

Namespace datagridMutiDelVB
Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
s
If Not Page.IsPostBack Then
DataBind()
Me.btnDelete.Attributes.Add("onclick", "return Delete();")
End If
End Sub 'Page_Load

Protected Overrides Sub OnInit(ByVal e As EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub 'OnInit

Private Sub InitializeComponent()

End Sub 'InitializeComponent

Private Sub DataBind()
Dim sql As String = "select * from ContactInfo"
Dim ds As DataSet = GetDataSet(sql)
Dim cols((ds.Tables(0).Columns.Count - 1)) As String
Dim i As Integer
For i = 0 To ((ds.Tables(0).Columns.Count) - 1)
cols(i) = ds.Tables(0).Columns(i).ColumnName
Next i
'string[] width = new string[]{"0","40","15","15","15","15"};
Me.WebDataGrid1.ColumnsField = cols
'this.WebDataGrid1.ColumnsWidth = width;
Me.WebDataGrid1.GridID = "WebDataGrid1"
Me.WebDataGrid1.GridDataSource = ds.Tables(0)
Me.WebDataGrid1.GridBind()
End Sub 'DataBind

Private Function GetDataSet(ByVal sql As String) As DataSet
Dim constring As String = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
Dim sda As New SqlDataAdapter(sql, constring)
Dim ds As New DataSet()
sda.Fill(ds)
Return ds
End Function 'GetDataSet

Private Sub delete(ByVal id As String)
Dim conn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
Dim sql As String = "declare @sql nvarchar(400)" + ControlChars.Cr + ControlChars.Lf + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + "set @sql = 'delete from ContactInfo where ID in('+@ID+')'" + ControlChars.Cr + ControlChars.Lf + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + ControlChars.Tab + "exec( @sql)"
Dim comm As New SqlCommand(sql, conn)
Dim parm1 As New SqlParameter("@ID", SqlDbType.VarChar, 20)
parm1.Value = id
comm.Parameters.Add(parm1)
conn.Open()
comm.ExecuteNonQuery()
conn.Close() DataBind()
End Sub 'delete

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim id As String = Me.WebDataGrid1.GridSelectItems '.Split(',');
If id.EndsWith(",") Then
id = id.Substring(0, id.Length - 1)
End If
delete(id)
Me.Response.Redirect(Request.Url.ToString(), True)
End Sub 'btnDelete_Click
End Class 'WebForm1
End Namespace 'WebApplication3

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks