2012-09-07 45 views
4

当我的页面加载时,将查询一些值的数据库,并填充一些文本框与他们:为什么我提交时我的文本框无法识别值已更改?

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

我然后更改值,并尝试重新提交到数据库中,通过点击一个按钮,执行以下操作:

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(); 
} 

回传的值是原始值(而非更改的值)。如果我注释出装入文本框的行,它可以正常工作。

回答

3

这是因为您没有将数据绑定的东西包装在IsPostBack中,请检查Page_Load。因此,你总是用来自数据库的旧数据覆盖已更改的值。

所以你只需要做到这一点:

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

传奇 - 彻底忘了! – Ben

相关问题