1

我有两张桌子。

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

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

4

1 回答 1

1

这是一种方法:

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();
            }
        }
    }
于 2012-05-25T18:58:44.913 回答