c# - 如何根据数据库的列值检查gridview中的单选按钮

标签 c# asp.net data-binding gridview radio-button

我在我的 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/

相关文章:

c# - 从 web 服务中检索客户端用户名和计算机名

c# - 如何在 C# 中以编程方式启用 MSMQ?

c# - 带有 ssl 的 asp.net 表单 - 拒绝访问日志文件

javascript - Vuejs 子组件中的 Prop 值无法绑定(bind)到元素属性

c# - 根据数据库值预选列表框中的多个项目

c# - 在 UWP 中设置有效日期列表 CalendarDatePicker

c# - CurrentThread.CurrentUICulture 设置正确但总是 en-US

c# - 我如何优化此 actionresult 以获得更好的性能?我需要在何时从 url 等获取 "GET"XML 数据时设置一个计时器

data-binding - Grails 数据绑定(bind)

data-binding - Grails 与命令对象的数据绑定(bind)