简体   繁体   English

将值从一页传递到另一页并更新

[英]Pass value from one page to another page and update

I have 2 pages, page 1 include the gridview and I made one linkbutton that passes the ID to another page ( page 2). 我有2页,第1页包括gridview ,我做了一个linkbutton ,将ID传递给另一页(第2页)。 On page 2 I fill 10 textbox es and I have one button for edit info. 在第2页上,我填写了10个textbox es,并且有一个按钮用于编辑信息。

This code is for page 1 : 此代码适用于第1页:

...
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lbNextPage" runat="server" 
                    PostBackUrl='<%# "~/secure/upst.aspx?id="+ Eval("ID_st") %>'>edit</asp:LinkButton>
                &nbsp;
            </ItemTemplate>
        </asp:TemplateField>

... ...

and this is the code for page 2: 这是第二页的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtID.Text = Request.QueryString["id"].ToString();
    }
    SqlConnection con = new SqlConnection(strcon);
    string query = "select * from user_st where ID_st = @id";
    SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@id", stid);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();

        txtName.Text = dr["name"].ToString();
        txtFamily.Text = dr["family"].ToString();
        txtAddress.Text = dr["adres"].ToString();
        txtHomeTel.Text = dr["home_tel"].ToString();
        txtTahsilat.Text = dr["tahsilat"].ToString();
        txtTel.Text = dr["celphone"].ToString();
        txtEmail.Text = dr["email"].ToString();
        txtShoghl.Text = dr["shoghl"].ToString();
        txtAge.Text = dr["age"].ToString();
        txtFadername.Text = dr["fader_name"].ToString();
        txtIDnumber.Text = dr["melli_code"].ToString();
        txtShSh.Text = dr["sh_sh"].ToString();

} 

protected void btnOk_Click(object sender, EventArgs e)
{


    Boolean res = false;
    SqlConnection conn = new SqlConnection(strcon);

    string famil = txtFamily.Text;
    string name = txtName.Text;
    string fader = txtFadername.Text;
    string tahsil = txtTahsilat.Text;
    Double telhome = Convert.ToDouble(txtHomeTel.Text);
    string adres = txtAddress.Text;
    Double cel = Convert.ToDouble(txtTel.Text);
    string email = txtEmail.Text;
    Double shsh = Convert.ToDouble(txtIDnumber.Text);
    string shoghl = txtShoghl.Text;
    int age = Convert.ToInt32(txtAge.Text);
    Double melli = Convert.ToDouble(txtIDnumber.Text);
    int id = Convert.ToInt32(txtID.Text);


    string query = "update user_st set name=@name ,fader_name=@fader ,family=@famil,tahsilat=@tahsil,adres=@adres,home_tel=@telhome,celphone=@cel,email=@email ,sh_sh=@shsh,shoghl=@shoghl,age=@age,melli_code=@melli where ID_st=@id";

    SqlCommand cmdup = new SqlCommand(query, conn);
    cmdup.Parameters.AddWithValue("@name",name);
    cmdup.Parameters.AddWithValue("@fader_name",fader );
    cmdup.Parameters.AddWithValue("@family", famil);
    cmdup.Parameters.AddWithValue("@tahsilat",tahsil);
    cmdup.Parameters.AddWithValue("@adres", adres);
    cmdup.Parameters.AddWithValue("home_tel",telhome );
    cmdup.Parameters.AddWithValue("@celphone",cel );
    cmdup.Parameters.AddWithValue("@email", email);
    cmdup.Parameters.AddWithValue("@sh_sh", shsh);
    cmdup.Parameters.AddWithValue("@shoghl", shoghl);
    cmdup.Parameters.AddWithValue("@age",age );
    cmdup.Parameters.AddWithValue("@melli_code", melli);
    cmdup.Parameters.AddWithValue("@id", id);


    try
    {
        conn.Open();
        cmdup.ExecuteNonQuery();
        conn.Close();
        res = true;
    }
    catch (SqlException ex)
    {
        lblRes.Text = "error" + ex.ToString();
    }

    if (res)
    {
        lblResult.Text = "Ok";
    }

That is not working so, I tried this: 那是行不通的,我尝试了这个:

        //cmdup.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = txtName.Text;
    //cmdup.Parameters.Add("@fader_name", SqlDbType.NVarChar, 50).Value = txtFadername.Text;
    //cmdup.Parameters.Add("@family", SqlDbType.NVarChar, 50).Value = txtFamily.Text;
    //cmdup.Parameters.Add("@tahsilat", SqlDbType.NVarChar, 50).Value = txtTahsilat.Text;
    //cmdup.Parameters.Add("@adres", SqlDbType.NVarChar, 150).Value = txtAddress.Text;
    //cmdup.Parameters.Add("home_tel", SqlDbType.Char, 10).Value = txtHomeTel.Text;
    //cmdup.Parameters.Add("@celphone", SqlDbType.Char, 10).Value = txtTel.Text;
    //cmdup.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
    //cmdup.Parameters.Add("@sh_sh", SqlDbType.Char, 10).Value = txtShSh.Text;
    //cmdup.Parameters.Add("@shoghl", SqlDbType.NVarChar, 50).Value = txtShoghl.Text;
    //cmdup.Parameters.Add("@age", SqlDbType.Int).Value = txtAge.Text;
    //cmdup.Parameters.Add("@melli_code", SqlDbType.Char, 10).Value = txtIDnumber.Text;
    //cmdup.Parameters.Add("@id", SqlDbType.Int).Value = txtID.Text;

or this : 或这个 :

        //SqlCommand cmdup = new SqlCommand("EXEC up_st'" + txtName.Text. + "' , '" + txtFamily.Text + "' , '" + txtTahsilat.Text +"' , '" + txtAddress.Text + "' , '" 
    //                                   + txtHomeTel.Text + "' , '" + txtTel.Text + "' , '" + txtEmail.Text + "' , '" + txtShoghl.Text + "' , '"
    //                                   + txtAge.Text + "' , '" + txtFadername.Text + "' , '" + txtIDnumber.Text + "' , '" + txtShSh.Text + "' , '" 
    //                                   + txtID.Text + "'", conn);

or this : 或这个 :

        /*"update user_st set name='" + txtName.Text + "',fader_name='" + txtFadername.Text + "',family='" + txtFamily.Text + "',tahsilat='" + txtTahsilat.Text + "',adres='" + txtAddress.Text + "',home_tel='" + txtHomeTel.Text + "',celphone='" 
        + txtTel.Text + "',email='" + txtEmail.Text + "',sh_sh='" +                      txtShSh.Text + "',shoghl='" + txtShoghl.Text + "',age='" + txtAge.Text + "',melli_code='" + txtIDnumber.Text + "' where ID_st=" + txtID.Text*/

but it also doesn't work. 但它也不起作用。

You need to move your initial Sql select into the !IsPostback block because what's happening is you are posting back your updates, but Page_Load fires before the textboxes are updated. 您需要将初始的Sql选择移到!IsPostback块中,因为发生的是您正在回发更新,但是在更新文本框之前会触发Page_Load。 So everything is working in your initial code, you are just updating it with the initial information. 因此,一切都在您的初始代码中运行,您只是在使用初始信息对其进行更新。 Try this: 尝试这个:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtID.Text = Request.QueryString["id"].ToString();
        SqlConnection con = new SqlConnection(strcon);
        string query = "select * from user_st where ID_st = @id";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@id", stid);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();

        txtName.Text = dr["name"].ToString();
        txtFamily.Text = dr["family"].ToString();
        txtAddress.Text = dr["adres"].ToString();
        txtHomeTel.Text = dr["home_tel"].ToString();
        txtTahsilat.Text = dr["tahsilat"].ToString();
        txtTel.Text = dr["celphone"].ToString();
        txtEmail.Text = dr["email"].ToString();
        txtShoghl.Text = dr["shoghl"].ToString();
        txtAge.Text = dr["age"].ToString();
        txtFadername.Text = dr["fader_name"].ToString();
        txtIDnumber.Text = dr["melli_code"].ToString();
        txtShSh.Text = dr["sh_sh"].ToString();
    }
} 

If you really want to reload the stuff from the database, you can pop that query into a function and re-run it after the button click update in the button click function (at the end), but there really is no reason since the textboxes will already have the same info. 如果您确实要从数据库中重新加载内容,则可以将该查询弹出到一个函数中,并在按钮单击功能中的按钮单击更新后(最后)重新运行该查询,但是实际上没有理由,因为文本框将已经有相同的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM