要求用DATAGRID显示后台数据库里的一张表,DATAGRID里显示的每条记录都能手动输入文本类型的回复,并且能把此文本写入数据库,对此记录回复后此记录自动隐藏. 好象不难 可小弟初学ASP.NET 不知道该怎么做 -_-#
有没有真正的高人能够实际的指点一下,愿意送100分,给个例子也好,小弟还是学生,请各位大大帮忙
人人为我,我为人人
有没有真正的高人能够实际的指点一下,愿意送100分,给个例子也好,小弟还是学生,请各位大大帮忙
人人为我,我为人人
解决方案 »
- 使用.net访问MYSQL数据库无法正常使用Parameters怎么解决?
- 从FTP上自动检测数据,有新数据自动导入数据库
- list集合中的实体我怎么能给放到一个datatable里面去啊
- vs2005(C#) 使用extjs
- 用javasript控制gridview文本框的值,急,在线等.
- 帮忙解释一下这段代码---参数是SM意思!!!
- 文件删除出错,求救高手!(100分)
- 为什么server=localhost就不能用了??系统还原以后.
- 我在用net编网页时,出现了以下错误,请高人指点一下,
- 菜鸟问题: 在一个web控件中如何得知 当前页面的文件名和路径?
- .net程序打包的时候出现不可恢复的错误是怎么回事?
- 新人求助啊 有分有分啊!!!在线等哈~~~~~~~~~~~~~~
<%@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>
</head>
<body bgcolor="#ffffff">
<span class="heading">Editing Data in a DataGrid Control</span><hr /><div id="outError" runat="server" /><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)
{
// 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 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 '%07645437%'"; // 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><!---------------------------------------------------------------------------></body>
</html>
我照您的意思去试试看,如果能够解决问题一定给分.
为什么我重装系统以后新建WEB项目的时候提示:指定的WEB服务器运行的不是ASP.NET1.1版本
可是我重装前用的也是这个版本啊,是不是IIS的版本不对?IIS需要什么版本才行?
上面的例子足够你用了
// 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 '%07645437%'"; // 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><!---------------------------------------------------------------------------></body>
} // set the DataSource property and bind the grid
MyDataGrid.DataSource = objDataReader;
MyDataGrid.DataBind();
}</script>
用到ado。net
先要连接数据库
然后用sql增加修改删除数据库
b/s
还是
c/s
</ItemTemplate>
<EditItemTemplate>
<ASP:TextBox id="txtTitle" Size="60"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
这就是核心啦