【问题标题】:Bind smallint from database to ajax stars rating in a gridview将数据库中的 smallint 绑定到网格视图中的 ajax 星级评分
【发布时间】:2014-10-12 05:29:55
【问题描述】:

我有一个简单的表格,其中存储评论以及评分如下:

Create table tblReview
 (
 Id int primary key identity,
 Rating smallint,
 Review nvarchar(max),
 RegistrationId int foreign key references tblAllRegisteredBusiness(RegistrationId),
 ReviewDate DateTime not null Default GetDate()
 )

我有一个带有 AJAX 星级评分控件的 gridview。我要做的就是使用基于 RegistrationId 的所有评论填充 gridview。gridview 如下所示:

<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="False" ShowHeader="False" Width="618px" OnRowDataBound="GridView1_RowDataBound">
                        <Columns>
                        <asp:TemplateField>
                        <ItemTemplate>

                            <table cellpadding="0" cellspacing="0" class="auto-style12">
                                <tr>
                                    <td class="auto-style58"></td>
                                    <td class="auto-style61">&nbsp;<ajax:Rating ID="rating2" AutoPostBack="true" runat="server" StarCssClass="ratingEmpty" WaitingStarCssClass="ratingSaved" EmptyStarCssClass="ratingEmpty" FilledStarCssClass="ratingFilled"></ajax:Rating>&nbsp;</td>
                                    <td class="auto-style59" style="vertical-align:bottom">&nbsp;<span class="auto-style11">Review by:</span><asp:Label ID="lblUser" runat="server" CssClass="auto-style11"></asp:Label>
                                        <span class="auto-style11">&nbsp;on </span>
                                        <asp:Label ID="lblDateTime" runat="server" style="font-size: small" Text="<%# Bind('ReviewDate') %>"></asp:Label>
                                    </td>
                                    <td class="auto-style60"></td>
                                </tr>
                                <tr>
                                    <td class="auto-style56">&nbsp;</td>
                                    <td class="auto-style57" colspan="2">
                                        <asp:Label ID="lblReview" runat="server" Text="<%# Bind('Review') %>"></asp:Label>
                                    </td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td class="auto-style56">&nbsp;</td>
                                    <td class="auto-style57" colspan="2">&nbsp;</td>
                                    <td>&nbsp;</td>
                                </tr>
                            </table>

                        </ItemTemplate>
                        </asp:TemplateField>
                        </Columns>
                        </asp:GridView>

我尝试将评分绑定如下:

 <ajax:Rating ID="rating2" CurrentRating=<%# Rating %>

但不起作用。所以我想到了使用gridview的rowdatabound事件来绑定评级如下:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string CS = ConfigurationManager.ConnectionStrings["IndiaLystConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select Rating from tblReview where RegistrationId=@RegistrationId", con);
            cmd.Parameters.AddWithValue("@RegistrationId", lblId.Text);
            SqlDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            AjaxControlToolkit.Rating star = (AjaxControlToolkit.Rating)GridView1.FindControl("rate2");
            star.CurrentRating = Convert.ToInt32(rdr["Rating"].ToString());
        }
    }
}

这又不起作用了。那么在这种情况下我应该怎么做。我认为这很简单,但结果却是一项艰巨的任务。

【问题讨论】:

    标签: c# asp.net ajax gridview rating


    【解决方案1】:

    你可以试试这条线

    <ajax:Rating ID="rating2" CurrentRating=<%# Bind('Rating') %>
    

    你也可以像这样在 rowdatabound 事件上做

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            AjaxControlToolkit.Rating star = (AjaxControlToolkit.Rating)e.Row.FindControl("rating2");
            star.CurrentRating = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Rating"));
    
        }
    }
    

    【讨论】:

    • 它仍然说“指定的演员表无效。”
    • star.CurrentRating = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Rating"));
    • 能否在debug模式下查看,DataBinder.Eval(e.Row.DataItem, "Rating")的值是多少?
    • 我很尴尬地告诉你我从来没有使用过调试模式,这意味着我对此一无所知。你有教程或一些好的视频,我可以学习如何测试使用调试器模式和断点的代码?
    • 好吧,我只是这样做了,看起来它是 NULL。
    【解决方案2】:

    试试这个,肯定会起作用的

    <cc1:Rating ID="rtUserRating" ReadOnly="true" runat="server" 
    StarCssClass="Star" WaitingStarCssClass="WaitingStar" CurrentRating=**'
    <%# Convert.ToInt16(Eval("Rating")) %>
    
    '** EmptyStarCssClass="Star" FilledStarCssClass="FilledStar">
    </cc1:Rating>
    

    希望你能从上面的代码中得到参考

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多