如何实现编辑主/从记录? [英] How to implement editing Master/Detail records?

查看:129
本文介绍了如何实现编辑主/从记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表。

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

FKrfequestID是一个外键tbl_request。我有Requests.aspx页面,显示tbl_request记录的网格视图。有一个在这个页面中新建项目按钮了。当用户点击这个按钮可以打开RequestInsert.aspx页面。在该页面中用户输入像请求code一些数据和打下一步按钮,去到其中包含一个GridView,显示谁是相关要求的人员Personel.aspx页面。在这个页面的用户应定义谁的人员都涉及到请求。当整个过程完成后用户点击保存按钮。两个表将更新,当用户点击保存按钮。我怎样才能实现Personel.aspx页?

FKrfequestID is a foreign key to tbl_request. I have a grid view in Requests.aspx page that shows tbl_request records. There is a "New Item" button in this page too. When user clicks this button RequestInsert.aspx page opens. In this page user enters some data like RequestCode and hits the "Next" button and goes to Personel.aspx page which contains a gridview that shows the personnel who are related to the request. In this page user should define Personnel who are related to the request. When the whole process finishes user hits "save" button. Both tables will be update when user clicks on "save" button. How can I implement Personel.aspx page?

下面是code为personnelaspx.cs code:

Here is the code for personnelaspx.cs code:

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();
        }
    }
}

我如何能实现编辑模式?在这种模式下我希望用户能够删除工作人员或添加它们?

How can I implement edit mode? In this mode I want users be able to delete personnel or add them?

推荐答案

确定。您需要使用会议存储 tbl_Request.PKRequestID ,它是记录来进行编辑,当用户点击 Edit按钮。这后重新将用户引导到一个新的页面 Edit_Requests_Personnel.aspx 在这里您可以编辑记录。

Ok. You need to use session to store the tbl_Request.PKRequestID of the record that is to be edited when user clicks Edit button. After that re direct the user to a new page Edit_Requests_Personnel.aspx where you can edit the records.

这篇关于如何实现编辑主/从记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆