Thursday, 14 June 2012

 Upload  Image and write comment of each image and show user name and comment time
.aspx code

<asp:Panel ID="Panel_Image" runat="server" Visible="False" Width="499px">
    <table>
        <tr>
            <td>
                Upload Image</td>
            <td>
                <asp:FileUpload ID="FileUpload_image" runat="server" />
            </td>
       <td>
       <asp:Button ID="btnsave_image" runat="server" Text="POST" BackColor="#666699"
           Font-Bold="True" ForeColor="White" Height="25px" Width="69px"
           style="margin-left: 20px" onclick="btnsave_image_Click" />
   </td>
        </tr>
 </table>
    </asp:Panel>

<asp:Panel ID="pnl_image_show" runat="server" Visible="true">
<asp:DataList ID="DataList_image" runat="server"
     RepeatColumns="0" RepeatDirection="Vertical" CellPadding="2" CellSpacing="2"
        Width="406px" onitemcommand="DataList_image_ItemCommand"
        onitemdatabound="DataList_image_ItemDataBound"
        onselectedindexchanged="DataList_image_SelectedIndexChanged" >
        <ItemTemplate>
        <div >
        <table>
        <tr>
        <td>
        <asp:Image Height="150" Width="150" ID="img1" class="imgzoom"  runat="server" ImageUrl='<%#Eval("images")%>' />
        <tr>
        <td>
        <asp:Label ID="lblimgid" Visible="false" runat="server" Text='<%#Eval("image_id") %>' />
       
<%---Repeater for show comments---%>
<span style="background-color:#666699; border-width:1px; font-size:medium; color:White; font-weight:bold;">Total comments:
<asp:Label ID="lblcount" runat="server"></asp:Label></span>

<asp:Repeater ID="rptimagecomment" runat="server">
<HeaderTemplate>
<table style="width:350px; min-width:350px; background-color:#d0e4fe" cellpadding="0" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="background-color:#ccccff; border-width:1px; font-size:medium; color:Black;">
<asp:TextBox ID="txt1" runat="server" Width="350px" ReadOnly="true" BackColor="#D3D3D3"  TextMode="MultiLine" style="overflow:auto" onkeyup="AutoExpand(this, event)" Rows="1" Text='<%#DataBinder.Eval(Container.DataItem,"com_desc") %>' />
</td>
</tr>
<tr>
<td  style="background-color:#CCCCFF; border-width:1px; font-size:medium; color:Black; text-align:right ">
Posted By:
<asp:Label ID="lblusername" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"username") %>' />
/
<asp:Label ID="lblpostdate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"com_postdate") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
  <asp:TextBox ID="txt_comment" runat="server"  TextMode="MultiLine" Width="350px"
style="overflow:auto" onkeyup="AutoExpand(this, event)" Rows="2" />
   <asp:Button ID="btnsubmitcomment" runat="server" Text="comment" />
  </td></tr>
     </table>    
        </ItemTemplate>
    </asp:DataList>
</asp:Panel>

aspx.cs code

 protected void btnsave_image_Click(object sender, EventArgs e)
    {
        try
        {
            string filename = FileUpload_image.FileName;
            string path = Server.MapPath("");
            string bpath = path + "/" + "/Upload/Images" + "/" + filename;
            FileUpload_image.SaveAs(bpath);
            String img = "Upload/Images" + "/" + filename;

            con.Open();
            DateTime dt = DateTime.Now;
            string str = "image_ins";
            cmd = new SqlCommand(str, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@image", img);      
            cmd.Parameters.AddWithValue("@pstdate", dt);
            cmd.ExecuteNonQuery();
            con.Close();
            imageshow();
            }

    public void imageshow()
    {
        DataSet dsimg = new DataSet();
        da = new SqlDataAdapter("select id,images from skimage", con);
        da.Fill(dsimg);
        DataList_image.DataSource = dsimg;
        DataList_image.DataBind();        
    }

    protected void DataList_image_ItemCommand(object source, DataListCommandEventArgs e)
    {

        DateTime dt = DateTime.Now;
        TextBox txtcomment = (TextBox)e.Item.FindControl("txt_comment");
        Label imgid = (Label)e.Item.FindControl("lblimgid");

        string username = Request.QueryString["id"];
        //string str = "insert into skcomment (com_desc,com_postdate,image_id) values(+'" + txtcomment.Text + "','" + dt + "'," + imgid.Text + ")";
        string s = "imagecom_ins";
        cmd = new SqlCommand(s, con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@desc", txtcomment.Text);
        cmd.Parameters.AddWithValue("@date", dt);
        cmd.Parameters.AddWithValue("@id", imgid.Text);
        cmd.Parameters.AddWithValue("@username", username);
        con.Open();
        cmd.ExecuteNonQuery();

        Label id = (Label)e.Item.FindControl("lblimgid");
        Repeater rpt = (Repeater)e.Item.FindControl("rptimagecomment");
        Label count = (Label)e.Item.FindControl("lblcount");

        string str = "select com_desc,username,com_postdate from comments where image_id='" + id.Text + "'";
        da = new SqlDataAdapter(str, con);
        DataSet dsimg1 = new DataSet();
        da.Fill(dsimg1);

        rpt.DataSource = dsimg1;
        rpt.DataBind();
        con.Close();
        txtcomment.Text = string.Empty;

        if (rpt.Items.Count != 0)
        {
            Label lbl = (Label)e.Item.FindControl("lblcount");
            lbl.Text = rpt.Items.Count.ToString();
        }
        else
        {
            Label lbl = (Label)e.Item.FindControl("lblcount");
            lbl.Text = "0";
        }
    }

    protected void DataList_image_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label id = (Label)e.Item.FindControl("lblimgid");
            Repeater rpt = (Repeater)e.Item.FindControl("rptimagecomment");

            string str = "select com_desc,username,com_postdate from comments where image_id=" + id.Text + "";
             con.Open();
            da = new SqlDataAdapter(str, con);
            DataSet dsimg2 = new DataSet();
            da.Fill(dsimg2);
            rpt.DataSource = dsimg2;
            rpt.DataBind();
            con.Close();
        }

    }
       

No comments:

Post a Comment