Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > DataGrid中使用Button操作
【标  题】:DataGrid中使用Button操作
【关键字】:DataGrid,Button
【来  源】:http://blog.csdn.net/youbl/archive/2006/07/25/976984.aspx

DataGrid中使用Button操作

Your Ad Here

首先在Datagrid里设置OnItemCommand事件,指向处理函数,然后在模板列加button,设置button的CommandName属性,在后台根据CommandName来判断点击了那个button,并进行处理。
  

  <asp:DataGrid id="DataGrid1" runat="server" OnItemCommand="itemComm">
    <Columns>
     <asp:TemplateColumn HeaderText="比较1">
      <ItemTemplate>
       <asp:Label id=Button2 runat="server">
        <%# DataBinder.Eval(Container.DataItem,"id")+@"/"+ DataBinder.Eval(Container.DataItem,"project")%>
       </asp:Label>
      </ItemTemplate>
     </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="比较2">
     <ItemTemplate>
      <asp:Button ID=btn1 Runat=server CommandName="test1" Text=<%# DataBinder.Eval(Container.DataItem,"id")%>>
      </asp:Button>
     </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="比较3">
     <ItemTemplate>
      <asp:Button ID="Button3" Runat=server CommandName="test2" Text=<%# DataBinder.Eval(Container.DataItem,"id")%>>
      </asp:Button>
     </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
   </asp:DataGrid>

后台的函数:

public void itemComm(object sender,DataGridCommandEventArgs e)
  {
   switch(Convert.ToString(((Button)e.CommandSource).CommandName))
   {
    case "test1":
     this.button1.Text=e.Item.ItemIndex.ToString();
     break;
    case "test2":
     this.button1.Text=e.Item.ID.ToString();
     break;
   }
  }

C#高级编程之“反射”:【上一篇】
ASP.NET中DataGrid和DataList控件用法比较:【下一篇】
【相关文章】
  • 如何在GridView中使用RadioButtons单选列!
  • GridView/DataGrid单元格不换行的问题
  • 为DataGrid的列添加链接/添加Button列/调整列顺序_AX
  • DataGrid分页一个大BUG有兴趣有可以进来看看。
  • 將DataGrid里的圖片導入到excel的例子
  • Post button for popup post to del.icio.us
  • datagrid的简单技巧
  • asp.net 中DataGrid自定义分页(简单,实用,易懂)
  • The Freeze Pane DataGrid
  • .NET获取DataGrid的Cell方法
  • 【随机文章】
  • Delphi客户服务器应用开发(五)
  • 个上面加上不同的文字注意去掉文字的光标可?属性
  • 真刀真枪实战CorelDRAW10(6)
  • 添加背景音乐的代码
  • 画一棵树
  • 数字塔问题(递归与非递归)
  • 通过XSLT将XML文件转换为HTML文件
  • Unilan屏蔽系统解决方案
  • WPF/E 2007年2月CTP发布了
  • 虚拟化存储管理
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.