学习困难问题,困难问题!

困难问题! - 故障解答 - 电脑教程网

困难问题!

日期:2007-02-19   荐:
困难问题!我怎么能随便点击一个单元格datagrid,就可以编辑它???!用css控制textbox,使得textbox边框不可见,然后在textbox的onfocus事件中将textbox的边框显示,看起来就是都可编辑的了[webform1.aspx.cs 以下内容为程序代码:using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace Sample { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { this.Bind();// 在此处放置用户代码以初始化页面 } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.DataGrid1.ItemDataBound =new DataGridItemEventHandler(DataGrid1_ItemDataBound); this.Load = new System.EventHandler(this.Page_Load); this.DataGrid1.EditCommand =new DataGridCommandEventHandler(DataGrid1_EditCommand); this.DataGrid1.CancelCommand =new DataGridCommandEventHandler(DataGrid1_CancelCommand); } #endregion private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ) { e.Item.Attributes.Add("onclick",Page.GetPostBackClientEvent(e.Item.Cells[1].Controls[0],"")); } } private void Bind() { SqlConnection Cn = new SqlConnection("server=(local);DataBase=pubs;uid=sa;pwd="); SqlDataAdapter SqlDa = new SqlDataAdapter("SELECT * FROM jobs",Cn); DataSet ds = new DataSet(); SqlDa.Fill(ds); this.DataGrid1.DataSource = ds; this.DataGrid1.DataBind(); } private void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e) { this.DataGrid1.EditItemIndex = e.Item.ItemIndex; this.Bind(); } private void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e) { this.DataGrid1.EditItemIndex = -1; this.Bind(); } } } webform1.aspx 以下内容为程序代码:<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Sample.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body> <form id="Form1" method="post" runat="server"> <div id="tt"> <asp:DataGrid id="DataGrid1" runat="server"> <Columns> <asp:ButtonColumn Text="选择" CommandName="Select"></asp:ButtonColumn> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn> </Columns> </asp:DataGrid> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button> </div> </form> </body> </HTML>
标签: