2013-12-08 84 views
1

我創建在GridView伯爵喜歡的圖像

每個imagename保存在dbwith和uinque ID和列名的圖片庫(存儲在圖片文件夾中的圖像,保存在數據庫路徑):storyid。

誰能告訴我如何綁定的indiviudal圖像 「喜歡」 在lblcount算

<body> 
<form id="form1" runat="server"> 
<asp:ScriptManager runat="server" ID="SM1"> 
</asp:ScriptManager> 
<div> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Arial" 
     OnRowCommand="GridView1_RowCommand1"> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:UpdatePanel runat="server" ID="up1" UpdateMode="Conditional"> 
         <ContentTemplate> 
          <asp:Button runat="server" ID="IncreaseButton" Text="Like" CommandName="Increase" 
           CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" /> 
           <asp:Label runat="server" ID="lblCount"></asp:Label> 
          <div style="display: none;"> 
           <asp:Label Text="<%#Bind('StoryId')%>" ID="lblStoryid" runat="server"></asp:Label> 
          </div> 
         </ContentTemplate> 
        </asp:UpdatePanel> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:ImageField DataImageUrlField="FilePath" ControlStyle-Width="100" ControlStyle-Height="100" 
       HeaderText="Preview Image"> 
       <ControlStyle Height="100px" Width="100px"></ControlStyle> 
      </asp:ImageField> 
      <asp:TemplateField> 
       <HeaderTemplate> 
        <asp:Label Text="Description" runat="server" Visible="False"></asp:Label> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <asp:Label Text="<%#Bind('Description')%>" ID="lblImageid" runat="server"></asp:Label> 
       </ItemTemplate> 
       <EditItemTemplate> 
       </EditItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
</div> 
</form> 

public partial class Gallery : System.Web.UI.Page 

{

private void bindimage() 
{ 
    DataTable dt = new DataTable(); 
    String strConnString = System.Configuration.ConfigurationManager. 
     ConnectionStrings["dbconnection"].ConnectionString; 
    string strQuery = "select * from story"; 


    SqlCommand cmd = new SqlCommand(strQuery); 
    SqlConnection con = new SqlConnection(strConnString); 
    SqlDataAdapter sda = new SqlDataAdapter(); 
    cmd.CommandType = CommandType.Text; 
    cmd.Connection = con; 
    try 
    { 
     con.Open(); 
     sda.SelectCommand = cmd; 
     sda.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
    finally 
    { 
     con.Close(); 
     sda.Dispose(); 
     con.Dispose(); 
    } 
} 

protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e) 
{ 

    if (e.CommandName == "Increase") 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 
     GridViewRow row = GridView1.Rows[index]; 
     Label listPriceTextBox = (Label)row.FindControl("lblStoryid"); 
     int storyId = Convert.ToInt32(listPriceTextBox.Text); 
     string UserEmailid = "[email protected]"; 
     String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 

     string strQuery = "INSERT INTO Likes(storyId,UserEmailid) VALUES(@storyId,@UserEmailid)"; 
     string LikeCount = "SELECT COUNT(StoryId) FROM Likes Where StoryId=1001;;"; 
     Label lblStoryid = (Label)row.FindControl("lblStoryid"); 
     lblStoryid.Text = LikeCount; 

     SqlCommand cmd = new SqlCommand(strQuery); 
     SqlCommand cmdl = new SqlCommand(LikeCount); 
     cmdl.CommandType = CommandType.Text; 
     cmdl.Connection = con; 


     cmd.Parameters.AddWithValue("@storyId", storyId); 
     cmd.Parameters.AddWithValue("@UserEmailid", UserEmailid); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 

     try 
     { 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      cmdl.ExecuteNonQuery(); 

     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
     } 
     finally 
     { 
      con.Close(); 
      con.Dispose(); 
     } 
     } 
    } 
} 
+0

'SELECT COUNT(StoryId)FROM Likes Where StoryId = 1001;' - 所以根據此查詢,行數表示喜歡的數量? – James

+0

如果您正在使用'<% %>'爲'lable'或某個顯示控件賦值,那麼它應該在''''內。我看到你在''「''('Text =」<%#Bind('StoryId')%>「')中使用它。將其更改爲'Text ='<%#Bind('StoryId')%>'',那麼它就會起作用。 – Bharadwaj

回答

1

你只需要拉數當你在你的bindimage方法中拉取故事記錄時,你會喜歡

SELECT Story.StoryId, Max(Description) As Description, Count(Likes.StoryId) as LikeCount from Story 
INNER JOIN Likes On Likes.StoryId = Story.StoryId 
GROUP BY Story.StoryId 

然後,你可以綁定計數像

<asp:Label Text='<%# Bind("LikeCount")%>' runat="server" ID="lblCount"></asp:Label> 

看到這個fiddle

+0

SELECT * from story,Count(Likes.StoryId)As LikeCount INNER JOIN Like On.StoryId = 1001;消息208,級別16,狀態1,行3 無效的對象名稱「計數」。我收到這個錯誤。 stroy和Likes都是單獨的表格。我沒有定義列 – user2621743

+0

@ user2621743之間的任何關係,你看到我的編輯?這種關係與您使用JOIN將表格拉到一起無關。 – James

+0

對不起,我的錯誤,查詢工作。可以請建議我應該保持代碼顯示喜歡。我的意思是,當網格填充時,它應該獲取個人照片的喜好。當用戶點擊類似按鈕 – user2621743