首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
goodboy1881 (13)
wangchinaking (58)
iiprogram (67)
fancyhf (1)
harrymeng (41)
yjz0065 (113)
coofucoo (105)
Drate (69)
lphpc (30)
smallnest (61)
CSDN - 文档中心 - .NET 阅读:8852   评论: 9    参与评论
标题   利用radio实现Datagrid的单选     选择自 guoyan19811021 的 Blog
关键字   Datagrid 单选
出处  

          在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测试通过。


相关文章
对该文的评论
CSDN 网友 ( 2005-06-10)
呵呵..感觉还不如用Javascript在页面中判断一下来得简便..
:)
CSDN 网友 ( 2005-05-02)
不好意思,应该把SubString写到下一行,即:
aa = aa.SubString(...)
CSDN 网友 ( 2005-05-02)
可以用类似这样的语句替换不知如何,测试是可以通过的.
 string aa = r.Text.Trim();
                aa = aa.Remove(0,aa.IndexOf("value='")+7).Substring(0,aa.IndexOf("'"));
                if (aa == lblView.Text.Trim())
                {...  }
                else
                {...  }
CSDN 网友 ( 2005-05-02)
下面button的click事件后面的几行实际上是为了保持选中行后的状态的,当用IndexOf时是会出现刚才的问题的,但是不会影响第一行的选值,但是要解决选中状态的保持还是有点麻烦的哦
CSDN 网友 ( 2005-05-02)
有一点问题哦,假设我在cells[1]列上有"aa","aabb"两个数据时,选择aa所在行时,会有点小问题的,原因时:选aabb所在行时,indexof也是大于0的