实例学习DataGrid教程2

王朝other·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

更新数据库在 Web 应用程序中可能经常很棘手。针对这种情况,DataGrid 控件提供了一些使更新更容易的内置支持。为了允许对行进行编辑,DataGrid 支持整型 EditItemIndex 属性,该属性指示网格的哪一行应该是可编辑的。设置了该属性后,DataGrid 按该索引将行呈现为文本输入框,而不是简单的标签。值 -1(默认值)指示没有行是可编辑的。页可以在服务器端窗体中包含 DataGrid,并通过 DataGrid 的对象模型获取对编辑数据的访问。

为了确定哪一行应该是可编辑的,需要一种方法接受用户关于他们希望编辑哪一行的输入。DataGrid 可以包含一个 EditCommandColumn 来呈现激发三个特殊事件的链接:EditCommand、UpdateCommand 和 CancelCommand。EditCommandColumn 以声明方式添加到 DataGrid 的 Columns 集合,如下面的示例所示。

<ASP:DataGrid id="MyDataGrid" runat="server"

...

OnEditCommand="MyDataGrid_Edit"

OnCancelCommand="MyDataGrid_Cancel"

OnUpdateCommand="MyDataGrid_Update"

DataKeyField="au_id"

>

<Columns>

<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />

</Columns>

</ASP:DataGrid>

在 DataGrid 标记本身上,将事件处理程序连到从 EditCommandColumn 激发的每个命令。这些处理程序的 DataGridCommandEventArgs 参数使您得以直接访问由用来设置 DataGrid 的 EditItemIndex 的客户端选择的索引。注意,需要重新绑定 DataGrid 以使更改生效,如下面的示例所示。

public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E) {

MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;

BindGrid();

}

Public Sub MyDataGrid_Edit(sender As Object, E As DataGridCommandEventArgs)

MyDataGrid.EditItemIndex = E.Item.ItemIndex

BindGrid()

End Sub

public function MyDataGrid_Edit(sender:Object, E:DataGridCommandEventArgs) : void {

MyDataGrid.EditItemIndex = int(E.Item.ItemIndex);

BindGrid();

}

当编辑某行 DataGrid 时,EditCommandColumn 呈现 Update 和 Cancel 链接。如果客户端选择 Cancel,只需将 EditItemIndex 设置回 -1。但如果客户端选择 Update,则需要对数据库执行更新命令。执行更新查询要求知道希望更新的行的数据库中的主键。为支持此要求,DataGrid 公开一个可以设置为主键字段名的 DataKeyField 属性。在连到 UpdateCommand 的事件处理程序中,可以从 DataGrid 的 DataKeys 集合检索键名。使用事件的 ItemIndex 在此集合中索引,如下面的示例所示。

myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];

myCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CType(E.Item.ItemIndex, Integer))

myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[int(E.Item.ItemIndex)];

在 Update 事件处理程序的最后,将 EditItemIndex 设置回 -1。下面的示例说明此代码的运行。

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

SqlConnection myConnection;

protected void Page_Load(Object Src, EventArgs E)

{

myConnection=new SqlConnection("server=jeff;uid=sa;password=;database=pubs");

if (!IsPostBack)

BindGrid();

}

public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)

{

MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;

BindGrid();

}

public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)

{

MyDataGrid.EditItemIndex = -1;

BindGrid();

}

public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)

{

String updateCmd = "UPDATE Authors SET au_lname = @LName, au_fname = @FName, phone = @Phone, "

+ "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";

SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);

myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));

myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));

myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));

myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));

myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));

myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));

myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));

myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));

myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));

myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@State","@Zip","@Contract"};

int numCols = e.Item.Cells.Count;

for (int i=2; i<numCols-1; i++) //跳过第一、第二和最后一列

{

String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text;

// 检查在所需字段中是否有空值

if (i<6 && colvalue == "")

{

Message.InnerHtml = "错误:“作者 ID”、“姓名”或“电话”不允许使用空值";

Message.Style["color"] = "red";

return;

}

myCommand.Parameters[cols[i-1]].Value = colvalue;

}

//追加最后一行,将 true/false 值转换为 0/1

if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, "True", true)==0)

myCommand.Parameters["@Contract"].Value = "1";

else

myCommand.Parameters["@Contract"].Value = "0";

myCommand.Connection.Open();

try

{

[1] [2] [3] [4] [5] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航