加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 当我提交时,为什么我的文本框无法识别值已更改?

发布时间:2020-12-16 09:51:58 所属栏目:asp.Net 来源:网络整理
导读:当我的页面加载时,它会在数据库中查询某些值并用它们填充一些文本框: protected void Page_Load(object sender,EventArgs e){ tadbDataContext tadb = new tadbDataContext(); Dictionarystring,string hexColors = tadb.msp_silentAuctionColors.ToDiction
当我的页面加载时,它会在数据库中查询某些值并用它们填充一些文本框:

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

回发的值是原始(不是更改的)值.如果我注释掉在加载时填充文本框的行,它可以正常工作.

解决方法

那是因为你没有将数据绑定内容包装在Page_Load中的IsPostBack-check中.因此,您始终使用旧数据库覆盖已更改的值.

所以你只需要这样做:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读