為DataGrid列添加Radio(單選框)列

來源:互聯網
上載者:User
datagrid|單選框 經常看到論壇上有人問在DataGrid中如何?單選,下面就是一種實現的方法。

查看例子

<%@ Page Language="C#" AutoEventWireup="True" EnableViewState = "true"%>
<%@ Import Namespace="System.Data" %>
<html>
<script runat="server">
DataTable Cart;
DataView CartView;
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 101; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i+1);
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

void Page_Load(Object sender, EventArgs e)
{
string js = "";
js+="<script>\r\n";
js+="function ld(){\r\n";
js+="for(i=0;i<document.getElementsByName('RadioName').length;i++)\r\n";
js+="if(document.getElementsByName('RadioName')[i].value==";
js+="document.getElementById('" + rd.ClientID + "').value) ";
js+="document.getElementsByName('RadioName')[i].checked=true\r\n";
js+="}\r\n";
js+="window.onload=ld\r\n";
js+="</"+"script>\r\n";
this.RegisterStartupScript("js",js);
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}
void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
{
ItemsGrid.CurrentPageIndex = e.NewPageIndex;
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
void btnClick(Object sender, EventArgs e)
{
if(Request.Form["RadioName"] != null)
{
rd.Value = Request.Form["RadioName"].ToString();
Label1.Text = "您所選擇的是:<font color=red>" + Request.Form["RadioName"].ToString() +"</font>";
}
}

</script>
<body>
<form runat="server" id=MM>
<input type="hidden" id=rd runat=server/>
<asp:DataGrid id="ItemsGrid" runat="server"
BorderColor="black" BorderWidth="1"
CellPadding="3" AllowPaging="true"
AutoGenerateColumns="false"
OnPageIndexChanged="Grid_Change">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<PagerStyle Mode="NumericPages"></PagerStyle>

<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<input type=radio name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "IntegerValue")%>'/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="數字列" DataField="IntegerValue"/>
<asp:BoundColumn HeaderText="字串列" DataField="StringValue"/>
<asp:BoundColumn HeaderText="貨幣列" DataField="CurrencyValue" DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="right"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<br>
<asp:Button id="Btn" Text="看你選擇的" OnClick="btnClick" runat="server"/>
<asp:Label id="Label1" Text="" runat="server"/>
</form>
</body>
</html


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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