我想实现单击GridView内的记录时,使被点击的单元格获得焦点,怎么实现?
也就是:如何获取GridView被单的单元格的行索引和列索引?请各位指教,谢谢!
也就是:如何获取GridView被单的单元格的行索引和列索引?请各位指教,谢谢!
解决方案 »
- asp的MSXML2.ServerXMLHTTP 操作.net session 为什么会无效
- 向正则表达式高手请教。
- 求一个正则表达式
- Update更新数据问题
- 有关asp.net datagrid更新按钮的问题
- 防用户重复登录的方法
- 那位人士可以给我介绍份工作,本人现在东莞
- 思归大哥,帮我看看呀!!!郁闷了4天得问题!!!!
- 请教:如何在asp.net实现对不同的用户设置管理权限????????(最好举例说明)
- ====咨询在涉及大型项目开发过程中,对于一些页面常规操作的设计思路====
- 对DataRowCollection.Find(object []keys)我有疑问??
- 为什么会出现这个问题?郁闷...
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Container.DataItemIndex%> "></asp:Label>
</ItemTemplate>
</asp:TemplateField>
在一个模版里绑定列
protected void imgBtnEdit_Command(object sender, CommandEventArgs e)//我用的是imagebutton,你可以用别的,但事件事command事件
{
int index = Convert.ToInt32(e.CommandArgument);//获取行号
GridViewRow row = GridView1.Rows[index];//获取该行
stirng s=row.Cells[2].Text;//该行第二列数据
}
给每个单元格,添加客户端onclick事件,在该事件处理函数中把该单元格所在行号和列号付给两个方本框,然后再触发那个按钮的回发事件就行了。
<!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.csusing 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());
}
}
}