选择文本框并更新

本文关键字:更新 文本 选择 | 更新日期: 2023-09-27 18:00:27

我无法在ASP中更新所选文本。NET文本框。我想使用文本框编辑MySql数据库记录。所以,首先我选择这些值并将它们添加到文本框中。在这之后,我试图更新我检索值的同一个文本框。我无法更新那些已编辑的选定值。

当我分别执行选择和更新时,我验证了我的代码单独运行良好。但我不能同时使用两者。

 protected void Page_Load(object sender, EventArgs e) try
    {
        string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=malla_softmail;" + "UID=xxx;" + "PASSWORD=xxx;" + "OPTION=3";
        OdbcConnection MyConnection = new OdbcConnection(MyConString);
        try
        {
            MyConnection.Open();
            OdbcCommand cmd = new OdbcCommand("Select nickname, fullname, sex, age, city, country, company, zipcode, school, college, profession, maritial, local_search, public_search from awm_profiles where email=?", MyConnection);
            cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = a;
            OdbcDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == false)
            {
                throw new Exception();
            }
            if (dr.Read())
            {
                tb_nickname.Text = dr[0].ToString();
                tb_fullname.Text = dr[1].ToString();
                dd_sex.SelectedValue= dr[2].ToString();
                dd_age.SelectedValue= dr[3].ToString();
                tb_city.Text = dr[4].ToString();
                tb_country.Text = dr[5].ToString();
                tb_company.Text = dr[6].ToString();
                tb_zipcode.Text = dr[7].ToString();
                tb_school.Text = dr[8].ToString();
                tb_college.Text = dr[9].ToString();
                tb_profession.Text = dr[10].ToString();
                dd_maritial.SelectedValue = dr[11].ToString();
                String local = dr[12].ToString();
                String web = dr[13].ToString();
                if(Convert.ToInt16(local) == 1)
                {
                lsearch.Checked = true;
                }
                else
                {
                lsearch.Checked = false;
                }
                if(Convert.ToInt16(web) == 1)
                {
                wsearch.Checked = true;
                }
                else
                {
                wsearch.Checked = false;
                }
            MyConnection.Close();
            }
        }
        catch
        {
        }
    }
    catch
    {
    }
}
protected void btn_profile_submit_Click(object sender, EventArgs e)
{
        a = Convert.ToString(Session["emailx10transfer"]);
        lbl_email.Text = a;
        tb_nickname.Text = aa;
        tb_fullname.Text = ab;
        dd_sex.SelectedValue = ac;
        dd_age.SelectedValue = ad;
        tb_city.Text = ae;
        tb_country.Text = af;
        tb_company.Text = ag;
        tb_zipcode.Text = ah;
        tb_school.Text = ai;
        tb_college.Text = aj;
        tb_profession.Text = ak;
        dd_maritial.Text = al;
       // lsearch.Checked = am;
       // wsearch.Checked = an;

   try
    {
        string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=malla_softmail;" + "UID=xxx;" + "PASSWORD=xxx;" + "OPTION=3";
        OdbcConnection MyConnection = new OdbcConnection(MyConString);
       MyConnection.Open();
        OdbcCommand cmd = new OdbcCommand("UPDATE awm_profiles SET nickname=?, fullname=?, sex=?, age=?, city=?, country=?, company=?, zipcode=?, school=?, college=?, profession=?, maritial=? WHERE email=? ", MyConnection);
        cmd.Parameters.Add("@tb_nickname", OdbcType.VarChar, 255).Value = tb_nickname.Text.Trim();
        cmd.Parameters.Add("@tb_fullname", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();
        cmd.Parameters.Add("@dd_sex", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();//dd_sex.SelectedValue.Trim();
        cmd.Parameters.Add("@dd_age", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();//dd_age.SelectedValue.Trim();
        cmd.Parameters.Add("@tb_city", OdbcType.VarChar, 255).Value = tb_city.Text.Trim();
        cmd.Parameters.Add("@tb_country", OdbcType.VarChar, 255).Value = tb_country.Text.Trim();
        cmd.Parameters.Add("@tb_company", OdbcType.VarChar, 255).Value = tb_company.Text.Trim();
        cmd.Parameters.Add("@tb_zipcode", OdbcType.VarChar, 255).Value = tb_zipcode.Text.Trim();
        cmd.Parameters.Add("@tb_school", OdbcType.VarChar, 255).Value = tb_school.Text.Trim();
        cmd.Parameters.Add("@tb_college", OdbcType.VarChar, 255).Value = tb_college.Text.Trim();
        cmd.Parameters.Add("@tb_profession", OdbcType.VarChar, 255).Value = tb_profession.Text.Trim();
        cmd.Parameters.Add("@dd_maritial", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();//dd_maritial.SelectedValue.Trim();
        //if (lsearch.Checked == true) { cmd.Parameters.Add("@localsearch", OdbcType.Int, 11).Value = "1"; }
        //else { cmd.Parameters.Add("@localsearch", OdbcType.Int, 11).Value = "0"; }
        //if (wsearch.Checked == true) { cmd.Parameters.Add("@wwwsearch", OdbcType.Int, 11).Value = "1"; }
        //else { cmd.Parameters.Add("@wwwsearch", OdbcType.Int, 11).Value = "0"; }
        cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = a;
        cmd.ExecuteNonQuery();
        MyConnection.Close();
        Response.Redirect("profile.aspx");
    }
    catch (Exception Ex)
    {
        Response.Write(Ex);
    }   
}

选择文本框并更新

如果您正在Page_Load中从DB加载文本,然后尝试在Postback上保存。如果是,您是否在Page_Load中检查PostBack。如果没有,你可能每次点击按钮都会覆盖你的更改

if (!IsPostBack)
{
    LoadValuesFromDBIntoTextBox();
}

http://msdn.microsoft.com/en-us/library/ms178472.aspx

使用if(!IsPostBack)函数。。。