Friday 28 February 2014

Show Number of Characters Remaining in Textbox or Textarea inside gridview.



Hello dear, 

    This operation can easily perform but it became tedious task when you have to do this for gridview controls (textbox) and evvn much tedious when there is
    multiple textbox with same ids (in case of multiple rows in gridview).  I have aslo faced this situation and after expendign more then 5 hrs I came to solve
    this problem. Here is the code. This may help you!

   
   
   
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Remark</title>
  

    <script src="../../../Resources/JSFiles/jquery-1.7.2.min.js" type="text/javascript"></script>

  
    <script type="text/javascript">
        $(function() {
            var maxlength = 120;
            $("[id^=grStudentRemark] input[type=text]").on("keypress", function(e) {
                var Len = $(this).val().length;
                var remChar = maxlength - Len;

                if (remChar > -1) {
                    $(this).next('span').text(remChar);
                }
                if (Len > maxlength) {
                    //e.preventDefault();
                    $(this).val($(this).val().substring(0, maxlength));
                }
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>

          
        <asp:GridView ID="grStudentRemark" runat="server" AutoGenerateColumns="False" BackColor="White"
            Visible="false" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
           GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <Columns>
                <asp:TemplateField HeaderText="Remark">
                    <ItemTemplate>
                       <asp:TextBox ID="txtMultipleRemark1"
                            MaxLength="200" runat="server" Width="350px" Style="margin-bottom: 1px;" /><asp:Label
                                ID="r1" runat="server" Style="background-color: #E2EEF1; color: Red; font-weight: normal;">120</asp:Label><br />
                       <asp:TextBox ID="txtMultipleRemark2"
                            MaxLength="200" runat="server" Width="350px" Style="margin-bottom: 1px;" /><asp:Label
                                ID="r2" runat="server" Style="background-color: #E2EEF1; color: Red; font-weight: normal;">120</asp:Label><br />
                       <asp:TextBox ID="txtMultipleRemark3"
                            MaxLength="200" runat="server" Width="350px" Style="margin-bottom: 1px;" /><asp:Label
                                ID="r3" runat="server" Style="background-color: #E2EEF1; color: Red; font-weight: normal;">120</asp:Label><br />
                       <asp:TextBox ID="txtMultipleRemark4"
                            MaxLength="200" runat="server" Width="350px" Style="margin-bottom: 1px;" /><asp:Label
                                ID="r4" runat="server" Style="background-color: #E2EEF1; color: Red; font-weight: normal;">120</asp:Label><br />
                       <asp:TextBox ID="txtMultipleRemark5"
                            MaxLength="200" runat="server" Width="350px" Style="margin-bottom: 1px;" /><asp:Label
                                ID="r5" runat="server" Style="background-color: #E2EEF1; color: Red; font-weight: normal;">120</asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
    </asp:GridView>
               
    </div>
    </form>
</body>
</html>



Cheers!!!

Friday 21 February 2014

Count number of left and right child


Select * FROM MLM



--    exec pr_CntLandR '1'
Create proc pr_CntLandR @UID varchar(10)
as
with Traverse as
(
  select
    UID, cast(POS as varchar(max)) Nodes
  from
    dbo.MLM c
  where
    P_ID = 0
  union all
  select
    c.UID, r.Nodes + CAST(c.POS as varchar)
  from
    dbo.MLM c
  join
    Traverse r on r.UID = c.P_ID
)
select
  t1.UID,-- u.P_ID,
  (select COUNT(*) from Traverse t2 where t1.Nodes + '1' = LEFT(t2.Nodes, len(t1.Nodes) + 1)) LeftCount,
  (select COUNT(*) from Traverse t2 where t1.Nodes + '2' = LEFT(t2.Nodes, len(t1.Nodes) + 1)) RightCount
from
  Traverse t1
join
  dbo.MLM u on u.UID = t1.UID
 where t1.UID=@UID
 order by
  t1.UID
option (maxrecursion 0)






Note: 1-> Left & 2-> Right childs.

Resize image and show




<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
        <asp:ImageButton ID="imgBtn" runat="server" ToolTip="To download right click 'Save Image As'" />
    </div>
</form>





using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Collections.Specialized;
using System.Drawing.Imaging;



public partial class ProgramFiles_OnlineRegistrationForm_ImageResizer : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {          
            string FilePath = System.Configuration.ConfigurationManager.AppSettings["UpcomingEventPath"].ToString();
            Session["PhotoGalleryPath"] = FilePath;
        }
    }
   
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string FileName1 = "";
        if (FileUpload1.HasFile == true)
        {
            FileName1 = FileUpload1.FileName.ToUpper(); ;
            string Extension = Path.GetExtension(FileName1);

            if (Extension == ".JPG" || Extension == ".JPEG" || Extension == ".PNG" || Extension == ".GIF" || Extension == ".BMP")
            {
                if (File.Exists(Session["PhotoGalleryPath"].ToString()))
                {
                    File.Delete(Session["PhotoGalleryPath"].ToString());
                }
                else
                {
                    FileUpload1.SaveAs(Server.MapPath((System.Configuration.ConfigurationManager.AppSettings["UpcomingEventPath"].ToString())) + FileName1);


                    string path = Server.MapPath((System.Configuration.ConfigurationManager.AppSettings["UpcomingEventPath"].ToString())) + FileName1;
                   
                    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                    using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 120, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            thumbnail.Save(memoryStream, ImageFormat.Png);
                            Byte[] bytes = new Byte[memoryStream.Length];
                            memoryStream.Position = 0;
                            memoryStream.Read(bytes, 0, (int)bytes.Length);
                            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                            imgBtn.ImageUrl = "data:image/png;base64," + base64String;
                            imgBtn.Visible = true;
                        }
                    }
                }
            }
        }
    }

    public bool ThumbnailCallback()
    {
        return false;
    }
}



Note :

    1. Add <add key="UpcomingEventPath" value="\DemoSite\UserSpace\UpcomingEvents\"/> in <appSettings> @ web.Congic
    2. Create two folder inside your project (DemoSite) USerSpace & UpcomingEvents.
    3. You can save this resized image by right click and save as on that thumbnal.



Cheers!!!