我有一个简单的表格,存储评论以及评级如下:
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"> <ajax:Rating ID="rating2" AutoPostBack="true" runat="server" StarCssClass="ratingEmpty" WaitingStarCssClass="ratingSaved" EmptyStarCssClass="ratingEmpty" FilledStarCssClass="ratingFilled"></ajax:Rating> </td>
<td class="auto-style59" style="vertical-align:bottom"> <span class="auto-style11">Review by:</span><asp:Label ID="lblUser" runat="server" CssClass="auto-style11"></asp:Label>
<span class="auto-style11"> 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"> </td>
<td class="auto-style57" colspan="2">
<asp:Label ID="lblReview" runat="server" Text="<%# Bind('Review') %>"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style56"> </td>
<td class="auto-style57" colspan="2"> </td>
<td> </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());
}
}
}
再也无法工作。所以我应该在这种情况下做些什么。我认为这很简单,但事实证明这是一项艰巨的任务。
答案 0 :(得分:0)
你可以尝试这一行
<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"));
}
}
答案 1 :(得分:0)
试试这个,它肯定会起作用
<cc1:Rating ID="rtUserRating" ReadOnly="true" runat="server"
StarCssClass="Star" WaitingStarCssClass="WaitingStar" CurrentRating=**'
<%# Convert.ToInt16(Eval("Rating")) %>
'** EmptyStarCssClass="Star" FilledStarCssClass="FilledStar">
</cc1:Rating>
希望您能从上面的代码中获得参考