在DataGrid 中如何實現返回被單擊的單元格的行號和列號(兩個同時返回)

我沒用過VS2005,所以GridView也不會,但我想應該和DataGrid差不多吧,下面是我用VS2003的DataGrid寫的例子,側試通過,至於有沒有更好的方法,我就不清楚了。

頁面:clickCell.aspx

<%@ Page language="c#" Codebehind="clickCell.aspx.cs" AutoEventWireup="false" Inherits="WebTest.clickCell" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>clickCell</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">
<FONT face="宋體">
<asp:DataGrid id="DataGrid1" runat="server" Width="288px" Height="112px"></asp:DataGrid></FONT>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</form>
</body>
</HTML>

後臺編碼文件:clickCell.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;
using System.Text;

namespace WebTest
{
/// <summary>
/// clickCell 的摘要說明。
/// </summary>
public class clickCell : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
private   SqlConnection myConnection;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
myConnection=new SqlConnection("server=127.0.0.1;;database=pubs;uid=sa;pwd=sa;");
this.TextBox1.Attributes.Add("style","display:none");
this.TextBox2.Attributes.Add("style","display:none");
this.LinkButton1.Attributes.Add("style","display:none");
BuildJs();
if(!this.Page.IsPostBack)
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors",myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");
DataGrid1.DataSource=ds.Tables["Authors"].DefaultView;
DataGrid1.DataBind();
}
}

#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{   
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound);

}
#endregion


private void LinkButton1_Click(object sender, System.EventArgs e)
{
Response.Write("行號:" + TextBox1.Text + ";列號:" + TextBox2.Text + "。");
}

private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType == ListItemType.Item)
{
for(int i=0;i<e.Item.Cells.Count;i++)
{
e.Item.Cells[i].Attributes.Add("onclick","javascript:clickCell('" + e.Item.ItemIndex + "','" + i + "')");
e.Item.Cells[i].Attributes.Add("style","cursor:hand");
}
}
}

private void BuildJs()
{
StringBuilder js = new StringBuilder();
js.Append("<script language=javascript>" + Environment.NewLine);
js.Append("    function clickCell(rowIndex,cellIndex)" + Environment.NewLine);
js.Append("    {" + Environment.NewLine);
js.Append("        document.getElementById('"+ TextBox1.ClientID +"').value=rowIndex;" + Environment.NewLine);
js.Append("        document.getElementById('"+ TextBox2.ClientID +"').value=cellIndex;" + Environment.NewLine);
js.Append("        __doPostBack('" + LinkButton1.UniqueID + "','');" + Environment.NewLine);
//如果你用的是用戶控件,不知道LinkButton1.UniqueID這裏要不要進行處理一下,你自己測一下,替換一下應該就行了,以前寫過,記不清了,我這個沒有寫在控件裏。
js.Append("    }" + Environment.NewLine);
js.Append("</script>" + Environment.NewLine);

this.Page.RegisterClientScriptBlock("clickCell",js.ToString());

}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章