0

我有 2 页,第 1 页包括gridview并且我制作了一个linkbutton将 ID 传递到另一页(第 2 页)的页面。在第 2 页上,我填写了 10 个textboxes,并且我有一个用于编辑信息的按钮。

此代码适用于第 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>

...

这是第 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";
    }

那是行不通的,我试过这个:

        //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;

或这个 :

        //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);

或这个 :

        /*"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*/

但它也不起作用。

4

1 回答 1

1

您需要将初始 Sql 选择移动到 !IsPostback 块中,因为您正在回发更新,但 Page_Load 在文本框更新之前触发。所以一切都在你的初始代码中工作,你只是用初始信息更新它。试试这个:

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

如果你真的想从数据库中重新加载东西,你可以将该查询弹出到一个函数中,然后在按钮单击函数中的按钮单击更新后重新运行它(最后),但实际上没有理由,因为文本框已经有相同的信息。

于 2012-09-30T20:53:16.440 回答