现在我要做的是 只要点击 gridview 中的某一项,就可以按这个 项 进行查找
并把结果显示在原来的 gridview 中
原来准备用gridview 中的 bottonfield 这个的,可是用了以后发现 获取不了
它的值(就是在gridview 中显示的值)
并把结果显示在原来的 gridview 中
原来准备用gridview 中的 bottonfield 这个的,可是用了以后发现 获取不了
它的值(就是在gridview 中显示的值)
解决方案 »
- 最简单的更新为什么不成功?
- asp.net 操作access,插入一条数据,成功了但立即查不到
- 关于lable换行style=word-break:break-all无效
- IIS右击浏览正常 但本地浏览器打开找不到网页
- 网页上FLASH无法显示
- DataGrid的分页问题
- 输入用户名和密码后出现这个情况
- PagedDataSource的数据源必须是 DataView吗??
- auto translate
- 请教高人给我分析一个小思路,我的脑子实在是转不过了圈了,谢谢大家了
- 哪错了[color=#FF0000]int i=(int)Application.Get("logincount");[/color] 未将对象引用设置到对象的实例。
- 上传图片到网络路径,出现 登录失败: 未知的用户名或错误密码。
要不你就button写个事件传一个值。
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Response.Write(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
}
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Query" />
</Columns>
</asp:GridView>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Query")
{
Response.Write(e.CommandArgument.ToString());
}
}
{
if (e.CommandName == "Query")
{
Response.Write(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
}
}
会在页面输出当前选中行的key 剩下的工作 你根据你的需求自己改下吧~
有疑问请联系~
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
实例之占击获得某一单元格的值
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound">
</asp:GridView>
</div>
</form>
<script type="text/javascript">
var isIe=window.navigator.appName.indexOf("Netscape") == -1?true:false;
function onCellClick(srcCell){
var sCellText=isIe?srcCell.innerText:srcCell.textContent;
alert(sCellText);
}
function initCellClick(){
var cells=document.getElementById("<%=GridView1.ClientID %>").getElementsByTagName("td");
for(var i=0;i<cells.length;++i){
cells[i].onclick="onCellClick(this)";
}
}
//initCellClick();
</script>
</body>
</html>后台using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class codeexample_demo_gridviewexp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GenerateTable();
GridView1.DataBind();
} private DataTable GenerateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
DataRow row;
Random rnd = new Random();
for (int i = 1; i != 31; ++i)
{
row = dt.NewRow();
row[0] = i;
row[1] = "Product_" + i;
dt.Rows.Add(row);
} return dt;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRowCollection rows = GridView1.Rows;
TableCellCollection cells;
for (int i = 0; i != rows.Count; ++i)
{
cells = rows[i].Cells;
for (int j = 0; j != cells.Count; ++j)
{
cells[j].Attributes.Add("onclick", "onCellClick(this)");
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body >
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField ButtonType="Button" Text="查询" CommandName="Query" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Default5 : System.Web.UI.Page
{
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal))); for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "学生" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Random newRd = new System.Random(Environment.TickCount * 0); ;
dr = dt.NewRow();
dr[0] = "班级0" ;
dr[1] = "学生0" ;
dr[2] = System.Math.Round(newRd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(newRd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(newRd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(newRd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
} protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = CreateDataSource();
this.GridView1.DataKeyNames = new string[] { "学生姓名" };
this.GridView1.DataBind();
} protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Query")
{
DataView dv=(DataView)GridView1.DataSource;
dv.Sort = "学生姓名"; GridView1.DataSource = dv.FindRows(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() );
GridView1.DataBind();
}
}}