0

I think my problem is quite easy, but I find no solution:

My Question: If I set a field by C#, I modify it in the browser, the value will still be the same.

Example

<input type="text" id="IdText" runat="server" />

Set it with c# (load data into the form, with page.aspx?id=2, get data from DB and set itz to input fields)

IdText.Value = "Example";

Modify it in the browser, I insert "MyExample", and click a button to update the DB And if I will request the Value with c# it's still the same.

string text = IdText.Value;

Here the Value is "Example", and not "MyExample".

Do I have to add some more code?

Thanks for your help.

2
  • 2
    Is there any reason for not using ASP.NET controls like TextBox? Commented Aug 26, 2014 at 8:44
  • 1
    Wrap your pageLoad code inside if(!Page.IsPostback) { // your code } block to avoid this. Commented Aug 26, 2014 at 8:48

2 Answers 2

2

As mentioned by Damien in the comment, you should wrap the code block that sets the Text-property to the value from DB in a !IsPostBack-check. Otherwise you're overwriting the changed value with the old from DB since Page_Load is executed before the button-click event-handler.

For example:

protected void Page_Load(OBject sender, EventArgs e)
{
    if(!IsPostBack)
    {
        IdText.Value = "Example";
    }
}

I would also use ASP.NET-controls if i use ASP.NET, so a TextBox instead of a html-input.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks a lot. I knew the solution is easy.

As I'm using boostrap, I think it is quiet hard to combine bootstrap with the asp.net controls. But you are right - I could use <asp:textbox> insted of <input type="text" />

Thanks Mike

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.