ASP.NET 中的母版详细信息

本文关键字:详细信息 NET ASP | 更新日期: 2023-09-27 18:35:26

我有两个表。

tbl_Request(PKRequestID, RequestCode) and tbl_Personnel(PKPersonID, PerosnelName FKRequestID)

FKrfequestID 是tbl_request的外键。我在"请求.aspx"页面中有一个网格视图,其中显示了tbl_request记录。此页面中也有一个"新项目"按钮。当用户单击此按钮"请求插入"时.aspx将打开页面。在此页面中,用户输入一些数据,如RequestCode,然后点击"下一步"按钮,然后转到Personel.aspx页面,其中包含一个网格视图,显示与请求相关的人员。在此页中,用户应定义与请求相关的人员。整个过程完成后,用户点击"保存"按钮。当用户单击"保存"按钮时,两个表都将更新。如何实现人员.aspx页面?

ASP.NET 中的母版详细信息

这里有一种方法:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dttbl = new DataTable();
        dttbl.Columns.Add("PKPersonID", System.Type.GetType("System.String"));
        dttbl.Columns.Add("PerosnelName", System.Type.GetType("System.String"));
        dttbl.Columns.Add("FKRequestID", System.Type.GetType("System.String"));
        Session["MyDataTable"] = dttbl;
    }
}
protected void btnok_Click(object sender, EventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    DataRow row1 = t.NewRow();
    row1["PKPersonID"] = txtid.Text ;
    row1["PerosnelName"] = txtname.Text;
    row1["FKRequestID"] = Session["FKRequestID"];
    t.Rows.Add(row1);
    Session["MyDataTable"] = t;
    GridView1.DataSource = t;
    GridView1.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
    {
        DataTable t2 = (DataTable)Session["MyDataTable"];
        SqlConnection con = new SqlConnection("connection_string"
        using (SqlCommand command = con.CreateCommand())
        {
            //Here you are inserting values to tbl_Request
            if (con.State == 0)
                con.Open();
            command.CommandText = @"INSERT INTO tbl_Request (PKRequestID,RequestCode) VALUES (@PKRequestID,@RequestCode)";
            command.Parameters.AddWithValue("@PKRequestID", Session["PKRequestID"]);
            command.Parameters.AddWithValue("@RequestCode", Session["RequestCode"]);
            command.ExecuteNonQuery();
        }
        foreach (DataRow row in t2.Rows)
        {
            //Here you are inserting values to tbl_Personnel
            using (SqlCommand command2 = con.CreateCommand())
            {
                if (con.State == 0)
                    con.Open();
                command2.CommandText = @"INSERT INTO tbl_Personnel (PerosnelName, FKRequestID) VALUES ( @PerosnelName, @FKRequestID)";
                command2.Parameters.AddWithValue("@PerosnelName", txtname.Text);
                command2.Parameters.AddWithValue("@FKRequestID", Session["PKRequestID"]);
                command2.ExecuteNonQuery();
            }
        }
    }