Why doesn't my text box recognize the value when I submit?

When my page loads, it queries the database for some values โ€‹โ€‹and populates them with text fields:

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"]; } 

Then I change the value and try to resubmit it to the database by clicking a button that 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 sent back is the original (not changed) value. If I comment out the lines filling the text fields at boot time, it works fine.

+4
source share
1 answer

This is because you did not IsPostBack data binding material in IsPostBack -check in Page_Load . Therefore, you always overwrite the changed value with the old from the database.

So you just 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"]; } } 
+3
source

Source: https://habr.com/ru/post/1433074/


All Articles