【问题标题】:How to make a radiobutton in gridview checked based on a column value of database如何根据数据库的列值检查gridview中的单选按钮
【发布时间】:2013-03-25 06:30:25
【问题描述】:

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

这是我用于绑定 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 下的单选按钮..

如何做到这一点?

【问题讨论】:

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


    【解决方案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 并在绑定到 1 之前递减 resultList 中的每个 rating_id。这样您就不需要具有 0 值的代理 ListItem。

    【讨论】:

    • 我已经按照您提到的方式进行了尝试,但出现以下异常:[ArgumentOutOfRangeException: 'Rating' 的 SelectedValue 无效,因为它不存在于项目列表中。参数名称:值]
    • 对数据库执行此脚本并检查您拥有的评级值:select distinct rating_id from TABLENAME
    • 完成.. 评分值为 3 1 2 4 5
    • resultList 中的 rating_id 是否为 null 或 0?
    • 是的,我有 0 作为结果列表中某些条目的值
    【解决方案2】:

    尝试使用 RadioButtonList。

    <asp:RadioButtonList id="myList" runat="Server">
       <asp:ListItem id="1" value="1">1</asp:ListItem>
       <asp:ListItem id="2" value="2">2</asp:ListItem>
       <asp:ListItem id="3" value="3">3</asp:ListItem>
       <asp:ListItem id="4" value="4">4</asp:ListItem>
       <asp:ListItem id="5" value="5">5</asp:ListItem>
    </asp:RadioButtonList>
    

    然后在后面的代码中使用类似的东西:

    myList.Items.FindByValue(yourDBValue).Selected = true;
    

    编辑:

    或者,将 selectedValue 绑定在控件本身;正如 Yuriy 建议的那样。

    【讨论】:

      【解决方案3】:

      GridView 控件有RowCreated 事件。在那里,您可以根据数据库中的值检查单选按钮。您可能必须使用 FindControl() 才能获得正确的单选按钮。

      这个页面有一个很好的例子来说明如何创建OnRowCreated 事件处理程序:

      http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated.aspx

      【讨论】:

        【解决方案4】:

        也许您可以尝试通过字符串名称执行 .FindControl,也就是说,如果您在 gridView 中寻找您的单选按钮。只需将网格视图绑定到您的数据源,然后在 rowDataBound 上搜索每一行的单选按钮。假设您有 5 个带有名称的单选按钮: rb1, rb2, rb3 等等.. 因此,当您获得评级 id 时,只需像这样将其添加到字符串名称中

        protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
           GridViewRow row = (GridViewRow)e.item;
           ..YourObject.. data = (..YourObject..)e.item.DataItem;
           //and now you search for your radio button
           RadioButton button = (RadioButton)row.FindControl("rb" + data.rating_id); 
           //or just use the id as a name like you have allready named them
           button.cheched = true;
        }
        

        【讨论】:

        • 在 RadioButton button = (RadioButton)row.FindControl("rb" + data.rating_id);
        猜你喜欢
        • 2019-09-16
        • 2013-03-19
        • 2016-05-08
        • 1970-01-01
        • 2022-10-17
        • 2014-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多