Friday 28 February 2014

Textbox string empty, caldander string empty after button click

<table width="450px" border="0" cellspacing="0" cellpadding="3" align="center">
          <tr>
            <td valign="top" class="text" align="center" >
                <asp:Label ID="Label1" runat="server" Text="empname"></asp:Label>
       </td>
            <td valign="top" class="text" align="center">
                <asp:TextBox ID="txtempname" runat="server" Height="22px"></asp:TextBox>
       </td>
     
          </tr>
          <tr>
            <td valign="top" class="text" align="center">
                <asp:Label ID="Label2" runat="server" Text="eno"></asp:Label>
       </td>
            <td valign="top" class="text" align="center">
                <asp:TextBox ID="txteno" runat="server"></asp:TextBox>
       </td>
     
          </tr>  
           <tr>
            <td valign="top" class="text" align="center">
                <asp:Label ID="Label3" runat="server" Text="startdate"></asp:Label>
       </td>
            <td valign="top" class="text" align="center">
                <asp:Calendar ID="empcalstartdate" runat="server"></asp:Calendar>
               
             
       </td>
     
          </tr>
           <tr>
            <td valign="top" class="text" align="center">
                &nbsp;</td>
            <td valign="top" class="text" align="center">
                &nbsp;</td>
     
          </tr>
           <tr>
            <td valign="top" class="text" align="center">
                <asp:Label ID="lblds" runat="server" Text="Your details submitted"
                    Visible="False"></asp:Label>
               </td>
            <td valign="top" class="text" align="center">
                <asp:Button ID="Submit" runat="server" BorderStyle="Solid" Text="Submit"
                    onclick="Submit_Click" />
                <asp:Button ID="btngraph" runat="server" BorderStyle="Solid" Text="Display graph"
                    onclick="Submit_Click"  Visible="False" />
       </td>
     
          </tr>          
        </table>  













protected void Submit_Click(object sender, EventArgs e)
        {
           

            //string empty1 = string.Empty;
            //empty1 = txtempname.Text;

            //string empty2 = string.Empty;
            //empty1 = txteno.Text;

            //string empty3 = string.Empty;
            //empty1 = empcalstartdate.SelectedDate.ToString();

            if (txtempname.Text.Length == 0 || txteno.Text.Length == 0)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please type your User ID and Password correctly and click Submit button again.  Thanks", true);

            }

            else
            {

                SPSite site = SPContext.Current.Site;
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Employeeinformations"];
                web.AllowUnsafeUpdates = true;
                SPListItemCollection coll = list.Items;
                SPListItem item = coll.Add();
                item["empname"] = txtempname.Text;

                // string txtnostring = txteno.Text;
                //int m = Int32.Parse("txtno");

                item["eno"] = txteno.Text;

                item["empjoineddate"] = empcalstartdate.SelectedDate;
                web.AllowUnsafeUpdates = false;
                lblds.Visible = true;
                item.Update();
                list.Update();


                lblds.Text = string.Empty;
                txtempname.Text = string.Empty;
                txteno.Text = string.Empty;

                btngraph.Visible = true;

                string seldt = empcalstartdate.SelectedDate.ToString();
                seldt = string.Empty;



            }  
    }

Wednesday 26 February 2014

Gridview Sorting

<asp:GridView ID="gvDetails" runat="server"
              AutoGenerateColumns="False"  AllowPaging="true"
              onsorting="gvDetails_Sorting" PageSize="6" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="EmployeeName" SortExpression="EmployeeName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("EmployeeName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("Mobile")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BatchCode" SortExpression="BatchCode">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("BatchCode")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created" SortExpression="Created">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Created")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>





 protected void Page_Load(object sender, EventArgs e)
        {
            gvDetails.DataSource = BindGridView();
            gvDetails.DataBind();
        }



 protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortingDirection = string.Empty;
            if (sortProperty == SortDirection.Ascending)
            {
                sortProperty = SortDirection.Descending;
                sortingDirection = "Desc";
            }
            else
            {
                sortProperty = SortDirection.Ascending;
                sortingDirection = "Asc";
            }

            DataView sortedView = new DataView(BindGridView());
            sortedView.Sort = e.SortExpression + " " + sortingDirection;
            gvDetails.DataSource = sortedView;
            gvDetails.DataBind();
        }


     private DataTable BindGridView()
        {
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["Employee Feedback Details"];
            SPListItemCollection item = list.GetItems();


            //foreach (SPListItem items in item) ;
            //{

            //}
            DataTable dt = new DataTable();
            dt = item.GetDataTable();

            return dt;
            //gvDetails.DataSource = dt;
            //gvDetails.DataBind();
        }



        public SortDirection sortProperty
        {
            get
            {
                if (ViewState["SortingState"] == null)
                {
                    ViewState["SortingState"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortingState"];
            }
            set
            {
                ViewState["SortingState"] = value;
            }
        }