简体   繁体   English

在编辑配置文件页面中提交表单时,文本框值保持不变

[英]textbox value remains unchanged on form submission in edit profile page

i am trying to create a edit profile page so when the page loads first i am filling all text boxes values as the value present in the database for the particular user.i am doing this using the code: 我正在尝试创建一个编辑配置文件页面,因此当页面首次加载时,我将所有文本框值填充为特定用户在数据库中存在的值。我正在使用代码进行此操作:

        DBconn obj1 = new DBconn();
        String sql1 = "select* from users where user_id='" + Session["user_id"].ToString() + "'";
        DataTable dt = new DataTable();
        dt = obj1.getdatatable(sql1);
        if (dt.Rows.Count > 0)
        {
            referal_id.Text = dt.Rows[0]["referal_id"].ToString();
            name.Text = dt.Rows[0]["name"].ToString();
            password.Text = dt.Rows[0]["password"].ToString();
            email.Text = dt.Rows[0]["email"].ToString();
            mobile.Text = dt.Rows[0]["mobile"].ToString();
            city.Text = dt.Rows[0]["city"].ToString();
            state.Text = dt.Rows[0]["state"].ToString();
            pincode.Text = dt.Rows[0]["pincode"].ToString();
            sec_ques.Text = dt.Rows[0]["securityq"].ToString();
            answer.Text = dt.Rows[0]["answer"].ToString();
            age.Text = dt.Rows[0]["age"].ToString();
            gender.Text = dt.Rows[0]["gender"].ToString();
            user_id.Text = dt.Rows[0]["user_id"].ToString();
            address.Text = dt.Rows[0]["address"].ToString();
            date_of_joining.Text = dt.Rows[0]["date_of_joining"].ToString();
            user_type.Text = dt.Rows[0]["user_type"].ToString();
        }

now this is filling all the text boxes with the values . 现在,这将使用值填充所有文本框。 when user edits some values in some textbox and resubmits the form the database is getting updated implementing the code: 当用户在某些文本框中编辑某些值并重新提交数据库以实现代码更新时:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    int i;
    DBconn obj2 = new DBconn();
    String sql2 = "update users set name='"+name.Text+"',address='"+address.Text+"',age='"+age.Text+"',gender='"+gender.Text+"',email='"+email.Text+"',mobile='"+mobile.Text+"',securityq='"+sec_ques.Text+"',answer='"+answer.Text+"',city='"+city.Text+"',state='"+state.Text+"',pincode='"+pincode.Text+"' where user_id='"+Session["user_id"].ToString()+"'";
    i = obj2.executeDML(sql2);
    if (i > 0)
    {
        Label1.Visible = true;
        Image2.Visible = true;
        Label1.Text = "Updated successfully!";

    }
    else
    {
        Label1.Visible = true;

        Label1.Text = "Oops Update was unsuccessfull!";
    }

}

the problem is the database is getting updated with the previous values which was already present in the database.when i used watchpoints i found the sql statement gets loaded with the textbox values which i binded using the above database code but its not fetching the values which the user edits. 问题是数据库正在使用数据库中已经存在的先前值进行更新。当我使用观察点时,我发现sql语句加载了文本框值,该文本框值是使用上述数据库代码绑定的,但未获取其中的值用户进行编辑。

plz help. 请帮助。

Hey Robin, Please check whether the code is placed under the !IsPostback condition or not. 嘿罗宾,请检查代码是否放在!IsPostback条件下。

Please bind the values under this if loop. 请在此if循环下绑定值。

    if(!IsPostback)
    {

    // Bind values

    }

do this on the page load. 在页面加载上执行此操作。

The values must only be bound for the first time the page is load. 只能在第一次加载页面时绑定值。

In your case it seems the values to the text boxes are bound for each and every time it is loaded(even in the postback also the values are loaded.) 在您的情况下,似乎每次加载文本框的值都是绑定的(即使在回发中也加载值)。

So, in page load you must check the condition whether the page is postback or not. 因此,在页面加载中,您必须检查条件页面是否为回发。

Thus add this condition while binding the values in page load. 因此,在绑定页面加载中的值时添加此条件。

 if(!IsPostback)      

Firstly check the location from where ur calling the mathod for binding values... 首先检查您从哪里调用绑定绑定值的方法的位置...

put it on 穿上

Page.isPostback Page.isPostback

so it will not change the value at posting time then when u will save it the value will change 因此它不会在发布时更改值,那么当您保存它时,值将更改

the value must be changing when u click on button and page is again loading. 当您单击按钮并再次加载页面时,值必须更改。

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

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