Showing posts with label comment box in asp.net c# code. Show all posts
Showing posts with label comment box in asp.net c# code. Show all posts

Tuesday, March 29, 2016

Comment Box in ASP.NET C#

In this  article I am going to explain how to write standard code to create comment box in ASP.NET c#.

Create a SQL table for insert comment

CREATE TABLE InsComment (
                CommentId BIGINT identity(1, 1)
                ,NAME VARCHAR(50)
                ,Comment VARCHAR(500)
                )

Now I am writing a XSLT file code to display comment after Save (Create a XSLT fie in "XSLT"  floder with name "History.xslt")
 
Solution Explorer look like


  
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:template match="/">
                     <xsl:for-each select="Comments/Comment">
                           <div style="border:1px solid #cccccc; padding:10px;  margin:5px 0px; color:#50505b;">
                   <div style=" float:left; margin-left:10px;">     
                   <div style="border-bottom:1px solid #cccccc; padding:5px 0px; position:relative;">
 
                   <div style="font-weight:bold; float:left; width:80px; color:blue;">Name :</div>
                   <div style="float:left; color:blue;"><xsl:value-of select="NAME"/></div>
                   <div style="clear:both;"></div>
                   </div>
                    <div style="">
                    <div style="font-weight:bold; float:left; width:100px;">Comment:</div>
                    <div style="float:left;"><xsl:value-of select="Comment"/></div>
                    <div style="clear:both;"></div>
                    </div>
                    </div>
                    <div style="clear:both;"></div>       
                           </div>
                     </xsl:for-each>
       </xsl:template>
</xsl:stylesheet>

Default.aspx Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
 
.drp
        {
            background: #F4F4F4;
            border: 1px solid #DEDCDD;
            color: #555555;
            height: 25px;
            padding-left: 10px;
                     margin-bottom:10px;
            -moz-transition: all 0.3s linear;
            -o-transition: all 0.3s linear;
            -webkit-transition: all 0.3s linear;
            transition: all 0.3s linear;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
        }
             
.drp:focus  { outline:none; border:1px solid #F90; }
 
 
 
.med-form-col-1-8 { width:12.5%; }
.med-form-col-2-8 { width:25%; }
.med-form-col-3-8 { width:37.5%; }
.med-form-col-4-8 { width:50%; }
.med-form-col-5-8 { width:62.5%; }
.med-form-col-6-8 { width:75%; }
.med-form-col-7-8 { width:87.5%; }
.med-form-col-8-8 { width:100%; }
 
.med-form-col-1-8,
.med-form-col-2-8,
.med-form-col-3-8,
.med-form-col-4-8,
.med-form-col-5-8,
.med-form-col-6-8,
.med-form-col-7-8,
.med-form-col-8-8
{
       float:left;
       padding:1px 10px;
      
       box-sizing: border-box;
       -moz-box-sizing: border-box;
       -ms-box-sizing: border-box;
       -webkit-box-sizing: border-box;
       -khtml-box-sizing: border-box;
}
 
.med-form-col-1-8 label,
.med-form-col-2-8 label,
.med-form-col-3-8 label,
.med-form-col-4-8 label,
.med-form-col-5-8 label,
.med-form-col-6-8 label,
.med-form-col-7-8 label,
.med-form-col-8-8 label,
{
       display:block !important;
       width:100% !important;
       margin:0 !important;
       float:none !important;
}
 
.med-form-item.large, .med-form-item.large .drp { width:100%; }
.med-form-item.medium, .med-form-item.medium .drp { width:75%; }
.med-form-item.small, .med-form-item.small .drp { width:55%; }
 
.frmlbl { color: #50505b; padding: 0px 10px 0px 0px; font-size: 14px; font-weight:400; !important }
 
.clr { clear:both; }
 
 
@media only screen and (max-width:600px) {
 
.med-form-col-1-8,
.med-form-col-2-8,
.med-form-col-3-8,
.med-form-col-4-8,
.med-form-col-5-8,
.med-form-col-6-8,
.med-form-col-7-8,
.med-form-col-8-8
{
       display:block !important;
       width:100% !important;
       margin:0 !important;
       float:none !important;
}
 
}
.btn { height: 50px; margin: 0; padding: 0 20px; vertical-align: middle; background: #9c9c9c; border: 0; font-family: 'Raleway', sans-serif; font-size: 16px; font-weight: 300; line-height: 50px; color: #fff; -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; text-shadow: none; -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none; -o-transition: all .3s; -moz-transition: all .3s; -webkit-transition: all .3s; -ms-transition: all .3s; transition: all .3s; }
</style>
 <script type="text/javascript">
     function Enablebutton(val) {
         if (val.value.trim() != "") {
             document.getElementById('#btnsavecomment').disabled = false;
             $('#btnsavecomment').attr("style", "background-color:Yellow");
 
         }
         else {
             document.getElementById('#btnsavecomment').disabled = true;
             $('#btnsavecomment').removeAttr("style");
 
         }
     }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
 
<fieldset>
<legend>Comment</legend>
<div class="med-form-col-8-8">
<label class="frmlbl"><asp:Label ID="lbltotcomment" runat="server" ForeColor="Blue" Font-Bold="true"></asp:Label>
<asp:LinkButton ID="btnlink" runat="server" Text="View all" onclick="btnlink_Click" ></asp:LinkButton>
<asp:LinkButton ID="btnlinktop" runat="server" Text="Last one" onclick="btnlinktop_Click"></asp:LinkButton>
</label>
<div class="med-form-item large">
<asp:Xml ID="xmlContacts" runat="server"></asp:Xml>
<asp:TextBox ID="txtcomment" onkeyup="Enablebutton(this);" runat="server" MaxLength="500"  Height="80" CssClass="drp" TextMode="MultiLine">
</asp:TextBox>
</div>
</div><!-- End col-2-8 -->
 
<div class="clr"></div>
 
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<div align="right" style="padding:10px 15px 0px 0px;">
<asp:Button ID="btnsavecomment" OnClick="btnsavecomment_Click" runat="server" Text="Post comment"  CssClass="btn btn-primary" />
</div>
 
<div style="clear:both;"></div>
 
</fieldset>
    </form>
</body>
</html>

Default.aspx.cs code
 
using System;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    string constr = "Data Source=.;Initial Catalog=Test;uid=******;pwd=**********";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Getcomments();
        }
 
    }
 
    private string Getxml(DataTable dt)
    {
        DataTable dtclone = dt.Clone();
        foreach (DataColumn column in dtclone.Columns)
        {
            column.DataType = typeof(string);
        }
        foreach (DataRow row in dt.Rows)
        {
            dtclone.ImportRow(row);
        }
        foreach (DataRow row in dtclone.Rows)
        {
            for (int i = 0; i < dtclone.Columns.Count; i++)
            {
                dtclone.Columns[i].ReadOnly = false;
 
                if (string.IsNullOrEmpty(row[i].ToString()))
                {
                    row[i] = string.Empty;
                }
                else
                {
                    row[i] = Convert.ToString(row[i]).Trim();
                }
            }
        }
 
        DataSet dsclone = new DataSet();
        dsclone.Tables.Add(dtclone);
        dsclone.DataSetName = "Comments";
        dsclone.Tables[0].TableName = "Comment";
        return dsclone.GetXml();
    }
 
    private void Getcomments(string loadtype = "L")
    {
 
        SqlDataAdapter da = new SqlDataAdapter("select CommentId,NAME,Comment  from InsComment",constr);
 
        DataSet  ds = new DataSet();
 
        da.Fill(ds);
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    lbltotcomment.Text = "Comments " + Convert.ToString(ds.Tables[0].Rows.Count);
                    var dt = ds.Tables[0];
                   
                    if (loadtype == "L")
                    {
                      dt=  ds.Tables[0].AsEnumerable()
                                  .Where((r, i) => i >= dt.Rows.Count-1).CopyToDataTable();
                                 
        
                    }
                    string xml = Getxml(dt);
                   
                    btnlink.Visible = true;
                    btnlinktop.Visible = true;
                    xmlContacts.DocumentContent = xml;
                    xmlContacts.TransformSource = Server.MapPath("~/XSLT/History.xslt");
                }
                else
                {
                    lbltotcomment.Text = "Comments 0";
                    btnlink.Visible = false;
                    btnlinktop.Visible = false;
                }
            }
            else
            {
                lbltotcomment.Text = "Comments 0";
                btnlink.Visible = false;
            }
        }
    }
    protected void btnsavecomment_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(constr))
            {
 
                connection.Open();
                SqlCommand myCommand = new SqlCommand("Insert into InsComment (NAME,Comment) values ('Varun',@Comment)", connection);
                myCommand.Parameters.Add("@Comment", SqlDbType.VarChar, 500);
                myCommand.Parameters["@Comment"].Value = txtcomment.Text.Trim();
                myCommand.CommandType = CommandType.Text;
                int res = myCommand.ExecuteNonQuery();
                connection.Close();
                if (res > 0)
                {
                    txtcomment.Text = string.Empty;
                    Getcomments();
                    lblmsg.Text = "Save Successfully";
                }
                else
                {
                    lblmsg.Text = "Data Not Saved";
                }
            }
 
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message.ToString();
        }
    }
 
    protected void btnlinktop_Click(object sender, EventArgs e)
    {
        Getcomments();
        btnlinktop.Enabled = false;
        btnlink.Enabled = true;
    }
    protected void btnlink_Click(object sender, EventArgs e)
    {
        Getcomments("A");
        btnlinktop.Enabled = true;
        btnlink.Enabled = false;
    }
}


OutPut