喜欢图像的数量

时间:2013-12-08 09:21:00

标签: c# asp.net

我正在gridview

中创建一个图库(图像文件夹中存储的图像,db中保存的路径)

每个imagename都保存在dbwith和uinque id以及columnname:storyid。

任何人都可以告诉我如何绑定个人图像“喜欢”计数在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 = "aditya.arjula@gmail.com";
        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();
         }
        }
    }
}

1 个答案:

答案 0 :(得分: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