DotNet Tutorials

Server Intellect

 Delete MutiRows with mouse drawing using ASP.NET and C#

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.
using System.Data.SqlClient;

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

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

private void DataBind()
{
string sql = "select * from ContactInfo";
DataSet ds=GetDataSet(sql);
string[] cols = new string[ds.Tables[0].Columns.Count];
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
cols[i]=ds.Tables[0].Columns[i].ColumnName;
}
string[] width = new string[]{"0","40","15","15","15","15"};
this.WebDataGrid1.ColumnsField = cols;
this.WebDataGrid1.ColumnsWidth = width;
this.WebDataGrid1.GridID = "WebDataGrid1";
this.WebDataGrid1.GridDataSource = ds.Tables[0];
this.WebDataGrid1.GridBind();
}

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

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType ==ListItemType.Header)
{
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");
}
e.Item.Cells[0].Style.Add("display","none");
}

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

protected void btnDelete_Click(object sender, System.EventArgs e)
{
string id=this.WebDataGrid1.GridSelectItems;//.Split(',');
if (!id.Equals(string.Empty))
{
if (id.EndsWith(","))
{
id = id.Substring(0, id.Length - 1);
}
delete(id);
this.Response.Redirect(Request.Url.ToString(), true);
}
else
{
this.Response.Redirect(Request.Url.ToString(), true);
}
}

private void delete(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
string sql= @"declare @sql nvarchar(400)
set @sql = 'delete from ContactInfo where ID in('+@ID+')'
exec( @sql)";
SqlCommand comm=new SqlCommand(sql,conn);
SqlParameter parm1=new SqlParameter("@ID",SqlDbType.VarChar,20);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
DataBind();
}
#endregion

protected void btnDelete_Click(object sender, System.EventArgs e)
{
string id=this.WebDataGrid1.GridSelectItems;//.Split(',');
if(id.EndsWith(","))
{
id=id.Substring(0,id.Length-1);
}
delete(id);
this.Response.Redirect( Request.Url.ToString(),true );
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!


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.

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

namespace DatagridMuriDelCsharp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
DataBind();
this.btnDelete.Attributes.Add("onclick","return Delete();");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

/
private void InitializeComponent()
{

}
#endregion

#region DataBind
private void DataBind()
{
string sql = "select * from ContactInfo";
DataSet ds=GetDataSet(sql);
string[] cols = new string[ds.Tables[0].Columns.Count];
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
cols[i]=ds.Tables[0].Columns[i].ColumnName;
}
string[] width = new string[]{"0","40","15","15","15","15"};
this.WebDataGrid1.ColumnsField = cols;
this.WebDataGrid1.ColumnsWidth = width;
this.WebDataGrid1.GridID = "WebDataGrid1";
this.WebDataGrid1.GridDataSource = ds.Tables[0];
this.WebDataGrid1.GridBind();
}
#endregion

#region GetDataSet
private DataSet GetDataSet(string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion

#region delete
private void delete(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
string sql= @"declare @sql nvarchar(400)
set @sql = 'delete from ContactInfo where ID in('+@ID+')'
exec( @sql)";
SqlCommand comm=new SqlCommand(sql,conn);
SqlParameter parm1=new SqlParameter("@ID",SqlDbType.VarChar,20);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
DataBind();
}
#endregion

protected void btnDelete_Click(object sender, System.EventArgs e)
{
string id=this.WebDataGrid1.GridSelectItems;//.Split(',');
if(id.EndsWith(","))
{
id=id.Substring(0,id.Length-1);
}
delete(id);
this.Response.Redirect( Request.Url.ToString(),true );
}
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!




Looking for the VB.NET2005 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!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect