我使用 ASP Web 表单和 C#。我使用 UserControl,所以我在代码隐藏中以编程方式创建控件。
我有一个列表视图,当我选择一个项目时,一个函数会填充我的控件(标签和文本框)以显示我所选项目的详细信息。我有一个阅读模式(标签)和一个编辑模式(文本框)。我必须通过阅读模式才能访问编辑模式。
我有两种情况:
- 当我创建一个新项目并从容地展示它的细节时
- 当我编辑一个项目时,设置新值并大步显示其详细信息
我看到使用 this.Controls.Add() 在我的 UserControl 中添加控件时设置了重复值。第一次咨询后问题消失。
例如,如果我为我的文本框设置了一个值,它会在以下控件中重复。如果我为两个第一个文本框设置值,则第一个值在第二个中重复,第二个值在第三个中重复。
我检查没有控件具有相同的 ID。当我使用断点并添加手表时,我可以看到我的功能很好,并且在 this.Controls.Add() 期间设置了重复值。
这是我的代码:
public class GetSetConfig : UserControl, IConfig
{
#region Composants
// Composants pour le mode edit
TextBox tUrl = new TextBox();
RegularExpressionValidator valUrl = new RegularExpressionValidator();
TextBox tHttpResp = new TextBox();
TextBox tCredentials = new TextBox();
TextBox tExpectedResult = new TextBox();
TextBox tMaxExecTime = new TextBox();
CompareValidator valMaxExecTime = new CompareValidator();
TextBox tMaxDelay = new TextBox();
CompareValidator valMaxDelay = new CompareValidator();
TextBox tTimeout = new TextBox();
CompareValidator valTimeout = new CompareValidator();
// Composants pour le mode read
Label lUrlGetter = new Label();
Label lHttpRespGetter = new Label();
Label lCredentialsGetter = new Label();
Label lExpectedResultGetter = new Label();
Label lMaxExecTimeGetter = new Label();
Label lMaxDelayGetter = new Label();
Label lTimeoutGetter = new Label();
#endregion
protected override void OnInit(EventArgs e)
{
#region Edit
tUrl.ID = "tUrl";
tHttpResp.ID = "tHttpResp";
tCredentials.ID = "tCredentials";
tExpectedResult.ID = "tExpectedResult";
tMaxExecTime.ID = "tMaxExecTime";
tMaxDelay.ID = "tMaxDelay";
tTimeout.ID = "tTimeout";
#endregion
#region Validation
//// Url
valUrl.ID = "valUrl";
valUrl.ControlToValidate = tUrl.ID;
valUrl.ValidationExpression = @"((http:\/\/|https:\/\/)(www.)?(([a-zA-Z0-9-]){2,}\.){1,4}([a-zA-Z]){2,6}(\/([a-zA-Z-_/.0-9#:+?%=&;,]*)?)?)";
valUrl.ErrorMessage = "Invalid format. Url must begin with http:// or https://.";
//// Max exec time
valMaxExecTime.ID = "valMaxExecTime";
valMaxExecTime.ControlToValidate = tMaxExecTime.ID;
valMaxExecTime.Type = ValidationDataType.Integer;
valMaxExecTime.Operator = ValidationCompareOperator.GreaterThanEqual;
valMaxExecTime.ValueToCompare = 0.ToString();
valMaxExecTime.ErrorMessage = "Invalid format. Please enter a number greater than or equal to 0.";
//// Max delay
valMaxDelay.ID = "valMaxDelay";
valMaxDelay.ControlToValidate = tMaxDelay.ID;
valMaxDelay.Type = ValidationDataType.Integer;
valMaxDelay.Operator = ValidationCompareOperator.GreaterThanEqual;
valMaxDelay.ValueToCompare = 0.ToString();
valMaxDelay.ErrorMessage = "Invalid format. Please enter a number greater than or equal to 0.";
//// Timeout
valTimeout.ID = "valTimeout";
valTimeout.ControlToValidate = tTimeout.ID;
valTimeout.Type = ValidationDataType.Integer;
valTimeout.Operator = ValidationCompareOperator.GreaterThanEqual;
valTimeout.ValueToCompare = 0.ToString();
valTimeout.ErrorMessage = "Invalid format. Please enter a number greater than or equal to 0";
#endregion
#region Read
lUrlGetter.ID = "lUrlGetter";
lHttpRespGetter.ID = "lHttpRespGetter";
lCredentialsGetter.ID = "lCredentialsGetter";
lExpectedResultGetter.ID = "lExpectedResultGetter";
lMaxExecTimeGetter.ID = "lMaxExecTimeGetter";
lMaxDelayGetter.ID = "lMaxDelayGetter";
lTimeoutGetter.ID = "lTimeoutGetter";
#endregion
base.OnInit(e);
}
protected override void CreateChildControls()
{
// tester EditMode pour gerer l'affichage : getter ou setter ?
//
#region ce qui ne change jamais
// Controls thate never change
#endregion
if (this.EditMode == true)
{
#region Mode edit
// Url interrogee
this.Controls.Add(tUrl);
this.Controls.Add(valUrl);
// Http response
this.Controls.Add(tHttpResp);
// Credentials
this.Controls.Add(tCredentials);
// Expected result
this.Controls.Add(tExpectedResult);
// Max exec time
this.Controls.Add(tMaxExecTime);
this.Controls.Add(valMaxExecTime);
// Max delay
this.Controls.Add(tMaxDelay);
this.Controls.Add(valMaxDelay);
// Timeout
this.Controls.Add(tTimeout);
this.Controls.Add(valTimeout);
#endregion
}
else
{
#region Mode read
// Url interrogee
this.Controls.Add(lUrlGetter);
// Http response
this.Controls.Add(lHttpRespGetter);
// Credentials
this.Controls.Add(lCredentialsGetter);
// Expected result
this.Controls.Add(lExpectedResultGetter); // DUPLICATED VALUE SETS HERE !!
// Max exec time
this.Controls.Add(lMaxExecTimeGetter);
// Max delay
this.Controls.Add(lMaxDelayGetter);
// Timeout
this.Controls.Add(lTimeoutGetter);
#endregion
}
base.CreateChildControls();
}
// Save new details after I edit the selected item
public ISettings SaveSettings()
{
var settings = new Settings();
settings.Url = tUrl.Text;
settings.HttpResponse = tHttpResp.Text;
settings.Credentials = tCredentials.Text;
settings.ExpectedResult = tExpectedResult.Text;
settings.MaxExecTime = tMaxExecTime.Text;
settings.MaxDelay = tMaxDelay.Text;
settings.Timeout = tTimeout.Text;
return settings;
}
// Populate my controls with details of my selected item
public void LoadSettings(ISettings settings)
{
Settings getSettings = new Settings();
getSettings = (Settings)settings;
lUrlGetter.Text = tUrl.Text = getSettings.Url;
lHttpRespGetter.Text = tHttpResp.Text = getSettings.HttpResponse;
lCredentialsGetter.Text = tCredentials.Text = getSettings.Credentials;
lExpectedResultGetter.Text = tExpectedResult.Text = getSettings.ExpectedResult;
lMaxExecTimeGetter.Text = tMaxExecTime.Text = getSettings.MaxExecTime;
lMaxDelayGetter.Text = tMaxDelay.Text = getSettings.MaxDelay;
lTimeoutGetter.Text = tTimeout.Text = getSettings.Timeout;
}
你有什么主意吗 ?我不明白问题出在哪里。它发生在一个简单的 Controls.Add() 期间很奇怪
提前致谢 :)