<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
CellPadding = "2"
EditItemStyle-BackColor="yellow"
DataKeyField="ISBN"//后面可以使用DataKeys集合的行索引来获取该行的ISBN栏的值
OnEditCommand="DoItemEdit"//Edit事件处理
OnUpdateCommand="DoItemUpdate"//Update事件
OnCancelCommand="DoItemCancel"//Cancel事件
AutoGenerateColumns="False">
<Columns>
<ASP:BoundColumn DataField="ISBN" HeaderText="ISBN" ReadOnly="True" />//isbn栏(显示文本)
<ASP:TemplateColumn HeaderText="Title">//模版列
<ItemTemplate>
<ASP:Label Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>//EditItemIndex指向行的栏将显示此模版
<ASP:TextBox id="txtTitle" Size="60"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
</EditItemTemplate>
</ASP:TemplateColumn>
<ASP:BoundColumn DataField="PublicationDate" HeaderText="Published" />//Published文本栏
<ASP:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update" />//编辑栏(显示为链接形式)
</Columns>
</ASP:DataGrid>
</form>
脚本:
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindDataGrid(); // create data set and bind to grid control
}
void DoItemEdit(Object objSource, DataGridCommandEventArgs objArgs)
{
lblSQL.Text = ""; // clear text from label that shows SQL statement
// set the EditItemIndex property of the grid to this item's index
//设置EditItemIndex属性为用户所电击的行标
MyDataGrid.EditItemIndex = objArgs.Item.ItemIndex;
BindDataGrid(); // bind the data and display it
}
void DoItemUpdate(Object objSource, DataGridCommandEventArgs objArgs)
{
// get a reference to the title and publication date text boxes
//objArgs.Item为激发事件的控件所在行的引用,FindControl则寻找id为txtTitle的textbox
TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
//取出激发事件的控件所在行第三个单元中第一个控件(因为该Published栏无ID
//故不能使用findcontrol方法来查找)
TextBox objPubDateCtrl = (TextBox)objArgs.Item.Cells[2].Controls[0];
// create a suitable SQL statement and execute it
string strSQL = "UPDATE Booklist SET Title='" + objTitleCtrl.Text + "', "
+ "PublicationDate='" + objPubDateCtrl.Text + "' "
+ "WHERE ISBN='" + MyDataGrid.DataKeys[objArgs.Item.ItemIndex] + "'";
ExecuteSQLStatement(strSQL);//数据库更新操作
// set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataGrid.EditItemIndex = -1;//设为初始值
BindDataGrid(); // bind the data and display it
}
void DoItemCancel(Object objSource, DataGridCommandEventArgs objArgs)
{
// set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataGrid.EditItemIndex = -1;
BindDataGrid(); // bind the data and display it
}
void ExecuteSQLStatement(string strSQL)
{
// this is where the SQL statement would be executed against the
// original data source. In this example, we're simply displaying
// the statement in a Label on the page
lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" + strSQL;
}
void BindDataGrid()
{
// get connection string from ../global/connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
// create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610025%'";
// create a variable to hold an instance of a DataReader object
OleDbDataReader objDataReader;
try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// open the connection to the database
objConnect.Open();
// create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);
// execute the SQL statement against the command to get the DataReader
objDataReader = objCommand.ExecuteReader();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source + "<p />";
return; // and stop execution
}
// set the DataSource property and bind the grid
MyDataGrid.DataSource = objDataReader;
MyDataGrid.DataBind();
}
</script>
注:
TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
TextBox objPubDateCtrl = (TextBox)objArgs.Item.Cells[2].Controls[0];
这两条语句而言,后者最适用于当栏是普通的BoundColumn或是自动生成的情况(没有定义ID)--而不适用于利用模版创建的自定义栏(
这就是为什么对Title栏使用FindControl的原因(定义了ID))