Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Monday, 5 August 2013

Select multiple date with limited range date selection from Ajax Calender control and show these in gridview in asp.net

.aspx contents:

<asp:Calendar ID="Calendar1" runat="server" BackColor="#CCF0C8" BorderColor="White"
           Font-Underline="false" BorderWidth="1px" Font-Names="Calibri" Font-Size="9pt"
           Font-Bold="true" ForeColor="Black" Height="155px" NextPrevFormat="ShortMonth"
           OnPreRender="Calendar1_PreRender" Width="170px" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" BackColor="#B4C86F" />
   <NextPrevStyle Font-Bold="True" Font-Size="12px" ForeColor="#F5E9CB" VerticalAlign="Bottom" />
    <OtherMonthDayStyle ForeColor="#999999" />
     <SelectedDayStyle BackColor="Maroon" ForeColor="White" />
     <TitleStyle BackColor="#227E15" BorderColor="White" BorderWidth="2px" Font-Bold="True"
                                                                                Font-Size="10pt" ForeColor="White" />

   </asp:Calendar>


<asp:GridView ID="GridView1" Font-Names="Calibri" Font-Size="Small" runat="server"
          EmptyDataText="No Selected Dates" CellPadding="4" AutoGenerateColumns="false"
          ForeColor="#333333" GridLines="None" Width="150px">
         <AlternatingRowStyle BackColor="#C4F9BD" />
        <HeaderStyle BackColor="#227E15" Font-Bold="True" ForeColor="White" Font-Size="14px" />
         <RowStyle BackColor="#EFF3FB" />
         <Columns>
         <asp:TemplateField HeaderText="Selected Date" ItemStyle-HorizontalAlign="Center">
               <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%#                                                     DateTime.Parse(Container.DataItem.ToString()).ToString("yyyy-MM-dd") %>' ></asp:Label>
               </ItemTemplate>
          <ItemStyle HorizontalAlign="Center" Font-Bold="true" />
           </asp:TemplateField>
           </Columns>
           </asp:GridView>


C# code:

        public List<DateTime> SelectedDates
        {
            get
            {
                if (ViewState["Dates"] == null)
                    ViewState["Dates"] = new List<DateTime>() { DateTime.MaxValue.AddDays(-2) };
                return (List<DateTime>)ViewState["Dates"];
            }
            set
            {
                ViewState["Dates"] = value;
            }
        }
        protected void Calendar1_PreRender(object sender, EventArgs e)
        {
            // Reset Selected Dates
            Calendar1.SelectedDates.Clear();
            // Select previously Selected Dates
            foreach (DateTime dt in SelectedDates)
                Calendar1.SelectedDates.Add(dt);
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {


            if (SelectedDates.Contains(Calendar1.SelectedDate))
                SelectedDates.Remove(Calendar1.SelectedDate);
            else
                SelectedDates.Add(Calendar1.SelectedDate);
            //update selection    
            GridView1.Visible = true;
            ViewState["Dates"] = SelectedDates;
            GridView1.DataSource = SelectedDates.Skip(1);
            GridView1.DataBind();
         
        }


        protected void Calendar1_DayRender(Object sender, DayRenderEventArgs e)
        {
         
            // Max day is to be the day before today, so subtract one day.
            DateTime maxDate = DateTime.Now.AddDays(ld);
            maxDate.AddDays(-1);
            if ((e.Day.Date > maxDate) || e.Day.Date.CompareTo(DateTime.Today.AddDays(-1)) < 0)
            {
                // Only make dates that are in our range selectable:
                e.Day.IsSelectable = false;
                e.Cell.ForeColor = Color.Gray;

            }


        }

Line/Word/Character Count in Textbox Control using Javascript funtion

JavaScript Funtion:

function LineCount(value) {
       
                    var text;
                    var lines_totals1;
                    var len;
                    var line_count;
                    var lines_totals2;
                    var diff_fli;
                    var totalCost;
                    var ExtraLine;

                    text = value;
                    lines_totals1 = Math.ceil(value.length / 23);
                    len = wordwrap(text, 23, "\n", true);
                    line_count = len.split("\n");
                    lines_totals2 = line_count.length;
                    diff_fli = lines_totals2 - lines_totals1;

                    if (diff_fli > 1) {
                        lines_totals = Math.ceil((lines_totals2 + lines_totals1) / 2);
                    }
                    else {
                        lines_totals = lines_totals2;
                    }
            }
         
        function wordcount(value) {
         
                var totalCost;
                var ExtraWord;

                var andchr = value.split(" & ").length - 1;
                var char_count = value.length;
                var fullStr = value + " ";

                //word count for regional language
                v = value.split(' ');
                var word_count = v.length;
                var cheArr = Array('@', '.', '"', "'", '_', '-', '+', '=', ';', '&', '*', '\(', '\)', '{', '}', '[', '}', '|', '\\', '\,', '/');
                for (i = 0; i <= cheArr.length; i++) {
                    word_count = word_count + value.split(cheArr[i]).length - 1;
                }

             
        }


        function charcount(value) {

         
                var totalCost;
                var ExtraWord;
                value = value.replace(/^\s+|\s+$/g, "");
                var word_count = value.length;

             

        }

Text Changed Event on Ajax HTML Editor Extender Control Textbox

Java Script to be Used :

<script type="text/javascript">
function onContentsChange12() 
        {
            //document.getElementById('Labeld21').innerText = document.getElementById('TextBoxd21').value;
    var inputText = document.getElementById('TextBoxd12').value;
    var returnText = "" + inputText;
        //-- remove BR tags and replace them with line break
    returnText=returnText.replace(/<br>/gi, "\n");
    returnText=returnText.replace(/<br\s\/>/gi, "\n");
    returnText=returnText.replace(/<br\/>/gi, "\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText=returnText.replace(/<p.*>/gi, "\n");
    returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText=returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g,'');

    //-- get rid of html-encoded characters:
    returnText=returnText.replace(/&nbsp;/gi," ");
    returnText=returnText.replace(/&amp;/gi,"&");
    returnText=returnText.replace(/&quot;/gi,'"');
    returnText=returnText.replace(/&lt;/gi,'<');
    returnText=returnText.replace(/&gt;/gi,'>');
    //-- return
    document.getElementById('Labeld12').innerHTML = returnText;
        }
</script>

paste this in header section and then write below code in body tag

<ajt:HtmlEditorExtender ID="HtmlEditorExtenderd12" runat="server" 
            TargetControlID="TextBoxd12" EnableSanitization="false"  OnClientChange="onContentsChange12">
            
        </ajt:HtmlEditorExtender>

Friday, 10 May 2013

Upload Multiple Files with One Fileupload Control and Save these file in database as new row



.aspx Page Content

<style type="text/css">
        #divFile p { font:13px Century }
        #divFile h3 { font:16px Century; font-weight:bold; }
    </style>
    <script type="text/javascript">
    $('#btUpload').click(function() { if (fileUpload.value.length == 0) { alert('No files selected.'); return false; } });
</script>
<div id="divFile" style="width: 95%;float:right; text-align:center">
<b>Upload Multiple Reports by selecting multiple reports files</b>
            <p><asp:FileUpload ID="fileUpload" multiple="true" runat="server" /></p>
            <p><input type="button" id="btUpload" value="Upload Reports"
                onserverclick="btUpload_Click" runat="server" /></p>
            <p><asp:label id="lblFileList" runat="server"></asp:label></p>
            <p><asp:Label ID="lblUploadStatus" runat="server"></asp:Label></p>
            <p><asp:Label ID="lblFailedStatus" runat="server"></asp:Label></p>
            <br />
            </div>



.cs Page Content


protected void btUpload_Click(object sender, EventArgs e)
        {
            if (fileUpload.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
            {
                int iUploadedCnt = 0;
                int iFailedCnt = 0;
                HttpFileCollection hfc = Request.Files;

                lblFileList.Text = "Select <b>" + hfc.Count + "</b> file(s)";

                if (hfc.Count <= 40)    // 40 FILES RESTRICTION.
                {
                    for (int i = 0; i <= hfc.Count - 1; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            if (!File.Exists(Server.MapPath("../Report/") + Path.GetFileName(hpf.FileName)))
                            {
                                DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("../Report/"));
                                string sFileName = Path.GetFileName(hpf.FileName);
                                string sFileExt = Path.GetExtension(hpf.FileName);

                                // CHECK FOR DUPLICATE FILES.
                                FileInfo[] objFI = objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");

                                if (objFI.Length > 0)
                                {
                                    // CHECK IF FILE WITH THE SAME NAME EXISTS (IGNORING THE EXTENTIONS).
                                    foreach (FileInfo file in objFI)
                                    {
                                        string sFileName1 = objFI[0].Name;
                                        string sFileExt1 = Path.GetExtension(objFI[0].Name);

                                        if (sFileName1.Replace(sFileExt1, "") == sFileName.Replace(sFileExt, ""))
                                        {
                                            iFailedCnt += 1;        // NOT ALLOWING DUPLICATE.
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    // SAVE THE FILE IN A FOLDER.
                                    string pid = "";
                                    string fname = "";
                                    if (Session["HID"].ToString() == "ultrad")
                                    {
                                        pid = hpf.FileName.Substring(0, 5);
                                        fname = hpf.FileName.Substring(5, hpf.FileName.Length - 5);
                                    }
                                    else
                                    {
                                        pid = hpf.FileName.Substring(0, 6);
                                        fname = hpf.FileName.Substring(6, hpf.FileName.Length - 6);
                                    }

                                    string path1 = Server.MapPath("../Report/Hosp/") + Session["HID"].ToString();

                                    if (!Directory.Exists(path1))
                                    {
                                        Directory.CreateDirectory(path1);
                                    }

                                    string path = Server.MapPath("../Report/Hosp/" + Session["HID"].ToString() + "/") + pid;

                                    if (!Directory.Exists(path))
                                    {
                                        Directory.CreateDirectory(path);
                                    }

                                 
                                    hpf.SaveAs(Server.MapPath("../Report/Hosp/" + Session["HID"].ToString() + "/" + pid + "/") + Path.GetFileName(DateTime.Now.ToString("yyyyMMddHHmmss") + " " + hpf.FileName));

                                    du.ExecuteSql("insert into Patient_MR(PatientId,RName,ReportPath,Hospital) values('" + pid + "','" + fname + "','Report/Hosp/" + Session["HID"].ToString() + "/" + pid + "/" + Path.GetFileName(DateTime.Now.ToString("yyyyMMddHHmmss") + " " + hpf.FileName) + "','" + Session["HID"].ToString() + "')");
                                 
                                    iUploadedCnt += 1;
                                }
                            }
                        }
                    }
                    lblUploadStatus.Text = "<b>" + iUploadedCnt + "</b> file(s) Uploaded.";
                    lblFailedStatus.Text = "<b>" + iFailedCnt + "</b> duplicate file(s) could not be uploaded.";
                    fileUpload.Attributes.Clear();

                }
                else lblUploadStatus.Text = "Max. 40 files allowed.";
            }
            else lblUploadStatus.Text = "No files selected.";
        }