下面的原例子的代码 <%@Page Language="C#"%><%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.OleDb" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head> <title>Editing Data in a DataGrid Control</title> <style type="text/css"> body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt} input {font-family:Tahoma,Arial,sans-serif; font-size:9pt} .heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold} .subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px} .cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial; font-size:14pt; font-weight:bold; padding:8px; color:green} .rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif; font-size:10pt} .rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}</style></head> <body bgcolor="#ffffff"> <span class="heading">Editing Data in a DataGrid Control</span><hr /> <!---------------------------------------------------------------------------><div id="outError" runat="server" /><ASP:Label id="lblSQL" runat="server" /><p /><form runat="server"> <ASP:DataGrid id="MyDataGrid" runat="server" CellPadding = "2" EditItemStyle-BackColor="yellow" DataKeyField="ISBN" OnEditCommand="DoItemEdit" OnUpdateCommand="DoItemUpdate" OnCancelCommand="DoItemCancel" AutoGenerateColumns="False"> <Columns> <ASP:BoundColumn DataField="ISBN" HeaderText="ISBN" ReadOnly="True" /> <ASP:TemplateColumn HeaderText="Title"> <ItemTemplate> <ASP:Label Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" /> </ItemTemplate> <EditItemTemplate> <ASP:TextBox id="txtTitle" Size="60" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" /> </EditItemTemplate> </ASP:TemplateColumn> <ASP:BoundColumn DataField="PublicationDate" HeaderText="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 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 TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle"); 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 web.config string strConnect = ConfigurationSettings.AppSettings["DsnWroxBooksOleDb"]; // create a SQL statement to select some rows from the database string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%7%'"; // 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><!---------------------------------------------------------------------------> <hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] +copy;2004 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> - <a class="cite" href="http://www.wrox.com/books/0764558900.shtml">Professional ASP.NET 1.1</a> (ISBN: 0-7645-5890-0)</span> </body> </html>可以运行
TextBox objauid1 = (TextBox)objArgs.Item.Cells[1].Controls[0];
在Watch中看objArgs.Item.Cells[1].Controls[0]是什么类型的
一般来说,肯定会有。反正错误显示,Controls[0]肯定不是一个TextBox。
2,用这种方式
TextBox objauid1 = (TextBox)objArgs.Item.Cells[1].FindControl("控件id");
不行吗
<%@Page Language="C#"%><%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Editing Data in a DataGrid Control</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}</style></head>
<body bgcolor="#ffffff">
<span class="heading">Editing Data in a DataGrid Control</span><hr />
<!---------------------------------------------------------------------------><div id="outError" runat="server" /><ASP:Label id="lblSQL" runat="server" /><p /><form runat="server"> <ASP:DataGrid id="MyDataGrid" runat="server"
CellPadding = "2"
EditItemStyle-BackColor="yellow"
DataKeyField="ISBN"
OnEditCommand="DoItemEdit"
OnUpdateCommand="DoItemUpdate"
OnCancelCommand="DoItemCancel"
AutoGenerateColumns="False"> <Columns> <ASP:BoundColumn DataField="ISBN" HeaderText="ISBN" ReadOnly="True" /> <ASP:TemplateColumn HeaderText="Title">
<ItemTemplate>
<ASP:Label Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<ASP:TextBox id="txtTitle" Size="60"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
</EditItemTemplate>
</ASP:TemplateColumn> <ASP:BoundColumn DataField="PublicationDate" HeaderText="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
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
TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
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 web.config
string strConnect = ConfigurationSettings.AppSettings["DsnWroxBooksOleDb"]; // create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%7%'"; // 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><!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>]
+copy;2004 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/books/0764558900.shtml">Professional ASP.NET 1.1</a> (ISBN: 0-7645-5890-0)</span>
</body>
</html>可以运行
都是修改后的值修改前的值无法拿到
行 41: TextBox objauid1 = (TextBox)objArgs.Item.Cells[1].Controls[0];
行 42:
行 43: string strconn = "update Authors SET au_id='"+objauid2.Text+"' WHRER au_id='"+objauid1.Text+"'";检查auid2、auid1、objArgs.Item.Cells[1].Controls[0];这三个控件是否是TextBox
检查字段au_id是int型还是char型