我想在文本框中输入EmpId的基础上,还应填写该特定emp形式的所有其他文本框

时间:2019-04-03 11:59:57

标签: asp.net

我希望当用户自动输入emp id时(根据用户填写的特定id),该特定员工的所有其他信息应填写在表格中,并应保持禁用状态,直到emp按下编辑按钮为止。 / p>

我设计了一个注册表,其中有以下文本框和3个按钮 emp ID 国籍 宗教 性别

保存按钮编辑按钮清除按钮

我已经实现了保存按钮和清除按钮的编码。填写表单后,当用户单击“保存”按钮时,数据将存储在数据库表中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    SqlConnection con= new SqlConnection();
    SqlConnection conn = new SqlConnection();
    SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = @"Data Source=SAKSHIKAUL-LT\SQLEXORESS1;Initial Catalog=registrationForm;Integrated Security=True";
        conn.ConnectionString = @"Data Source=SAKSHIKAUL-LT\SQLEXORESS1;Initial Catalog=registrationForm;Integrated Security=True";
        con.Open();
        conn.Open();
        {
            string FetchData = "Select * from EmpDetails where EmpId=''";
            cmd = new SqlCommand(FetchData, conn);
            dr = cmd.ExecuteReader();
            if (!this.IsPostBack)
                Label1.Text = "";
        }

        if (dr.Read())
        {
            TextBox1.Text += dr[0].ToString() + " ";
            TextBox2.Text += dr[1].ToString() + " ";
            TextBox3.Text += dr[2].ToString() + " ";
            TextBox4.Text += dr[3].ToString() + " ";
            TextBox5.Text += dr[4].ToString() + " ";
            TextBox6.Text += dr[5].ToString() + " ";
            TextBox7.Text += dr[6].ToString() + " ";
            conn.Close();
            {

            }
        }
    }        

    protected void Button1_Click(object sender, EventArgs e)
    { SqlCommand cmd = new SqlCommand("insert into RegistrationTable" + "(EmpId,Nationality,Religion,Gender,Address,EmailId,Hobbies,MaritalStatus) values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@Hobbies,@MaritalStatus)", con);

        //string insertQuery = "insert into RegistrationTable(EmpId,Nationality,Religion,Gender,Address,EmailId,Hobbies,Maritalstatus)values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@MHobbies,@Maritalstatus)";

            cmd.Parameters.AddWithValue("@EmpId", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Nationality", TextBox2.Text);
            cmd.Parameters.AddWithValue("@Religion", TextBox3.Text);
            cmd.Parameters.AddWithValue("@Gender", TextBox4.Text);
            cmd.Parameters.AddWithValue("@Address", TextBox5.Text);
            cmd.Parameters.AddWithValue("@EmailId", TextBox6.Text);
            cmd.Parameters.AddWithValue("@Hobbies", TextBox7.Text);
            cmd.Parameters.AddWithValue("@Maritalstatus", TextBox8.Text);
            cmd.ExecuteNonQuery();
            Label1.Text = "Registered Successfuly";


    }

    protected void Button2_Click(object sender, EventArgs e)
    { cmd.CommandText = "update RegistrationTable set Nationality =' " + TextBox2.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Religion =' " + TextBox3.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Gender =' " + TextBox4.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Address =' " + TextBox5.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set EmailId =' " + TextBox6.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set Hobbies =' " + TextBox7.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.CommandText = "update RegistrationTable set MaritalStatus =' " + TextBox8.Text + "' where EmpId='" + TextBox1.Text + "' ";
cmd.Connection = con;
cmd.ExecuteNonQuery();
Label1.Text = "Updated Successfuly";
        }





    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Text = ""; 
        TextBox2.Text = ""; 
        TextBox3.Text = ""; 
        TextBox4.Text = ""; 
        TextBox5.Text = ""; 
        TextBox6.Text = ""; 
        TextBox7.Text = ""; 
        TextBox8.Text = ""; 

        Label1.Text = "Clear All";
    }

    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    protected void TextBox1_TextChanged1(object sender, EventArgs e)
    {

    }
}

预期的输出-当用户输入EmpId时,表单中存在的所有其他文本框应自动填充(从现有数据库中填充),并应保持禁用状态,直到用户单击编辑按钮为止

实际结果-用户输入emp id,然后他必须填写所有文本框。它不会自动更新。当用户单击“保存”按钮时,数据将注册到表中,而“清除”按钮也会清除所有外部框。

0 个答案:

没有答案