ASP.NET中為DataGrid添加單選框

來源:互聯網
上載者:User
asp.net|datagrid|單選框   使用DataGrid控制項添加單選框時,如果直接在模板中加入RadioButton伺服器控制項,由於.Net的機制,無法將這些RadioButton在用戶端出現在同一個組裡面。這時我們可以使用Radio標籤來實現。

  下面是HMTL 頁面代碼

<form id="Form1" method="post" runat="server">
 <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
 <Columns>
  <asp:TemplateColumn>
   <ItemTemplate>
    <input type="radio" name="rdo" <%# GetChecked(DataBinder.Eval(Container, "DataItem")) %> value='<%# DataBinder.Eval(Container, "DataItem") %>'>
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem") %>' ID="Label1">
    </asp:Label>
   </ItemTemplate>
  </asp:TemplateColumn>
 </Columns>
 </asp:datagrid><br>
<asp:Button id="btnOk" runat="server" Text="確定"></asp:Button>
  下面是Page_Load中的資料繫結代碼

Dim arr As New ArrayList
arr.Add("新聞綜合")
arr.Add("綜藝")
arr.Add("電影")
arr.Add("教育")
arr.Add("戲劇")
arr.Add("軍事")
arr.Add("體育")
DataGrid1.DataSource = arr
DataGrid1.DataBind()
  下面是btnOk_Click中擷取選擇項的代碼

Response.Write(Request.Form("rdo"))
  下面是設定繫結項目中哪個Radio被中的函數

Public Function GetChecked(ByVal str As String) As String
 If str = Request.Form("rdo") Then
  Return "checked"
 Else
  Return ""
 End If
End Function

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.