利用radio实现Datagrid的单选[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 guoyan19811021 的 blog

          在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

本文关键:Datagrid 单选
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top