Ben
Ben

Reputation: 4319

Why does my text box not recognise the value has changed when I submit?

When my page loads, it queries the database for some values and populates some text boxes with them:

protected void Page_Load(object sender, EventArgs e)
{
    tadbDataContext tadb = new tadbDataContext();
    Dictionary<string, string> hexColors = tadb.msp_silentAuctionColors.ToDictionary(t => t.colorDescription, t => t.colorValue);

    tbTextColor.Text = hexColors["textColor"];
    tbAltColor.Text = hexColors["altColor"];
    tbBackgroundColor.Text = hexColors["backgroundColor"];
}

I then change the value and try to resubmit to the database, by clicking a button which does the following:

using (tadbDataContext tadb = new tadbDataContext())
{
    var textColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "textColor");

    var altColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "altColor");
    var backgroundColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "backgroundColor");

    textColor.colorValue = tbTextColor.Text;
    altColor.colorValue = tbAltColor.Text;
    backgroundColor.colorValue = tbBackgroundColor.Text;

    tadb.SubmitChanges();
}

The value that is posted back is the original (not the changed) value. If I comment out the lines that populate the text boxes on load it works fine.

Upvotes: 4

Views: 177

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460350

That is because you didn't wrap the databinding stuff in a IsPostBack-check in Page_Load. Hence you're always overwriting the changed value with the old from database.

So you simply have to do this:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        tadbDataContext tadb = new tadbDataContext();
        Dictionary<string, string> hexColors = tadb.msp_silentAuctionColors.ToDictionary(t => t.colorDescription, t => t.colorValue);

        tbTextColor.Text = hexColors["textColor"];
        tbAltColor.Text = hexColors["altColor"];
        tbBackgroundColor.Text = hexColors["backgroundColor"];
    }
}

Upvotes: 3

Related Questions