添加GridViewDataGrid单击一行服务器事件_第1页
添加GridViewDataGrid单击一行服务器事件_第2页
添加GridViewDataGrid单击一行服务器事件_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、添加GridView/DataGrid单击一行服务器事件实现功能:的GridView/DataGrid控件本身均支持行选择事件(通过设置Button/LinkButton.CommandName="Selected",并在 SelectedIndexChanged 事件中处理)。 然而,有时候我们希望用户点击网页上GridView/DataGrid 一行中任意位置都可以实现触发一个事件,并在服务端对此行进行相应处理,现在我们就实现此功能。 实现方式:这里我们采取的方法有点 "hack" :  通过客户端 javas

2、cript 引发行中隐藏的按钮(Button/LinkButton 均可以)的 click 事件。 主要代码:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">       

3、0;     <Columns>                                               <asp:TemplateField HeaderText="ProductName" >

4、                     <ItemTemplate>                         <%# Eval("ProductName") %>       

5、                  <asp:Button ID="btnHiddenPostButton" CommandName="HiddenPostButtonCommand" runat="server" Text="HiddenPostButton" style="display:none" />      

6、60;              </ItemTemplate>                 </asp:TemplateField>                 <asp:BoundField DataField="UnitPri

7、ce" HeaderText="UnitPrice" />             </Columns>         </asp:GridView>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)         

8、;     Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;         if (btnHiddenPostButton != null)              e.Row.Attributes"onclick" = String.Form

9、at("javascript:document.getElementById(0).click()", btnHiddenPostButton.ClientID);             / 额外样式定义             e.Row.Attributes"onmouseover" = "javascript:this.style.background=red

10、"             e.Row.Attributes"onmouseout" = "javascript:this.style.background="             e.Row.Attributes"style" = "cursor:pointer"      

11、0;      e.Row.Attributes"title" = "单击选择当前行"                  / 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引         /e.Row.Cells<cellIndex>.At

12、tributes"style" = "display:none"          protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)              int rowIndex = -1;         Grid

13、ViewRow row = null;         switch (e.CommandName)                          case "HiddenPostButtonCommand": / 模板列             

14、;                    Control cmdControl = e.CommandSource as Control; / 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control                 row = cmdControl.NamingContainer a

15、s GridViewRow; / 当前行                 / 如何访问单元格值                 / string txt = row.Cells0.Text;                 / 如何获取模板列中的 Label 

16、                / string lbl = row.FindControl("MyLabelID") as Label;                 / 执行更多的自定义操作                 / .   

17、;             /  .                Response.Write(String.Format("GridView Version 当前第 0 行:", row.RowIndex + 1);                 break;    

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论