在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<!-- Countries for selection -->
<asp:DataGrid id="countriesGrid" runat="server"
DataKeyField="ID" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<!--
Draw attention at this control.
We would like to use radio-buttons to
select single country from the list.
-->
<asp:RadioButton id="selectRadioButton"
runat="server" GroupName="country" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Country" HeaderText="Country"
HeaderStyle-Font-Bold="True" />
<asp:BoundColumn DataField="Capital" HeaderText="Capital"
HeaderStyle-Font-Bold="True" />
</Columns>
</asp:DataGrid> Now, bind to the Where is a mistake? We have specified <!-- Countries for selection -->
<table cellspacing="0" rules="all" border="1" id="countriesGrid"
style="border-collapse:collapse;">
<tr>
<td> </td>
<td style="font-weight:bold;">Country</td>
<td style="font-weight:bold;">Capital</td>
</tr>
<tr>
<td><input id="countriesGrid__ctl2_selectRadioButton"
type="radio" name="countriesGrid:_ctl2:country"
value="selectRadioButton" /></td>
<td>USA</td>
<td>Washington</td>
</tr>
<tr>
<td><input id="countriesGrid__ctl3_selectRadioButton"
type="radio" name="countriesGrid:_ctl3:country"
value="selectRadioButton" /></td>
<td>Canada</td>
<td>Ottawa</td>
</tr>
<!-- etc. --> The ' When rendering Now you have understood the cause of error, but how to implement the feature we want? In the next section, I'll provide you the solution. Solution of the problemTo solve the problem I have stated above, I've created a new In this control, I have changed the rendering method so that ' Another one modification is postback data handling ( Other functionality of the See the source code of the Using the codeNow, let's modify the initial form. Use the following script for the Countries list: <%@ Register TagPrefix="vs" Namespace="Vladsm.Web.UI.WebControls"
Assembly="GroupRadioButton" %>
...
<!-- Countries for selection -->
<asp:DataGrid id="countriesGrid" runat="server" DataKeyField="ID"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<vs:GroupRadioButton id="selectRadioButton"
runat="server" GroupName="country" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Country" HeaderText="Country"
HeaderStyle-Font-Bold="True" />
<asp:BoundColumn DataField="Capital" HeaderText="Capital"
HeaderStyle-Font-Bold="True" />
</Columns>
</asp:DataGrid> Add reference to the It remained only to show how to determine which of the countries have been selected:
Collapse
using Vladsm.Web.UI.WebControls;
...
private void selectButton_Click(object sender, System.EventArgs e)
{
// for each grid items...
foreach(DataGridItem dgItem in countriesGrid.Items)
{
// get GroupRadioButton object...
GroupRadioButton selectRadioButton =
dgItem.FindControl("selectRadioButton") as GroupRadioButton;
// if it is checked (current item is selected)...
if(selectRadioButton != null && selectRadioButton.Checked)
{
// sample data that was boud to the countriesGrid
DataTable dataSource = DataSource.CreateSampleDataSource();
// get country corresponding to the current item...
DataRow row =
dataSource.Rows.Find(countriesGrid.DataKeys[dgItem.ItemIndex]);
// ...and show selected country information
selectedCountryInfo.Text =
String.Format("Selected country: {0}, Capital: {1}",
row["Country"], row["Capital"]);
return;
}
}
// there are no selected countries
selectedCountryInfo.Text = String.Empty;
|
请发表评论