You must Sign In to post a response.
  • Category: JQuery

    How to display data in aspx page when i enter data in my database

    i had three text boxes,a gridview and a button in my aspx page,when i insert/update or any commands that i had entered in my database,it should be display immediately in gridview of my aspx page without page refresh,how can i solve this using jquery get/post methods?
  • #762531
    Hi Jaswanth,

    If you want to achieve this using Jquery then using AJAX Call in JSON you can achieve it, in that you need to pass Page URL and webmethod,


    $.ajax({
    url: url,
    dataType: 'json', //json data type
    data: data,
    success: callback,
    error: callback
    });


    In URL, you need to pass your page and webmethod too.

    Google it the same, you may found lakhs of records out of this.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762536
    Hi

    try this code

    client side


    <script type="text/javascript">
    $(document).ready(function () {
    $("#Btsubmit").click(function () {
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "AjaxGrid.aspx/BindEmployees",
    data: "{}",
    dataType: "json",
    success: function (result) {
    for (var i = 0; i < result.d.length; i++) {
    $("#gvData").append("<tr><td>" + result.d[i].EmpId + "</td><td>" + result.d[i].EmpName + "</td><td>");
    }
    },
    error: function (result) {
    alert("Error");
    }
    });
    });
    });
    </script>

    <asp:TextBox ID="TxtEmpName" runat="server" ></asp:TextBox>
    <asp:Button ID="Btsubmit" runat="server" Text="Click" OnClick="Btsubmit_Click" />


    <asp:GridView ID="gvData" runat="server" CellPadding="4" ShowHeaderWhenEmpty="true" ForeColor="#333333">
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" />
    </asp:GridView>



    Server Side c#



    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindGrid();
    }
    }


    public void BindGrid()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("EmpId");
    dt.Columns.Add("EmpName");

    DataRow dr;
    dr=dt.NewRow();
    dr[0] = 1;
    dr[1] = "John wesly";
    dt.Rows.Add(dr);
    gvData.DataSource = dt;
    gvData.DataBind();
    }
    [WebMethod]
    public static Employee[] BindEmployees()
    {
    string connectionString = "Connection String";
    DataTable dt = new DataTable();
    List<Employee> employeeList = new List<Employee>();
    using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand command = new SqlCommand("select EmpId, EmpName from tblEmp", con))
    {
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(command);
    da.Fill(dt);
    foreach (DataRow dtrow in dt.Rows)
    {
    Employee employee = new Employee();
    employee.EmpId = Convert.ToInt32(dtrow["EmpId"]);
    employee.EmpName = dtrow["EmpName"].ToString();
    employeeList.Add(employee);
    }
    }
    }
    return employeeList.ToArray();
    }
    public class Employee
    {
    public int EmpId { get; set; }
    public string EmpName { get; set; }
    }

    protected void Btsubmit_Click(object sender, EventArgs e)
    {
    string connectionString = "Connection String";
    using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand command = new SqlCommand("Insert into tblEmp values('"+ TxtEmpName.Text +"')", con))
    {
    con.Open();
    command.ExecuteNonQuery();
    BindEmployees();
    }
    }
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments