c#更新控件不会更新绑定源 [英] c# update control does not update binding source

查看:154
本文介绍了c#更新控件不会更新绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我用数据转发器创建了一个表单,在数据转发器中,我有一些标签和文本框.我已将标签和文本框绑定到使用以下代码创建的数据集:

Hello, I created a form with data repeater and in the data repeater I have some labels and textboxes. I have binded the labels and textboxes to a dataset which is created using the following codes:

<pre>private DataSet createDataSet(ProgrammingConfiguration pc)
        {
            ds = new DataSet("Preferences");

            //create a table for the preferences
            DataTable table = new DataTable("Preference");

            //Create table columns
            table.Columns.Add("Tag");
            table.Columns.Add("Precedence");
            table.Columns.Add("DataType");
            table.Columns.Add("Length");
            table.Columns.Add("Value");
            table.Columns.Add("Maximum");
            table.Columns.Add("Minimum");
            table.Columns.Add("OverflowBehaviour");

            //Load object data to dataset
            for (int i = 0; i < pc.Preferences.Length; i++)
            {
                if (pc.Preferences[i].Value.ItemElementName == ItemChoiceType.AutoIncrement)
                {
                    ValueTypeAutoIncrement incrementalData = (ValueTypeAutoIncrement)pc.Preferences[i].Value.Item;
                    table.Rows.Add(pc.Preferences[i].Tag, pc.Preferences[i].Precedence, pc.Preferences[i].Value.ItemElementName, pc.Preferences[i].Value.Length,
                        "", incrementalData.Maximum, incrementalData.Minimum, incrementalData.OverflowBehaviour);
                }
                else
                {
                    table.Rows.Add(pc.Preferences[i].Tag, pc.Preferences[i].Precedence, pc.Preferences[i].Value.ItemElementName, pc.Preferences[i].Value.Length, pc.Preferences[i].Value.Item);
                }
            }

            //Add to dataset
            ds.Tables.Add(table);  
                  
            return ds;
        }




我使用以下方法绑定控件:




I bind the controls with the following methods:

private void bindControls()
       {
           //Define binding source
           bindingsource = new BindingSource();
           bindingsource.DataSource = ds;

           //clear before binding
           TagLabel.DataBindings.Clear();
           PrecedenceLabel.DataBindings.Clear();
           DataTypeLabel.DataBindings.Clear();
           LengthLabel.DataBindings.Clear();
           ValueTextBox.DataBindings.Clear();
           MaxTextBox.DataBindings.Clear();
           MinTextBox.DataBindings.Clear();
           OverflowComboBox.DataBindings.Clear();

           //Bind controls (This has to be done before binding datarepeater)
           TagLabel.DataBindings.Add(new Binding("Text", bindingsource, "Tag"));
           PrecedenceLabel.DataBindings.Add(new Binding("Text", bindingsource, "Precedence"));
           DataTypeLabel.DataBindings.Add(new Binding("Text", bindingsource, "DataType"));
           LengthLabel.DataBindings.Add(new Binding("Text", bindingsource, "Length"));
           ValueTextBox.DataBindings.Add(new Binding("Text", bindingsource, "Value", false, DataSourceUpdateMode.OnPropertyChanged));
           MaxTextBox.DataBindings.Add(new Binding("Text", bindingsource, "Maximum", false, DataSourceUpdateMode.OnValidation));
           MinTextBox.DataBindings.Add(new Binding("Text", bindingsource, "Minimum", false, DataSourceUpdateMode.OnValidation));
           OverflowComboBox.DataBindings.Add(new Binding("SelectedItem", bindingsource, "OverflowBehaviour", false, DataSourceUpdateMode.OnPropertyChanged));


           //Bind data repeater
           dataRepeater.DataSource = bindingsource;
           dataRepeater.DataMember = "Preference";
       }




我想编辑ValueTextBox并更改其值.但是,一旦我离开文本框,该值将被重置回其原始值.我以为数据集未使用新值更新.




I want to edit the ValueTextBox and change its value. However, as soon as I leave the textbox, the value is reset back to its original value. I pressume the dataset is not updated with the new value. Can anyone suggest how to achieve updating the dataset when textbox is updated please?

推荐答案



可能是因为您有PostBack.
尝试使用等于有条件"的更新"模式的更新面板在回发时隐藏面板值.

在您的客户代码中:
Hi,

Maybe because you have PostBack.
Try to use the the update pannel with Update mode equal "Conditional" to hide the pannel value when postback.

In your client code:
   <asp:updatepanel id="panelToUpdate" runat="server" updatemode="Conditional" xmlns:asp="#unknown">
   

</asp:updatepanel>



在您后面的代码中:



In your code behind:

panelToUpdate.Update();



问候



Regards,


这篇关于c#更新控件不会更新绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆