Monday, May 20, 2013

Limit maximum characters in textbox (textarea) and display remaining count using javascript in C# Asp.Net

function Count(text, long) {
          var maxlength = new Number(long); // Change number to your max length.
          if (text.value.length > maxlength) {
              text.value = text.value.substring(0, maxlength);
              //alert(" Only " + long + " chars");
          }
          //Display remaining count
          //ctl00_ContentPlaceHolder1_lblCount.innerHTML = maxlength - text.value.length;
          var lblMsg = $get('<%= lblCount.ClientID %>');
          lblMsg.innerHTML = maxlength - text.value.length;
      }

<asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server" onKeyUp="javascript:Count(this,10);" onChange="javascript:Count(this,10);" ></asp:TextBox>
    <br />
    <asp:Label ID="lblCount" runat="server" Text="10"></asp:Label>

Writing Data in log file or text file line by line in C# Asp.Net

private void WriteTextFile()
    {
        string FileLoc = "C:\\LOGFOLDER\\TextFile.txt";

//This will  create the new file if the file is not exist.
        if (!File.Exists(FileLoc))
        {
            using (StreamWriter sw = File.CreateText(FileLoc))
            {
                sw.WriteLine("My Content Data");
            }
        }

//This will append to the existing file if the file already exist.
        else
        {
            using (StreamWriter sw = new StreamWriter(FileLoc, true))
            {
                sw.WriteLine("My Content Data");
            }
        }
    }