在DataGrid中经弹出窗口确认后执行操作(删除)。(附在DataGrid中修改,添加记录)详见代码!!
发表于:2007-06-30来源:作者:点击数:
标签:
前台代码: asp:Label id=Message runat=server/asp:LabelBR asp:datagrid id=BigClassList Width=100% CellPadding=3 Runat=server DataKeyField=BigClassID OnUpdateCommand=MyDataGrid_Update OnCancelCommand=MyDataGrid_Cancel OnEditCommand=MyDataGrid
前台代码:
<asp:Label id="Message" runat="server"></asp:Label><BR>
<asp:datagrid id="BigClassList" Width="100%" CellPadding="3" Runat="server" DataKeyField="BigClassID" OnUpdateCommand="MyDataGrid_Update" OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit" OnDeleteCommand="MyDataGrid_Del" OnSortCommand="MyDataGrid_Sort" OnItemCreated="MyDataGrid_ItemCreated" AutoGenerateColumns="False" AllowSorting="True">
<HeaderStyle Font-Bold="True" BackColor="Gainsboro"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="BigClassID" ReadOnly="True" HeaderText="大类编号" SortExpression="BigClassID"></asp:BoundColumn>
<asp:BoundColumn DataField="BigClassName" HeaderText="大类名称" SortExpression="BigClassName"></asp:BoundColumn>
<asp:BoundColumn DataField="BigClassSortID" HeaderText="排序编号" SortExpression="BigClassSortID"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="返回" EditText="编辑"></asp:EditCommandColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="cmdDel" runat="server" Text="删除" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<table height="23" cellSpacing="0" cellPadding="3" width="100%" boder="0">
<tr>
<
td bgColor="#efefef">添加新大类</td>
</tr>
<TR>
<
TD>类别名称:
<asp:TextBox id="bclassName" runat="server"></asp:TextBox>
<asp:Label id="AddMessage" runat="server"></asp:Label></TD>
</TR>
<tr>
<td>排序编号:
<asp:TextBox id="bclassID" runat="server"></asp:TextBox></td>
</tr>
<TR>
<TD>
<asp:Button id="btnAddbclass" runat="server" Text="添加大类(A)"></asp:Button></TD>
</TR>
</table>
后台代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
myConn=new OdbcConnection(ConnStr);
if (!IsPostBack)
BindGrid("BigClassID");
}
public void BindGrid(string sortfield)
{
// 绑定数据
// OdbcDataAdapter myDataAdapter= new OdbcDataAdapter("select * from BigClass order by BigClassID desc", myConn);
// DataSet ds = new DataSet();
// myDataAdapter.Fill(ds, "BigClass");
// BigClassList.DataSource=ds.Tables["BigClass"].DefaultView;
// BigClassList.DataBind();
OdbcDataAdapter myDataAdapter = new OdbcDataAdapter("select * from BigClass", myConn);
DataSet ds = new DataSet();
myDataAdapter.Fill(ds, "BigClass");
DataView Source = ds.Tables["BigClass"].DefaultView;
Source.Sort = sortfield;
BigClassList.DataSource=Source;
BigClassList.DataBind();
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
// 将当前行置为编辑状态
BigClassList.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid("BigClassID");
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
// 将当前行置为非编辑状态
BigClassList.EditItemIndex = -1;
BindGrid("BigClassID");
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
// 更新当前行
string BigClassID=e.Item.Cells[0].Text;
string BigClassName=Regex.Replace(((TextBox)e.Item.Cells[1].Controls[0]).Text,"@#","@#@#");
string BigClassSortID=((TextBox)e.Item.Cells[2].Controls[0]).Text;
String updateCmd = "UPDATE BigClass SET BigClassName =@#" + BigClassName + "@#,"
+ "BigClassSortID=" + BigClassSortID
+ " where BigClassID =" + BigClassID;
myComm = new OdbcCommand(updateCmd, myConn);
myComm.Connection.Open();
try
{
myComm.ExecuteNonQuery();
// Response.Write("<font color=red><b>已更新记录</b><font><br>");
Message.Text = "<font color=red><b>已更新记录</b><font><br>";
BigClassList.EditItemIndex = -1;
}
catch (OdbcException exc)
{
Message.Text="<font color=red><B>错误:" + exc.Message.ToString() + "</b></font>";
}
myComm.Connection.Close();
BindGrid("BigClassID");
}
public void MyDataGrid_ItemCreated(Object sender, DataGridItemEventArgs e)
{
// 删除大类
LinkButton l=new LinkButton();
switch(e.Item.ItemType)
{
case(ListItemType.Item):
l=(System.Web.UI.WebControls.LinkButton)e.Item.FindControl("cmdDel");
l.Attributes.Add("OnClick","return confirm(@#确定要删除该大类吗?@#);");
break;
case(ListItemType.AlternatingItem):
l=(System.Web.UI.WebControls.LinkButton)e.Item.FindControl("cmdDel");
l.Attributes.Add("OnClick","return confirm(@#确定要删除该大类吗?@#);");
break;
}
}
protected void MyDataGrid_Del(Object sender, DataGridCommandEventArgs e)
{
myConn=new OdbcConnection(ConnStr);
myConn.Open();
string BigClassID=e.Item.Cells[0].Text;;
string deleteCmd = "DELETE from BigClass where BigClassID =" + BigClassID;
myComm=new OdbcCommand(deleteCmd,myConn);
myComm.ExecuteNonQuery();
BindGrid("BigClassID");
}
protected void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
BindGrid(e.SortExpression);
}
private void btnAddbclass_Click(object sender, System.EventArgs e)
{
string bClassName=Regex.Replace(bclassName.Text,"@#","@#@#");
string bClassID=bclassID.Text;
if(bClassName!="" & bClassID!="")
{
myConn=new OdbcConnection(ConnStr);
myConn.Open();
string InsertInto="INSERT INTO BigClass (BigClassName,BigClassSortID) values ("
+ "@#" + bClassName + "@#,"
+ bClassID + ")";
// Response.Write(InsertInto);
myComm=new OdbcCommand(InsertInto,myConn);
myComm.ExecuteNonQuery();
bclassName.Text="";
bclassID.Text="";
AddMessage.Text="<font color=red><B>已经添加新的文章大类!</B></Font>";
BindGrid("BigClassID");
}
else
{
AddMessage.Text="<font color=red><B>请填写完整!</B></Font>";
}
}
原文转自:http://www.ltesting.net