文本框不反映所做的更改 [英] text box does not reflect changes made

查看:67
本文介绍了文本框不反映所做的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我有一个页面,其目的是允许用户编辑他/她的个人详细信息。为此,首先我在Page_load函数中提取详细信息,然后有一个按钮,单击该按钮会推送对数据库所做的更改。

但是,更改不会回发到数据库中某些原因 - 意味着不会回发更改:我提取的ORIGINAL数据也仅用于更新目的。代码如下。请帮助......似乎无法弄清楚我哪里出错了..谢谢!





功能1 - Page_Load:



Hi!
I have a page whose purpose is to allow the User to edit his/her personal details. For that, first I pull the details in the Page_load function and then there is a button, which when clicked pushes the changes made to the database.
However, the changes are NOT posted back to the database for some reason - meaning that the changes are not posted back : the ORIGINAL data that I extracted is ONLY being used for the updating purpose as well. The code is as below. Please Help ... can''t seem to figure out where I''m going wrong .. Thanks!


Function 1 - Page_Load :

protected void Page_Load(object sender, EventArgs e)
        {
            string str = "<MARQUEE> NOTIFICATIONS </MARQUEE>";
            Literal1.Text = str;

            using (SqlConnection con1 = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
            {
                string strValue = Session["User"].ToString();

                using (var connection = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT empFname,empLname,pwd,empBday,empTitle,city,empEmail,empAddr,empMobNo,empLocNo,empLoginId FROM [emp] WHERE ( [empLoginId] = @empLogin ) ";
                        command.Parameters.AddWithValue("@empLogin", strValue);
                        connection.Open();
                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                TextBox1.Text = reader["empFname"].ToString().ToUpper().Trim();
                                TextBox2.Text = reader["empLname"].ToString().ToUpper().Trim();
                                TextBox3.Text = reader["empLoginId"].ToString();
                                TextBox4.Text = reader["pwd"].ToString();
                                TextBox5.Text = reader["empBday"].ToString().ToUpper().Trim();
                                TextBox6.Text = reader["empTitle"].ToString().ToUpper().Trim();
                                TextBox7.Text = reader["city"].ToString().ToUpper().Trim();
                                TextBox8.Text = reader["empEmail"].ToString();
                                TextBox11.Text = reader["empAddr"].ToString().ToUpper().Trim();
                                TextBox9.Text = reader["empMobNo"].ToString().ToUpper().Trim();
                                TextBox10.Text = reader["empLocNo"].ToString().ToUpper().Trim();
                            }
                        }
                        connection.Close();
                    }
                }
            }
        }





功能2 - Button1_Click:





Function 2 - Button1_Click :

protected void Button1_Click(object sender, EventArgs e)
        {
            string pwdChange = TextBox4.Text.ToString().Trim();
            string bdayChange = TextBox5.Text.ToString() ;
            string mailChange = TextBox8.Text.ToString();
            string addrChange = TextBox11.Text.ToString().ToUpper().Trim();
            string mobChange = TextBox9.Text.ToString();
            string locChange = TextBox10.Text.ToString();
            string strValue = Session["User"].ToString();

                using (SqlConnection connection = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand("UPDATE emp set pwd = @pwdChan, empBday = @bdayChan, empEmail = @mailChan, empAddr = @addrChan, empMobNo = @mobChan, empLocNo = @locChan  where empLoginId = @empLogin", connection))
                    {
                        command.Parameters.AddWithValue("@pwdChan", pwdChange);
                        command.Parameters.AddWithValue("@bdayChan", bdayChange);
                        command.Parameters.AddWithValue("@mailChan", mailChange);
                        command.Parameters.AddWithValue("@addrChan", addrChange);
                        command.Parameters.AddWithValue("@mobChan", mobChange);
                        command.Parameters.AddWithValue("@locChan", locChange);
                        command.Parameters.AddWithValue("@empLogin",  strValue);
                        if (command.ExecuteNonQuery()>0)
                        {
                            Label5.Text="success" + pwdChange;
                        }


                    }
                    connection.Close();
                }
        }





用户的密码最初为vd。将pwd文本框更改为''darkd''并单击Submit按钮后,label5显示''successvd'' - 这意味着从数据库表中获取的数据仅被再次使用!



The pwd for the user is ''vd'' originally. After changing the pwd text box to ''darkd'' and clicking the Submit Button, the label5 displays ''successvd'' - which implies that the data taken from the the database table is being used again only!

推荐答案

您好,



您必须将整个page_load代码包装在IsPostback中

例如

Hello,

You have to wrap your entire page_load code inside IsPostback
e.g.
protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {    
            string str = "<MARQUEE> NOTIFICATIONS </MARQUEE>";
            ......
            ......
     }    
}



谢谢,
Imdadhusen


Thanks,
Imdadhusen


这篇关于文本框不反映所做的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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