首页 > 解决方案 > 我希望在文本框中输入 EmpId 的基础上,还应填写该特定 emp 表单中存在的所有其他文本框

问题描述

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

我设计了一个注册表单,其中有以下文本框和 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,然后他必须填写所有文本框。它不会自动更新。当用户单击保存按钮时,数据会在表格中注册,并且清除按钮也会清除所有分机框。

标签: asp.net

解决方案


推荐阅读