首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用radio实现Datagrid的单选

利用radio实现Datagrid的单选

作者头像
阿新
发布2018-04-13 10:46:55
1.6K0
发布2018-04-13 10:46:55
举报
文章被收录于专栏:c#开发者c#开发者

在datagrid中,我们可能会需要实现这种功能——列的单选,本身datagrid提供了select命令可以实现这种功能。另为也可以利用HTML 控件中的radiobutton来实现这样的功能,当然这也是我们所习惯的。       好的,现在来实现它。  首先在页面上加入DataGrid控件。 第一列设置为模板列,在项模板中加入label 再将datagrid绑定上数据 具体格式如下: 〈asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="176px" Height="22px"> 〈SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66">〈/SelectedItemStyle> 〈ItemStyle ForeColor="#330099" BackColor="White">〈/ItemStyle> 〈HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000">〈/HeaderStyle> 〈FooterStyle ForeColor="#330099" BackColor="#FFFFCC">〈/FooterStyle> 〈Columns> 〈asp:TemplateColumn HeaderText="Select"> 〈ItemTemplate> 〈asp:Label id="Label2" runat="server">〈/asp:Label> 〈/ItemTemplate> 〈/asp:TemplateColumn> 〈asp:BoundColumn DataField="a" HeaderText="Last Name">〈/asp:BoundColumn> 〈/Columns> 〈PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC">〈/PagerStyle> 〈/asp:DataGrid> 再在页面上加入一个label(用于显示我们在Datagrid中单选的项)和一个Button(查看选中项),如下: 〈asp:Label id="Label3" style="Z-INDEX: 103; LEFT: 222px; POSITION: absolute; TOP: 35px" runat="server" Width="184px">〈/asp:Label> 〈asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 218px; POSITION: absolute; TOP: 70px" runat="server" Text="Display Selected Value">〈/asp:Button〉     在后台代码中: 在DataGrid的ItemDataBound事件中          If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then             Dim lbl As Label             lbl  = e.Item.FindControl("Label2")             '加入radio             lbl .Text = "<input type=radio name='myradiogroup' value=" & e.Item.Cells(1).Text & ">"         End If 在Button的click事件中         Label3.Text = Request.Form("myradiogroup")         Dim i As DataGridItem         For Each i In DataGrid1.Items             If i.ItemType = ListItemType.AlternatingItem Or i.ItemType = ListItemType.Item Then                 Dim r As Label                 r = i.FindControl("Label2")                 If r.Text.IndexOf(Label3.Text) > 0 Then                     r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & " checked>"                 Else                     r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & ">"                 End If             End If         Next          好了,这样就可以实现利用radio实现Datagrid的单选的功能了。           如图:                          当我们选中其中一项,点击"查看选中内容"时,如图:         在vs.net2003、iis5.0测试通过。

  • 第一列设置为模板列,在项模板中加入label
  • 再将datagrid绑定上数据

具体格式如下: 〈asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="176px" Height="22px"> 〈SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66">〈/SelectedItemStyle> 〈ItemStyle ForeColor="#330099" BackColor="White">〈/ItemStyle> 〈HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000">〈/HeaderStyle> 〈FooterStyle ForeColor="#330099" BackColor="#FFFFCC">〈/FooterStyle> 〈Columns> 〈asp:TemplateColumn HeaderText="Select"> 〈ItemTemplate> 〈asp:Label id="Label2" runat="server">〈/asp:Label> 〈/ItemTemplate> 〈/asp:TemplateColumn> 〈asp:BoundColumn DataField="a" HeaderText="Last Name">〈/asp:BoundColumn> 〈/Columns> 〈PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC">〈/PagerStyle> 〈/asp:DataGrid> 再在页面上加入一个label(用于显示我们在Datagrid中单选的项)和一个Button(查看选中项),如下: 〈asp:Label id="Label3" style="Z-INDEX: 103; LEFT: 222px; POSITION: absolute; TOP: 35px" runat="server" Width="184px">〈/asp:Label> 〈asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 218px; POSITION: absolute; TOP: 70px" runat="server" Text="Display Selected Value">〈/asp:Button〉     在后台代码中:

  • 在DataGrid的ItemDataBound事件中

         If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then             Dim lbl As Label             lbl  = e.Item.FindControl("Label2")             '加入radio             lbl .Text = "<input type=radio name='myradiogroup' value=" & e.Item.Cells(1).Text & ">"         End If

  • 在Button的click事件中

        Label3.Text = Request.Form("myradiogroup")         Dim i As DataGridItem         For Each i In DataGrid1.Items             If i.ItemType = ListItemType.AlternatingItem Or i.ItemType = ListItemType.Item Then                 Dim r As Label                 r = i.FindControl("Label2")                 If r.Text.IndexOf(Label3.Text) > 0 Then                     r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & " checked>"                 Else                     r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & ">"                 End If             End If         Next          好了,这样就可以实现利用radio实现Datagrid的单选的功能了。           如图:          当我们选中其中一项,点击"查看选中内容"时,如图: 在vs.net2003、iis5.0测试通过。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2005-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档