在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
实现功能:单击表格中某个单元格(不是第一列、最后一列、最后一行,不为0)根据行第一个单元格内容及列名来查询详细内容,在消息框中查看显示。 在代码中增加 protected override void Render(HtmlTextWriter writer) { foreach (GridViewRow r in GridViewTzx.Rows) { if (r.RowType == DataControlRowType.DataRow) { for (int columnIndex = 1; columnIndex < r.Cells.Count; columnIndex++)//(int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++) { Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString()); } } } base.Render(writer); } protected void GridViewTzx_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // 从第一个单元格内获得LinkButton控件 LinkButton _singleClickButton = (LinkButton)e.Row.Cells[7].Controls[0]; // 返回一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用 string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, ""); // 给每一个可编辑的单元格增加事件 for (int columnIndex = 1; columnIndex < e.Row.Cells.Count-2; columnIndex++)//int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++) { // 增加列索引作为事件参数 string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString()); // 给单元格增加onclick事件 e.Row.Cells[columnIndex].Attributes["onclick"] = js; // 给单元格增加鼠标经过时指针样式 e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;"; } } } protected void GridViewTzx_RowCommand(object sender, GridViewCommandEventArgs e) { string sXianghaos = ""; int _rowIndex = int.Parse(e.CommandArgument.ToString()); int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]); if (GridViewTzx.Rows[_rowIndex].Cells[0].Text != "汇总" && GridViewTzx.Rows[_rowIndex].Cells[_columnIndex].Text != "0" && _columnIndex < 6 && _columnIndex > 0) { string sGk = GridViewTzx.Columns[_columnIndex].HeaderText; string sXx = GridViewTzx.Rows[_rowIndex].Cells[0].Text; string sSubCmd = "id in (select max(id) as id from jzx_active group by xianghao) and XIANGHAO in (select boxno from boxnumber where boxtypeid = (select id from boxtype where type ='"+sXx+"') )"; string sCmd = ""; sCmd = "select xianghao from jzx_active where (QYG like'" + sGk + "%' or MDG like'" + sGk + "%') and " + sSubCmd ; if (sGk=="租出") { sCmd = "SELECT xianghao from jzx_active where ZHUANGTAI='" + sGk + "' and " + sSubCmd; } MySqlDataReader reader = null; mySqlMod newMySqlMod = new mySqlMod(); newMySqlMod.RunSQL(sCmd, out reader); if (reader.HasRows) { while (reader.Read()) { sXianghaos += reader[0].ToString(); sXianghaos += ","; } sXianghaos = sXianghaos.TrimEnd(','); CommData.MessageBoxAsyncPostBack(this, GetType(), sXianghaos); } reader.Close(); } }
参考资料:原文http://www.codeproject.com/Articles/18136/Edit-Individual-GridView-Cells-in-ASP-NET翻译:webabcd GridView有一个不可见的asp:ButtonField控件,它处于GridView的第一列,名为“SingleClick”。 它用于给GridView的数据行增加单击事件。 <Columns> <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" /> </Columns> 在RowDataBound事件内循环为每一数据行的每一单元格增加单击事件。 使用单元格在数据行中的索引作为事件参数,这样在单元格触发了单击事件后我们就可以知道到底是哪个单元格被单击了。 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // 从第一个单元格内获得LinkButton控件 LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0]; // 返回一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用 string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, ""); // 给每一个可编辑的单元格增加事件 for (int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++) { // 增加列索引作为事件参数 string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString()); // 给单元格增加onclick事件 e.Row.Cells[columnIndex].Attributes["onclick"] = js; // 给单元格增加鼠标经过时指针样式 e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;"; } } } 在RowCommand事件内读出命令参数和事件参数。 这会告诉我们被选中的行和列的索引。 int _rowIndex = int.Parse(e.CommandArgument.ToString()); int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
protected override void Render(HtmlTextWriter writer) { foreach (GridViewRow r in GridView1.Rows) { if (r.RowType == DataControlRowType.DataRow) { for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++) { Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString()); } } } base.Render(writer); } 这将防止任何“回发或回调参数无效”的错误。 |
请发表评论