我在我的 aspx 页面中使用了 gridview。因为我在一个单元格中有五个水平对齐的单选按钮。
<asp:GridView ID="CrowdRatingGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="4" OnPageIndexChanging="CrowdRatingGrid_PageIndexChanging" ViewStateMode="Enabled">
<PagerSettings Mode="Numeric" PageButtonCount="4" />
<Columns>
<asp:BoundField DataField="idea_id" HeaderText="Idea ID"></asp:BoundField>
<asp:TemplateField>
<HeaderTemplate>
Rating<br />1 2 3 4 5
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton runat="server" GroupName="rating" ID="1" />
<asp:RadioButton runat="server" GroupName="rating" ID="2" />
<asp:RadioButton runat="server" GroupName="rating" ID="3" />
<asp:RadioButton runat="server" GroupName="rating" ID="4" />
<asp:RadioButton runat="server" GroupName="rating" ID="5" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的数据库表结构:
idea_id rating_id
23882 3
23883 5
63720 1
这是我绑定(bind) gridview 数据的隐藏代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCrowdRatingGrid();
}
}
private void BindCrowdRatingGrid()
{
Campaigns campaign = new Campaigns();
ObjectResult<GetCrowdRating_Result> resultList = campaign.GetCrowdRatingIdeas();
CrowdRatingGrid.DataSource = resultList;
CrowdRatingGrid.DataBind();
}
我在标题“Idea ID”下的网格中正确显示了“idea_id”值 我还需要根据 rating_id 的值检查单选按钮。如果 rating_id 为 3,我需要检查 3 以下的单选按钮,如果 1,则检查 1 以下的单选按钮。
如何实现?
最佳答案
尝试将 RadioButtonList 放入 ItemTemplate 中:
<asp:RadioButtonList runat="server' ID="Rating"
SelectedValue='<%# Bind("rating_id") %>' RepeatDirection="Horizontal" >
<asp:ListItem Value="1" />
<asp:ListItem Value="2" />
<asp:ListItem Value="3" />
<asp:ListItem Value="4" />
<asp:ListItem Value="5" />
</asp:RadioButtonList>
要从每个单选按钮中删除文本,请为每个 ListItem 设置空的 Text
属性。要隐藏具有 0 值的 ListItem
,您可以在 RadioButtonList 上设置 RepeatLayout="Flow"
并像这样设置一些 CssClass 属性值:CssClass="rating"
。然后将此样式规则添加到页面上:
.rating > input:first-child
{
display: none;
}
还有一个可用的选项,您可以使用 RadioButtonList 的 SelectedIndex
属性而不是 SelectedValue,并在绑定(bind)到 1 之前递减 resultList 中的每个 rating_id
。这样您就不会不需要具有 0 值的代理 ListItem。
关于c# - 如何根据数据库的列值检查gridview中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15789925/