C# ASP.Net快速按钮单击以在UpdatePanel中添加控件会产生不一致性问题
当我单击按钮并将控件添加到UpdatePanel内的占位符时,一切正常,除非我快速单击几次,然后收到如下错误消息:C# ASP.Net快速按钮单击以在UpdatePanel中添加控件会产生不一致性问题,c#,asp.net,updatepanel,C#,Asp.net,Updatepanel,当我单击按钮并将控件添加到UpdatePanel内的占位符时,一切正常,除非我快速单击几次,然后收到如下错误消息: 'Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID 'VehicleRegistrationEnhancedTextBox3_Label' were f
'Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID 'VehicleRegistrationEnhancedTextBox3_Label' were found. FindControl requires that controls have unique IDs.' when calling method: [nsIDOMEventListener::handleEvent]
[Break On This Error]
我将一个整数保存在一个隐藏字段中,以创建唯一的ID,当我真正快速单击时,该方法会执行多次,但计数值尚未更新。我尝试使用C#lock关键字,但没有任何效果
代码如下:
protected void AddVehicleButton_Click(object sender, EventArgs e)
{
lock (this)
{
int count = Convert.ToInt32(VehicleRegistrationCountHiddenField.Value);
var TBId = "VehicleRegistrationEnhancedTextBox" + count;
IList<Panel> oldPanels = (IList<Panel>)Session["VehiclePanels"] ?? new List<Panel>();
//Seperator
Literal hr = new Literal { Text = "<hr/>" };
//RemoveSpan
Literal span = new Literal() { Text = "<span class=\"RemoveVehicleRegistration\">X</span>" };
//Crop
Control uc = LoadControl("~/Controls/ImageUploadAndCrop/ImageUploadAndCrop.ascx");
uc.ID = "VehicleRegistrationImageUploadAndCrop" + count;
//Vehicle Registration
Label vehicleRegistration = new Label
{
ID = TBId + "_Label",
AssociatedControlID = TBId,
Text = "Vehicle Registration:"
};
EnhancedTextBox vehicleTypeTextBox = new EnhancedTextBox
{
ID = TBId,
Required = true,
RequiredErrorText = "Vehicle Registration is a required field."
};
//Add new controls to the form
Panel newPanel = new Panel();
newPanel.Controls.Add(hr);
newPanel.Controls.Add(span);
newPanel.Controls.Add(vehicleRegistration);
newPanel.Controls.Add(uc);
newPanel.Controls.Add(vehicleTypeTextBox);
AddVehiclePlaceholder.Controls.Add(newPanel);
//Increment the ID count
count++;
VehicleRegistrationCountHiddenField.Value = count.ToString();
//Save the panel to the Session.
oldPanels.Add(newPanel);
Session["VehiclePanels"] = oldPanels;
//Go back to the same wizard step.
ShowStep2HiddenField.Value = "true";
ShowStep3HiddenField.Value = "false";
}
}
protectedvoid AddVehicleButton\u单击(对象发送方,事件参数e)
{
锁(这个)
{
int count=Convert.ToInt32(vehiclerRegistrationCountHiddenField.Value);
var TBId=“VehiclerRegistrationHancedTextBox”+计数;
IList oldPanels=(IList)会话[“车辆面板”]??新列表();
//分离器
文字hr=新文字{Text=“
”};
//移开
文字span=new Literal(){Text=“X”};
//收成
Control uc=LoadControl(“~/Controls/imageuploadcrop/imageuploadcrop.ascx”);
uc.ID=“车辆注册图像上载作物”+计数;
//车辆登记
标签车辆注册=新标签
{
ID=TBId+“_标签”,
AssociatedControlID=TBId,
Text=“车辆登记:”
};
EnhancedTextBox车辆类型TextBox=新的EnhancedTextBox
{
ID=待定,
必需=真,
RequiredErrorText=“车辆登记是必填字段。”
};
//向窗体中添加新控件
Panel newPanel=新面板();
newPanel.Controls.Add(hr);
newPanel.Controls.Add(span);
newPanel.Controls.Add(车辆注册);
newPanel.Controls.Add(uc);
newPanel.Controls.Add(车辆类型文本框);
添加VehiclePlaceHolder.Controls.Add(新建面板);
//增加ID计数
计数++;
VehiclerRegistrationCountHiddenField.Value=count.ToString();
//将面板保存到会话中。
旧面板。添加(新面板);
会话[“车载面板”]=旧面板;
//返回到同一向导步骤。
ShowStep2HiddenField.Value=“true”;
ShowStep3HiddenField.Value=“false”;
}
}
问题是您正在使用同一请求回退多个回发,然后最后一个响应是更新更新面板div的内容,这导致了您的问题。一个可能的解决方案是包含一些javascript来禁用单击按钮
<asp:Button ID="myUpdatePanelPostBackButton" runat="server" OnClientClick="this.disabled=true; return true;" Text="Submit" />
假设您的按钮位于UpdatePanel内,当更新面板返回时,该按钮将重新启用。禁用的按钮不会导致回发。所以,若只禁用OnClientClick中的按钮,则不会发生回发 请尝试使用submitbehavior=“false”,它的意思是“无论如何都会导致回发”
<asp:Button runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Submitting...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Submit Me!" />