简体   繁体   中英

ASP.NET - Controls submit wrong information

in ASP.NET I have 2 text boxes, one dropdownlist and a button that submit the wrong information to a method that I am calling.

I am using Linq to SQL with Entity Framework.

What I am trying to do is updating a ticket in the database that contains a subject, content and isPublic(bool). When I load the page it automatically fills the text boxes and combo boxes with the fields from ticket where the ticket id is known. So if ticket 2 is clicked,the page loads and the controls get filled with the information from that ticket. So if ticket 2 has a subject Dogs, content a Dog is an animal and isPublic is true, you'll get to see that information in those boxes. Now, when I change the content of those controls and press the button, it loads a method that I wrote to update the database:

    public void updateTicket(int _ticket_id,bool _isManager,string _subject,bool _isPublic,string _description)
    {
        TICKET ticket = (from t in db.TICKETs
                         where t.id == _ticket_id
                         select t).First();

        REACTION reaction = (from i in db.REACTIONs
                             where i.tickets_id == _ticket_id
                             select i).FirstOrDefault();

        ticket.subject = _subject;
        ticket.isPublic = _isPublic;
        reaction.contents = _description;

        db.SubmitChanges();
}

And I call this function like this:

instanceOfTickets.updateTicket(2, false, TextBox1.Text,waarde,TextBox2.Text);

Where ticket_id is 2 just for testing and waarde is Public. The problem that I have is when I debug this method, the controls return the wrong information to the method overload. Instead for example cat, it still sends dog to my method. Even though I really filled in cat before clicking the button.

Does anyone know why it does this and how to fix this? Thanks in advance.

EDIT:

To be more clear:

After loading the page:

在此处输入图片说明

Here I changed the content:

在此处输入图片说明

After I pressed the button and started to debug:

在此处输入图片说明

EDIT 2:

So I figured out what causes the problem but I don't know how to fix it. I have a var ticket that contains the ticket after I used a linq query to get that ticket from the database. When I tell a text box to get fill itself with the subject (or content) from that ticket, it keeps somehow "connected" to that. So when I say textbox1.text = ticket.subject , the subject will show up in that text box but it will always stay like that. How do I "overwrite" that so that I can send the proper information to my method?

Looks like you fill your data on every postback in Page_Load without checking Page.IsPostBack. So after you submit your form it primarily fills textboxes from db and after that it tries to update data. But textboxes already contain original values from DB. small example of initial data set

public void Page_Load(object sender, EventArgs e) 
{
  if (!IsPostBack)
  {
    var myObj = GetdataFromDb();
    TextBox1.Text = myObj.Subject;
    TextBox2.Text = myObj.Desctiption;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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