更新Ajax评级控件,更新评论

时间:2011-07-28 01:19:31

标签: c# asp.net ajax gridview

我正在尝试更新数据库中的ajaxrating控件和注释的值

`  <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false" DataKeyNames="id"
           onrowdatabound="GridView1_RowDataBound" >
        <Columns>
        <asp:BoundField  HeaderText="PurchasedPID"  DataField="PurchasedPID"/>
         <asp:BoundField  HeaderText="DatetimePurchased" DataField="orderdate" />
        <asp:BoundField  HeaderText="MMBName" DataField="MMBName" />
        <asp:TemplateField HeaderText="Rating">
        <ItemTemplate>
                    <asp:Rating  RatingDirection="LeftToRightTopToBottom" Visible="true" AutoPostBack="true"
                      ID="Rating2" runat="server" MaxRating="5"  

StarCssClass="star_rating" EmptyStarCssClass="star_empty" 
                          FilledStarCssClass="star_filled" WaitingStarCssClass="star_saved" CurrentRating='<%# Bind("Rating") %>'
                          OnChanged="Rating2_Changed" >
                        </asp:Rating>
                        </ItemTemplate>
                 </asp:TemplateField>

                 <asp:TemplateField HeaderText="Comments">
                 <ItemTemplate>
                 <asp:TextBox ID="TextBox1" runat="server" Text= '<%# Bind("Comments") %>' multiline="true">
                </asp:TextBox>
                 </ItemTemplate>
                 </asp:TemplateField>

                 <asp:TemplateField HeaderText="Action">
                 <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server">Submit</asp:LinkButton>  
                 </ItemTemplate>
                 </asp:TemplateField>
    </Columns>
                       </asp:GridView>

所以我在成员建议上添加了以下rowcommand事件。

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Submit")
            {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                Int32 Id = Convert.ToInt32(e.CommandArgument);
                int ratingScore = ((AjaxControlToolkit.Rating)row.FindControl("Rating2")).CurrentRating;
                TextBox TextComments = row.FindControl("TextBox1") as TextBox;
                string comments = TextComments.Text;
                objBLL.UpdateRating(ratingScore, Id,comments);
            }

但是这里没有获得新的评级,而是在表中插入CurrentRating。

  int ratingScore = ((AjaxControlToolkit.Rating)row.FindControl("Rating2")).CurrentRating;

我认为这是因为这里的CurrentRating。 知道如何获得更新评级的价值吗?或者我应该使用额外的Rating_changed事件来更新速率,然后使用行命令事件来更新注释

由于 太阳

2 个答案:

答案 0 :(得分:1)

将DataKey / ItemID绑定到Rating控件的Tag属性的最简单方法

<asp:Rating  RatingDirection="LeftToRightTopToBottom" Visible="true" 
     AutoPostBack="true"
     ID="Rating2" runat="server" MaxRating="5" **Tag='<%# Bind("id")%>'** 
     StarCssClass="star_rating" EmptyStarCssClass="star_empty" 
     FilledStarCssClass="star_filled" WaitingStarCssClass="star_saved"
     CurrentRating='<%# Bind("Rating") %>'
     OnChanged="Rating2_Changed" >
                    </asp:Rating>

事件处理程序

protected void Rating2_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
         Rating r = sender as Rating;
         int id = Convert.ToInt32(r.Tag);
         objBLL.UpdateRating(Convert.ToInt32(e.Value),id)
 }

答案 1 :(得分:0)

您可以使用GridView1_RowCommand更新数据库中的评分。 e.g。

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Rating")
    {
        GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
        Int32 Id = Convert.ToInt32(e.CommandArgument);
        ratingScore = ((AjaxControlToolkit.Rating)row.FindControl("Rating2")).CurrentRating;
    }
}

CommandName="Rating"设置为您的链接按钮

<asp:TemplateField HeaderText="Action">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Rating">Submit</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>