我在网格视图上添加了一个复选框。我如何做到这一点,所以任何时候都只能选择1个复选框。
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" Font-Size="Small" Width="100%">
<Columns>
<asp:BoundField DataField="Supplier" HeaderText="Supplier" SortExpression="Supplier" />
<asp:BoundField DataField="Term" HeaderText="Term" SortExpression="Term" />
<asp:BoundField DataField="Tariff" HeaderText="Tariff" SortExpression="Tariff" />
<asp:BoundField DataField="SC" HeaderText="SC" SortExpression="SC" />
<asp:BoundField DataField="Charge" HeaderText="Charge" SortExpression="Charge" />
<asp:BoundField DataField="Unit_Rate" HeaderText="Unit_Rate" SortExpression="Unit_Rate" />
<asp:BoundField DataField="Day_Rate" HeaderText="Day_Rate" SortExpression="Day_Rate" />
<asp:BoundField DataField="Night_Rate" HeaderText="Night_Rate" SortExpression="Night_Rate" />
<asp:BoundField DataField="Weekday_Rate" HeaderText="Weekday_Rate" SortExpression="Weekday_Rate" />
<asp:BoundField DataField="Eve_Wkend_Rate" HeaderText="Eve_Wkend_Rate" SortExpression="Eve_Wkend_Rate" />
<asp:BoundField DataField="Eve_Wkend_Night_Rate" HeaderText="Eve_Wkend_Night_Rate" SortExpression="Eve_Wkend_Night_Rate" />
<asp:BoundField DataField="Winter_Rate" HeaderText="Winter_Rate" SortExpression="Winter_Rate" />
<asp:BoundField DataField="Other_Rates" HeaderText="Other_Rates" SortExpression="Other_Rates" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
答案 0 :(得分:0)
按如下所示在用户界面中更改复选框定义
<asp:checkbox id="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged"/>
在您的复选框中选中更改事件,尝试
CheckBox chk = sender as CheckBox;
GridViewRow gvr = (GridViewRow)chk.NamingContainer;
if(chk.Checked)
{
foreach (GridViewRow row in grid.Rows)
{
if (gvr != row && ((CheckBox)row.FindControl("CheckBox1")).Checked)
{
chk.Checked = false.
}
}
}
注意:-建议使用@Tim如果一次只选择一项,请尝试使用RadioButtom
答案 1 :(得分:0)
添加一个OnRowDataBound
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" Font-Size="Small" Width="100%" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Supplier" HeaderText="Supplier" SortExpression="Supplier" />
<asp:BoundField DataField="Term" HeaderText="Term" SortExpression="Term" />
<asp:BoundField DataField="Tariff" HeaderText="Tariff" SortExpression="Tariff" />
<asp:BoundField DataField="SC" HeaderText="SC" SortExpression="SC" />
<asp:BoundField DataField="Charge" HeaderText="Charge" SortExpression="Charge" />
<asp:BoundField DataField="Unit_Rate" HeaderText="Unit_Rate" SortExpression="Unit_Rate" />
<asp:BoundField DataField="Day_Rate" HeaderText="Day_Rate" SortExpression="Day_Rate" />
<asp:BoundField DataField="Night_Rate" HeaderText="Night_Rate" SortExpression="Night_Rate" />
<asp:BoundField DataField="Weekday_Rate" HeaderText="Weekday_Rate" SortExpression="Weekday_Rate" />
<asp:BoundField DataField="Eve_Wkend_Rate" HeaderText="Eve_Wkend_Rate" SortExpression="Eve_Wkend_Rate" />
<asp:BoundField DataField="Eve_Wkend_Night_Rate" HeaderText="Eve_Wkend_Night_Rate" SortExpression="Eve_Wkend_Night_Rate" />
<asp:BoundField DataField="Winter_Rate" HeaderText="Winter_Rate" SortExpression="Winter_Rate" />
<asp:BoundField DataField="Other_Rates" HeaderText="Other_Rates" SortExpression="Other_Rates" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
在您的代码中
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strScript = "uncheckOthers(" + ((CheckBox)e.Row.Cells[0].FindControl("CheckBox1")).ClientID + ");";
((CheckBox)e.Row.Cells[0].FindControl("CheckBox1")).Attributes.Add("onclick", strScript);
}
}
catch (Exception Ex)
{
//report error
}
}
添加这样的脚本
<script type="text/javascript" language="javascript">
function uncheckOthers(id)
{
var elm = document.getElementsByTagName('input');
for(var i = 0; i < elm.length; i++)
{
if(elm.item(i).type == "checkbox" && elm.item(i)!=id)
elm.item(i).checked = false;
}
}
</script>
完成