Wednesday, 25 February 2015

To get TextBox value without Master Page
var firstName = $('#txtFirstName').val()

To get TextBox value with MasterPage for TextBox
var subject = $(GetClientID('CallsSubjectTxt')).val();

To get drop down Text with Master Page
var callType = $(GetClientID('CallTypeDrp')).find('option:selected').text();

To get drop down Text without Master Page
var countryText = $('#DropDownList1').find('option:selected').text()

To get drop down Id without Master Page
var callTypeID = $('#CallTypeDrp')).val()

To get drop down Id with Master Page
var callTypeID = $(GetClientID('CallTypeDrp')).val()


To check checkbox validation with Master Page
 if ($(GetClientID('RemainderChk')).is(':checked'))
             RemainderCheck = 1;
         else
             RemainderCheck = 0;

To check checkbox validation without Master Page
if ($('#RemainderChk')).is(':checked'))
             RemainderCheck = 1;
         else
             RemainderCheck = 0;


To find out all checked check boxes

$("input[type='checkbox']:checked").each(
    function() {
       // Your code goes here...
    }
);



var isChecked = $('#chkSelectBox').attr('checked')?true:false;
Here if check box is checked it returns true, else undefined




var isChecked = $('#chkSelectBox').prop('checked');

prop() will return true or false 





Label Validations:



<head runat="server">
    <title></title>
    <script src="jquery-1.7.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function () {
                var lablevalue = $('#Label1').text();
                alert('Label Value ' + lablevalue);
            });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="divClassusingID" style="width:400px"> 
        <asp:Label ID="Label1" runat="server" Text="hi uday"></asp:Label>

    </div>
    </form>
</body>



output:




To get label value with master page

$('#<%=Label1.ClientID%>').text();


To get label value without master page
$('#Label1').text();

-----------------------------------------------------------------------------------------

Set label value with master page
$('#<%=Label1.ClientID%>').text("New Value");


Set label value without master page

$('#Label1').text("dotnetbyudayrajakonda.blogspot.in");


Example:


<head runat="server">
    <title></title>
    <script src="jquery-1.7.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function () {
                var lablevalue= $('#Label1').text("dotnetbyudayrajakonda.blogspot.in");
            });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="divClassusingID" style="width:400px"> 
        <asp:Label ID="Label1" runat="server" Text="hi uday"></asp:Label>
    </div>
    </form>
</body>





Output:





Set Textbox value with Master Page
$('#<%=TextBox1.ClientID%>').val("New Value");

Set Textbox value without Master Page
$('#TextBox1').val("HI uday");


<head runat="server">
    <title></title>
    <script src="jquery-1.7.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function () {
                var a=$('#TextBox1').val("HI uday");
            });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="divClassusingID" style="width:400px"> 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>



Output:

Set Dropdown value with Master Page
$('#<%=DropDownList1.ClientID%>').val("hi");

Set Dropdown value without Master Page
$('#DropDownList1').val("hi");

Get text of selected item in dropdown with Master Page
$('#<%=DropDownList1.ClientID%> option:selected').text();

Get text of selected item in dropdown without Master Page
$('#DropDownList1 option:selected').text()

Make textbox as Read only
$('#TextBox1').attr('readonly', 'readonly');


<head runat="server">
    <title></title>
    <script src="jquery-1.7.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function () {
                $('#TextBox1').attr('readonly', 'readonly');
            });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="divClassusingID" style="width:400px"> 
        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    </div>
    </form>
</body>


To disable texbox



Using check box
$('#CheckBox1').attr('checked');





output:



Example:



Tuesday, 24 February 2015

Edit and delete in grid view code:




Login.aspx.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Factory;
using BAL;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page
{
    string str = null;
  public static  string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["Sqlconnection"].ToString();
    SqlConnection con = new SqlConnection(sqlc);
    EmpBO bo = new EmpBO();
    EmpBal bl = new EmpBal();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            display();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            bo.Firstname = TextBox1.Text;
            bo.Lastname = TextBox2.Text;
            bo.Empid = Convert.ToInt32(TextBox3.Text);
            bo.Dept = DropDownList1.SelectedItem.Text;
            bo.Salary = Convert.ToInt32(TextBox5.Text);
            bo.Gender = RadioButtonList1.SelectedItem.Text;
            for (int n = 0; n < CheckBoxList1.Items.Count; n++)
            {
                if (CheckBoxList1.Items[n].Selected)
                {
                    if (str == null)
                        str += CheckBoxList1.Items[n].Text;
                    else
                        str += ", " + CheckBoxList1.Items[n].Text;
                }
            }
            bo.Hobbies = str;
            ds = bl.passto(bo);
            display();
           CleartextBoxes(this);
           DropDownList1.SelectedIndex = 0;
         
        }
        catch (Exception)
        {
            throw;
        }

    }
    public void display()
    {
        try
        {
            SqlConnection con = new SqlConnection(sqlc);
            SqlCommand cmd = new SqlCommand("Select * from emp", con);
            SqlDataAdapter sd = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sd.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception)
        {

            throw;
        }
    }

public void CleartextBoxes(Control parent)
    {

        foreach (Control x in parent.Controls)
        {
            if ((x.GetType() == typeof(TextBox)))
            {

                ((TextBox)(x)).Text = "";
            }

            if (x.HasControls())
            {
                CleartextBoxes(x);
            }
        }
    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    Label lblID = (Label)row.FindControl("lblID");
 
    TextBox texte = (TextBox)row.Cells[0].Controls[0];
    TextBox textf = (TextBox)row.Cells[1].Controls[0];
    TextBox textl = (TextBox)row.Cells[2].Controls[0];
    TextBox textd = (TextBox)row.Cells[3].Controls[0];
    TextBox texts = (TextBox)row.Cells[4].Controls[0];
    TextBox textg = (TextBox)row.Cells[5].Controls[0];
    TextBox texth = (TextBox)row.Cells[6].Controls[0];


       
   
    GridView1.EditIndex = -1;
    con.Open();

    SqlCommand cmd = new SqlCommand("update emp set Firstname='" + textf.Text + "',Lastname='" + textl.Text + "',Dept='" + textd.Text + "' ,Salary=" + texts.Text + ",Gender='" + textg.Text + "' ,Hobbies='" + texth.Text + "' where Empid=" + userid + "", con);
    cmd.ExecuteNonQuery();
    con.Close();
    display();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    display();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    display();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    display();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    Label lbldeleteid = (Label)row.FindControl("lblID");
    con.Open();
    SqlCommand cmd = new SqlCommand("delete FROM emp where Empid='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
    cmd.ExecuteNonQuery();
    con.Close();
    display();
}

   
}
Design:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Firstname"></asp:Label>
                    </td>

                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                    <td>
                       
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Lastname"></asp:Label>
                    </td>

                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                    <td>
                       
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Empid"></asp:Label>
                    </td>

                    <td>
                        <asp:TextBox ID="TextBox3" runat="server" MaxLength="3"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Text="Dept"></asp:Label>
                    </td>

                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>select</asp:ListItem>
                            <asp:ListItem>CSE</asp:ListItem>
                            <asp:ListItem>ECE</asp:ListItem>
                            <asp:ListItem>MECH</asp:ListItem>
                            <asp:ListItem>Testing</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label5" runat="server" Text="Salary"></asp:Label>
                    </td>

                    <td>
                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label6" runat="server" Text="Gender"></asp:Label>
                    </td>
                    <td>
                        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                            <asp:ListItem>Male</asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label7" runat="server" Text="Hobbies"></asp:Label>
                    </td>
                    <td>
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                            <asp:ListItem>PlayingCricket</asp:ListItem>
                            <asp:ListItem>Reading</asp:ListItem>
                            <asp:ListItem>Dancing</asp:ListItem>
                        </asp:CheckBoxList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
                    </td>
                </tr>
            </table>
        </div>
        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4" DataKeyNames="Empid"
            AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
             OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BorderStyle="None">
            <Columns>
                <asp:BoundField DataField="Empid" HeaderText="Empid" />
                <asp:BoundField DataField="Firstname" HeaderText="Firstname." />
                <asp:BoundField DataField="Lastname" HeaderText="Lastname" />
                <asp:BoundField DataField="Dept" HeaderText="Dept" />
                <asp:BoundField DataField="Salary" HeaderText="Salary" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" />
                <asp:BoundField DataField="Hobbies" HeaderText="Hobbies" />
                <asp:CommandField ShowEditButton="true" HeaderText="Edit" />
                <asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
            </Columns>

            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <SortedAscendingCellStyle BackColor="#FEFCEB" />
            <SortedAscendingHeaderStyle BackColor="#AF0101" />
            <SortedDescendingCellStyle BackColor="#F6F0C0" />
            <SortedDescendingHeaderStyle BackColor="#7E0000" />

        </asp:GridView>


    </form>
</body>
</html>