Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I declare the code in console is below .this is used for gridview binding, new row data insert,edit, update, delete.

I need to validate all the footer row textboxs when i click the insert button by using JavaScript.

I dont have any idea in JavaScript.
C#
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["empDBConnectionString"].ConnectionString);

    protected void Page_Load(object sender, System.EventArgs e)
    {
        //Button btnInsert = (Button)GridView1.FooterRow.FindControl("btnInsert");
        //btnInsert.Attributes.Add("onclick", "return validate()");



        if (!IsPostBack)
        {
            GridFill();
        }
         
       
       
    }

    protected void GridFill()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from emp", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();

            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
        catch
        {

        }
    }



    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            try
            {
                TextBox txtname = GridView1.FooterRow.FindControl("txtempname1") as TextBox;
                TextBox txtage = GridView1.FooterRow.FindControl("txtempage1") as TextBox;
                TextBox txtEmail = GridView1.FooterRow.FindControl("txtempemail1") as TextBox;
                DropDownList ddlgender = GridView1.FooterRow.FindControl("ddlempgender1") as DropDownList;
                SqlCommand cmd = new SqlCommand("insert into emp values(@empname,@empage,@empemail,@empgender)", con);
                cmd.Parameters.Add("@empname", SqlDbType.VarChar).Value = txtname.Text;
                cmd.Parameters.Add("@empage", SqlDbType.VarChar).Value = txtage.Text;
                cmd.Parameters.Add("@empemail", SqlDbType.VarChar).Value = txtEmail.Text;
                cmd.Parameters.Add("@empgender", SqlDbType.VarChar).Value = Convert.ToString(ddlgender.Text);
                con.Open();
                cmd.ExecuteNonQuery();

                con.Close();

                GridFill();
            }
            catch
            {
            }
        }




    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //SqlCommand cmd = new SqlCommand("update  emp set empname=@empname,empage=@empage,empemail=@empemail,empgender=@empgender where empid=@empid", con);

        Label lblempid1 = GridView1.Rows[e.RowIndex].FindControl("lblempid") as Label;
        TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("txtempname") as TextBox;
        TextBox txtage = GridView1.Rows[e.RowIndex].FindControl("txtempage") as TextBox;
        TextBox txtEmail = GridView1.Rows[e.RowIndex].FindControl("txtempemail") as TextBox;
        DropDownList ddlgender = GridView1.Rows[e.RowIndex].FindControl("ddlempgender") as DropDownList;
        SqlCommand cmd = new SqlCommand("update  emp set empname='" + txtname.Text + "',empage='" + txtage.Text + "',empemail='" + txtEmail.Text + "',empgender='" + ddlgender.SelectedValue + "' where empid='" + lblempid1.Text + "'", con);
        //cmd.Parameters.Add("@empid", SqlDbType.Int).Value = Convert.ToInt32(lblempid1.Text);
        //cmd.Parameters.Add("@empname", SqlDbType.VarChar).Value = txtname.Text;
        //cmd.Parameters.Add("@empage", SqlDbType.Int).Value = txtage.Text;
        //cmd.Parameters.Add("@empemail", SqlDbType.VarChar).Value = txtEmail.Text;
        //cmd.Parameters.Add("@empgender", SqlDbType.VarChar).Value = ddlgender.SelectedValue;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        GridFill();
        Response.Write("Row Updated");


    }


    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {

        int empid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); 

        SqlCommand cmd = new SqlCommand("DELETE FROM emp WHERE EMPID=" + empid + "", con);
        con.Open();
        int temp = cmd.ExecuteNonQuery();
        if (temp == 1)
        {
            Response.Write("Row Deleted");
        }
        con.Close();
        GridFill();
    }

    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridFill();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
        GridFill();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sValue = ((HiddenField)GridView1.SelectedRow.Cells[0].FindControl("HiddenField1")).Value;
        GridFill();
        Response.Write("Product Id=" + sValue);
    }




    //protected void Button2_Click(object sender, EventArgs e)
    //{
    //    if (!(Session["empid"] == null))
    //    {
    //        lblempid.Text = Session["empid"].ToString();
    //    }
    //         Server.Transfer("empdetails.aspx");
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("empdetails.aspx");
    }
    protected void GridView1_PreRender(object sender, EventArgs e)
    {

        ClientScriptManager cs = Page.ClientScript;



        TextBox lblempid = (TextBox)GridView1.FooterRow.FindControl("lblempid");

        TextBox txtempname = (TextBox)GridView1.FooterRow.FindControl("txtempname1");

        TextBox txtempage = (TextBox)GridView1.FooterRow.FindControl("txtempage1");

        TextBox txtempemail = (TextBox)GridView1.FooterRow.FindControl("txtempemail1");

        cs.RegisterArrayDeclaration("grd_vb", string.Concat("'", lblempid.ClientID, "'"));

        cs.RegisterArrayDeclaration("grdName_Txt", String.Concat("'", txtempname.ClientID, "'"));

        cs.RegisterArrayDeclaration("grdAge_Int", String.Concat("'", txtempage.ClientID, "'"));

        cs.RegisterArrayDeclaration("grdEmail_Txt", String.Concat("'", txtempemail.ClientID, "'"));





    }

    //protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    //{
    //    Response.Write("EmpName:" + e.SortExpression);
    //    //e.SortExpression will provide colomn name selected by user  

    //    SqlDataAdapter da = new SqlDataAdapter("select * from emp orderby empname" + e.SortExpression, con);
    //    DataSet ds = new DataSet();
    //    da.Fill(ds, "emp");
    //    GridView1.DataSource = ds.Tables["emp"];
    //    GridView1.DataBind();
    //}
}

I need to validate all the footer row textbox when i click the insert button using javascript. What could I do. Any one kindly assist me.

Thanks
Posted
Updated 14-Mar-12 1:31am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900