2011-07-28 75 views
0

我試圖更新更新阿賈克斯評分控制,更新評論

` <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事件的成員建議在database` ajaxrating控制和評論的價值。

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事件更新的速度,然後再行命令事件更新評論

感謝 孫

+0

這可能有所幫助: [GridView with a AJAX rating control ...](http://www.codeproject.com/KB/webforms/MultiFeatureGridView.aspx) – Waqas

回答

0

您可以使用GridView1_RowCommand更新數據庫等級分數。例如

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"你的LinkBut​​ton

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

到您的DataKey /條目標識號綁定到等級控制的標籤屬性

<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) 
} 
最簡單的方法
+0

不錯,它肯定是最簡單的方法.. – Waqas