C# 将数据库中的smallint绑定到gridview中的ajax stars rating
我有一个简单的表格,其中存储了评论和评级,如下所示:C# 将数据库中的smallint绑定到gridview中的ajax stars rating,c#,asp.net,ajax,gridview,rating,C#,Asp.net,Ajax,Gridview,Rating,我有一个简单的表格,其中存储了评论和评级,如下所示: 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 Get
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()
)
<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 %>
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());
}
}
}
我有一个gridview,它有一个AJAX stars评级控件。我所要做的就是使用基于RegistrationId的所有评论填充gridview。gridview如下所示:
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()
)
<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 %>
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());
}
}
}
这又不起作用了。那么在这种情况下我该怎么办呢?我原以为这很简单,但事实证明这是一项艰巨的任务。你可以试试这句话
<ajax:Rating ID="rating2" CurrentRating=<%# Bind('Rating') %>
试试这个,肯定有用
<cc1:Rating ID="rtUserRating" ReadOnly="true" runat="server"
StarCssClass="Star" WaitingStarCssClass="WaitingStar" CurrentRating=**'
<%# Convert.ToInt16(Eval("Rating")) %>
'** EmptyStarCssClass="Star" FilledStarCssClass="FilledStar">
</cc1:Rating>
希望您能从上面的代码中得到参考它仍然说“指定的强制转换无效”。star.CurrentRating=Convert.ToInt32DataBinder.Evale.Row.DataItem,Rating;您可以在调试模式下检查DataBinder.Evale.Row.DataItem的值是多少吗,评级?我真的很不好意思告诉你,我从来没有使用过调试模式,这意味着我对它一无所知。你有教程或好的视频,我可以学习如何使用调试模式和断点测试代码吗?我刚刚做了,它看起来是空的。