Showing posts with label multiple. Show all posts
Showing posts with label multiple. Show all posts

Saturday, 5 October 2013

GridView Select all row using Header Checkbox or select one row using checkbox in C#.NET

Copy and Paste this code to .aspx Page :

<asp:GridView ID="GridView1" runat="server" Width="100%" BackColor="White" BorderColor="White"
                                BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None"
                                AutoGenerateColumns="False" Style="font-size: 10pt">
                                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                                <PagerStyle BackColor="#669900" ForeColor="White" Font-Bold="true" />
                                <HeaderStyle BackColor="#669900" Font-Bold="True" ForeColor="#E7E7FF" />
                                <Columns>
                                    <asp:TemplateField HeaderText="Course" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="id" runat="server" Text='<%#Eval("Row_Id")%>' Visible="false"></asp:Label>
                                            <asp:Label ID="lblgrade" runat="server" Text='<%#Eval("Quali1")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="35px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Grade" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblsubgrade" runat="server" Text='<%#Eval("Quali2")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="45px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Roll No" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblroll" runat="server" Text='<%#Eval("RollNo")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="80px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="180px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Father's Name" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lblfname" runat="server" Text='<%#Eval("FName")%>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="180px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:Label ID="lbldate" runat="server" Text=""></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle Width="70px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="cbxap" runat="server" AutoPostBack="true" OnCheckedChanged="cbxap_CheckedChanged" />
                                        </ItemTemplate>
                                        <HeaderTemplate>
                                            A/P<asp:CheckBox ID="cbxapall" runat="server" AutoPostBack="true" OnCheckedChanged="cbxapall_CheckedChanged" />
                                        </HeaderTemplate>
                                        <ItemStyle Width="50px" />
                                    </asp:TemplateField>
                                </Columns>

                            </asp:GridView>


and .cs Page Code is given:

Bind GridView:
private void bindgridview()
        {
            try
            {
                DataTable dt = app.GetDataTable("select * from tb_Regstd_mst where Center='" + ddlcenter.SelectedItem.Text + "' and Quali1='" + ddlgrade.SelectedItem.Text + "' and Quali2='" + ddlsubgrade.SelectedItem.Text + "' order by RollNo Asc");
                GridView1.DataSource = dt;
                GridView1.DataBind();
                foreach (GridViewRow gv in GridView1.Rows)
                {
                    Label date = (Label)gv.FindControl("lbldate");
                    date.Text = DateTime.Now.ToShortDateString();
                }
                if (GridView1.Rows.Count <= 0)
                {
                    btnattd.Visible = false;
                    lblmsg.Text = "No Student Records Found in this Category.";
                }
                else
                {
                    lblmsg.Text = "";
                    btnattd.Visible = true;
                }
            }
            catch (Exception)
            {

            }

        }


On Header Checkbox checkchanged Event :
protected void cbxapall_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkH = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
                if (chkH.Checked == true)
                {
                    foreach (GridViewRow gv in GridView1.Rows)
                    {
                        CheckBox chk = (CheckBox)gv.FindControl("cbxap");
                        chk.Checked = true;
                    }
                }
                else
                {
                    foreach (GridViewRow gv in GridView1.Rows)
                    {
                        CheckBox chk = (CheckBox)gv.FindControl("cbxap");
                        chk.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }


Checkchanged event of each row :
protected void cbxap_CheckedChanged(object sender, EventArgs e)
        {
            bool isFound = false;
            CheckBox cbxall = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                CheckBox chkSelect = gvr.FindControl("cbxap") as CheckBox;
                if (chkSelect.Checked == false)
                {
                    isFound = true;
                    break;
                }

            }

            if (isFound)
                cbxall.Checked = false;
            else
                cbxall.Checked = true;

        }

Monday, 5 August 2013

Get multiple values from checkboxlist selected values in textbox as comma seperated using javascript in asp.net

Java Script Funtion:

function GetSelectedValue() {
        var chkBox = document.getElementById("<%=chkList.ClientID%>");
        var checkbox = chkBox.getElementsByTagName("input");
        var objTextBox = document.getElementById("<%=txtChkValue.ClientID%>");
        var counter = 0;
        objTextBox.value = "";
        for (var i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
                var chkBoxText = checkbox[i].parentNode.getElementsByTagName('label');
                if (objTextBox.value == "") {
                    objTextBox.value = chkBoxText[0].innerHTML;
                }
                else {
                    objTextBox.value = objTextBox.value + ", " + chkBoxText[0].innerHTML;
                }
            }
        }

    }

where chkList is CheckboxList and txtChkValue is Textbox as given below:

<asp:TextBox ID="txtChkValue1" runat="server" CssClass="textbox"
                Width="270px" Height="25px"></asp:TextBox>
             
                <asp:CheckBoxList ID="chkList1" runat="server" onclick="GetSelectedValue1();">
                </asp:CheckBoxList>

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;

            }


        }

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.";
        }