简体   繁体   English

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

[英]update Ajax rating control, update comments

I am trying to update the value of ajaxrating control and comments in the database` 我正在尝试更新数据库中的邻接控制和注释的值。

`  <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>

So I added the following rowcommand event on of the members suggestion. 因此,我在成员建议中添加了以下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);
            }

But here instead of getting the new rating, it is inserting the CurrentRating in the table. 但是,这里没有获得新的评级,而是在表中插入了CurrentRating。

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

I think its because of this CurrentRating here. 我认为是因为这里有CurrentRating。 Any idea how to get the value of updated rating? 任何想法如何获得更新的评级的价值? Or should i use an additional Rating_changed event to update the rate, and then a row command event to update the comments 或者我应该使用附加的Rating_changed事件来更新费率,然后使用行命令事件来更新评论

Thanks Sun 谢谢孙

The easiest way to bind the your DataKey/ItemID to the Tag attribute of the Rating control 将您的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>

Event Handler 事件处理程序

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)
 }

You can use GridView1_RowCommand to update rating score in the DB. 您可以使用GridView1_RowCommand更新数据库中的评分。 eg 例如

 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;
    }
}

Set CommandName="Rating" to your linkbutton CommandName="Rating"为您的linkbutton

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM