我有两张桌子。
tbl_Request(PKRequestID, RequestCode) and tbl_Personnel(PKPersonID, PerosnelName FKRequestID)
FKrfequestID是tbl_request的外键。 我在Requests.aspx页面中有一个显示tbl_request记录的网格视图。此页面中还有一个“新项目”按钮。当用户单击此按钮时,将打开RequestInsert.aspx页面。在此页面中,用户输入一些数据,如RequestCode,然后点击“下一步”按钮,进入Personel.aspx页面,其中包含一个gridview,显示与请求相关的人员。在此页面中,用户应定义与请求相关的人员。当整个过程结束时,用户点击“保存”按钮。当用户点击“保存”按钮时,两个表都将更新。如何实现Personel.aspx页面?
答案 0 :(得分: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();
}
}
}