Design and CSS styles
Version 6.x > Design and CSS styles > Two user control conflict issue View modes: 
User avatar
Member
Member
steven4733-gmail - 12/22/2011 11:59:19 AM
   
Two user control conflict issue
I have a custom user control which I put it both in masterpage and a content page. In the user control, there are some required field validation control which I found if use the UC in a content page, after you click the submit button it also validate the UC in masterpage. If you click the submit button of the UC in masterpage, it does not validate the UC in content page. How to avoid this id conflict between masterpage and a content page? Hope I describe the issue cliearly.

User avatar
Certified Developer 9
Certified Developer 9
charbf - 12/22/2011 12:37:54 PM
   
RE:Two user control conflict issue
Hi,

you can use the property ValidationGroup="" for all your controls

If your using portal you can use the webpart layout to differentiate from master templete and your content

Francois

User avatar
Member
Member
steven4733-gmail - 12/22/2011 1:37:51 PM
   
RE:Two user control conflict issue
If I didn't get your suggestion wrong. It's easy to change the textbox id in webpart layout, but I have no way to change it's backend code. So I think it does not make sense. Where to set the validationgroup=""?

User avatar
Certified Developer 9
Certified Developer 9
charbf - 12/23/2011 9:10:55 AM
   
RE:Two user control conflict issue
Hi,

If you add the property ValidationGroup="somegroup" to your textbox, validator and button

they will only validate for the same group and will ignore the other controls that are bound to an other group

you can't change the IDs in the layout of the webpart. if you do the code behind won't work

add it like this

<asp:TextBox ID="txt" ValidationGroup="somegroup" runat="server" ></asp:TextBox>

User avatar
Member
Member
steven4733-gmail - 12/23/2011 10:11:19 AM
   
RE:Two user control conflict issue
Do you mean to add the validationgroup property by creating a new layout of the webpart? After add this property in a new layout of the webpart, it comes out an error says:

[Error loading the WebPart 'ContactUs']
http://server/CMSWebPartLayouts/2fb39d94-e401-482f-a532-c399414a3ec4/ContactUs.ascx.cs(912305): error CS0115: 'ASP.cmswebpartlayouts_2fb39d94_e401_482f_a532_c399414a3ec4_contactus___contentcontactusform_ascx.FrameworkInitialize()': no suitable method found to override


Any idea?

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 12/29/2011 7:24:10 AM
   
RE:Two user control conflict issue
Hello,

do you use custom controls or any standard web parts?

If you use the custom controls, please change it on the .ascx template directly in the file system.

It is a standard ASP.NET property so you can find some help by google, the official Microsoft document is for example here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup.aspx


Best regards,
Helena Grulichova

User avatar
Member
Member
steven4733-gmail - 12/29/2011 8:32:49 AM
   
RE:Two user control conflict issue
I use custom control. It works fine as long as only one shows in a combined page. However, when it appears twice(one is in masterpage, the other is in content page), the submit button will trigger validation control for both. If I modify the template directly in the file system, I mean add validationgroup, it still the same thing, doesn't make sense to this situation.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/2/2012 2:03:08 AM
   
RE:Two user control conflict issue
Hi,

Could you please post here the exact code you are using and how are you entering the custom controls to the pages? Have you tried to add the validation group property to your user control and set it for your control?

You should set the validation group for the control instance - are you using ASPX templates?

Best regards,
Juraj Ondrus

User avatar
Member
Member
steven4733-gmail - 1/3/2012 8:34:48 AM
   
RE:Two user control conflict issue

ASCX CODE:
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI" tagprefix="asp" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>

<h2>Contact Form</h2>
<div>Name:*</div>
<div><asp:TextBox ID="tb_Name" runat="server" Width="220px"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ErrorMessage="!!"
ControlToValidate="tb_Name" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>
<div>Company Name:</div>
<div><asp:TextBox ID="tb_CompanyName" runat="server" Width="220px"></asp:TextBox></div>
<div>Email:*</div>

<div><asp:TextBox ID="tb_Email" runat="server" Width="220px"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server" ErrorMessage="!!"
ControlToValidate="tb_Email" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>
<div><asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Invalid Email" ControlToValidate="tb_Email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
CssClass="ErrorMessage" Display="Dynamic"></asp:RegularExpressionValidator></div>
<div>Phone:*</div>

<div><asp:TextBox ID="tb_Phone" runat="server" Width="220px"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server" ErrorMessage="!!"
ControlToValidate="tb_Phone" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>
<div>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="tb_Phone" ErrorMessage="Invalid Phone"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
CssClass="ErrorMessage" Display="Dynamic"></asp:RegularExpressionValidator></div>
<div>Inquiry Type:*</div>
<div>
<asp:DropDownList ID="ddl" runat="server" Width="220px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Sales Inquiry</asp:ListItem>
<asp:ListItem>Customer Service</asp:ListItem>
<asp:ListItem>Technical Support</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="ddl" ErrorMessage="!!" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>

<div>Question/Comment:*</div>
<div>
<asp:TextBox ID="tb_Comment" runat="server" TextMode="MultiLine" Height="64px"
Width="220px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="tb_Comment" CssClass="ErrorMessage" ErrorMessage="!!"></asp:RequiredFieldValidator>
</div>
<br />
<div><asp:Button ID="btn_Send" runat="server" Text="Submit"
onclick="btn_Send_Click" Width="100px" /> <asp:CheckBox
ID="cb_SendMe" runat="server" Text="Send me a copy" /></div>
<div id="ContactMsg" runat="server"></div>

</ContentTemplate>
</asp:UpdatePanel>

<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div>
<img src="~/CMSWebParts/ContactUs/image/PleaseWait.gif" />Please wait...</div>
</ProgressTemplate>
</asp:UpdateProgress>


ASCX.CS CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PortalControls;
using CMS.GlobalHelper;
using CMS.CMSHelper;
using System.IO;
using System.Net.Mail;
public partial class CMSWebParts_ContactUs_ContactUs : CMSAbstractWebPart
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Send_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("~/CMSWebParts/ContactUs/ContactUs.txt");
string mailBody = File.ReadAllText(fileName);

mailBody = mailBody.Replace("##Type##", ddl.SelectedValue);
mailBody = mailBody.Replace("##Name##", tb_Name.Text);
mailBody = mailBody.Replace("##Company##", tb_CompanyName.Text);
mailBody = mailBody.Replace("##Email##", tb_Email.Text);
mailBody = mailBody.Replace("##Phone##", tb_Phone.Text);
mailBody = mailBody.Replace("##Comments##", tb_Comment.Text);

MailMessage myMessage = new MailMessage();
myMessage.Subject = "Inquiry message";
myMessage.Body = mailBody;
myMessage.From = new MailAddress(tb_Email.Text.Trim(), tb_Name.Text);
myMessage.To.Add(new MailAddress("test@xxxx.com", "Steven"));
if (cb_SendMe.Checked)
myMessage.CC.Add(new MailAddress(tb_Email.Text.Trim(), tb_Name.Text));


try
{
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
ddl.SelectedValue = "";
tb_Name.Text = "";
tb_CompanyName.Text = "";
tb_Email.Text = "";
tb_Phone.Text = "";
tb_Comment.Text = "";
cb_SendMe.Checked = false;
ContactMsg.InnerHtml = "Your message has been sent.<br />Thank you!";
}
catch (Exception)
{

throw;
}

}
}
}


I know validation group property, but I'm going to use the same user control both in Masterpage and content page. Validation group will be the same the user control appears twice in a combined page, right? I added the user control to Kentico via administrator->development->web part.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 1/3/2012 3:09:31 PM
   
RE:Two user control conflict issue
I have edited your code with notes on what to do and why. This should solve your problem.

Using validation groups allows you to configure which validators are fired when a button is clicked and which validators are used to determine whether Page.IsValid. Google about asp.net validation groups for more info. Here is the code:
ASCX CODE:
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI" tagprefix="asp" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>

<h2>Contact Form</h2>
<div>Name:*</div>
<div><asp:TextBox ID="tb_Name" runat="server" Width="220px"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ErrorMessage="!!"
ControlToValidate="tb_Name" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>
<div>Company Name:</div>
<div><asp:TextBox ID="tb_CompanyName" runat="server" Width="220px"></asp:TextBox></div>
<div>Email:*</div>

<div><asp:TextBox ID="tb_Email" runat="server" Width="220px"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server" ErrorMessage="!!"
ControlToValidate="tb_Email" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>
<div><asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Invalid Email" ControlToValidate="tb_Email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
CssClass="ErrorMessage" Display="Dynamic"></asp:RegularExpressionValidator></div>
<div>Phone:*</div>

<div><asp:TextBox ID="tb_Phone" runat="server" Width="220px"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server" ErrorMessage="!!"
ControlToValidate="tb_Phone" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>
<div>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="tb_Phone" ErrorMessage="Invalid Phone"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
CssClass="ErrorMessage" Display="Dynamic"></asp:RegularExpressionValidator></div>
<div>Inquiry Type:*</div>
<div>
<asp:DropDownList ID="ddl" runat="server" Width="220px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Sales Inquiry</asp:ListItem>
<asp:ListItem>Customer Service</asp:ListItem>
<asp:ListItem>Technical Support</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="ddl" ErrorMessage="!!" CssClass="ErrorMessage"></asp:RequiredFieldValidator></div>

<div>Question/Comment:*</div>
<div>
<asp:TextBox ID="tb_Comment" runat="server" TextMode="MultiLine" Height="64px"
Width="220px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="tb_Comment" CssClass="ErrorMessage" ErrorMessage="!!"></asp:RequiredFieldValidator>
</div>
<br />
<div><asp:Button ID="btn_Send" runat="server" Text="Submit"
onclick="btn_Send_Click" Width="100px" /> <asp:CheckBox
ID="cb_SendMe" runat="server" Text="Send me a copy" /></div>
<div id="ContactMsg" runat="server"></div>

</ContentTemplate>
</asp:UpdatePanel>

<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div>
<img src="~/CMSWebParts/ContactUs/image/PleaseWait.gif" />Please wait...</div>
</ProgressTemplate>
</asp:UpdateProgress>


ASCX.CS CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PortalControls;
using CMS.GlobalHelper;
using CMS.CMSHelper;
using System.IO;
using System.Net.Mail;
public partial class CMSWebParts_ContactUs_ContactUs : CMSAbstractWebPart
{
protected void Page_Load(object sender, EventArgs e)
{
// this should ensure that the validation group is unique
String validationGroup = "vg"+this.clientID;
//since we're dynamically creating the validation group name,
//we need to programmatically set the validation group on your validators
RequiredFieldValidator1.ValidationGroup = validationGroup;
RequiredFieldValidator2.ValidationGroup = validationGroup;
RequiredFieldValidator3.ValidationGroup = validationGroup;
RequiredFieldValidator4.ValidationGroup = validationGroup;
RequiredFieldValidator5.ValidationGroup = validationGroup;
//Set the validation group on your button
btn_Send.ValidationGroup = validationGroup;
}
protected void btn_Send_Click(object sender, EventArgs e)
{
//You must always call Page.Validate() before checking if Page.Isvalid
//In this instance to solve your problem you want to call Page.Validate("yourvalidationgroup");
Page.Validate(((Button)sender).ValidationGroup); // cast the sender as a button and get the validation group name from it
if (Page.IsValid)
{
string fileName = Server.MapPath("~/CMSWebParts/ContactUs/ContactUs.txt");
string mailBody = File.ReadAllText(fileName);

mailBody = mailBody.Replace("##Type##", ddl.SelectedValue);
mailBody = mailBody.Replace("##Name##", tb_Name.Text);
mailBody = mailBody.Replace("##Company##", tb_CompanyName.Text);
mailBody = mailBody.Replace("##Email##", tb_Email.Text);
mailBody = mailBody.Replace("##Phone##", tb_Phone.Text);
mailBody = mailBody.Replace("##Comments##", tb_Comment.Text);

MailMessage myMessage = new MailMessage();
myMessage.Subject = "Inquiry message";
myMessage.Body = mailBody;
myMessage.From = new MailAddress(tb_Email.Text.Trim(), tb_Name.Text);
myMessage.To.Add(new MailAddress("test@xxxx.com", "Steven"));
if (cb_SendMe.Checked)
myMessage.CC.Add(new MailAddress(tb_Email.Text.Trim(), tb_Name.Text));


try
{
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
ddl.SelectedValue = "";
tb_Name.Text = "";
tb_CompanyName.Text = "";
tb_Email.Text = "";
tb_Phone.Text = "";
tb_Comment.Text = "";
cb_SendMe.Checked = false;
ContactMsg.InnerHtml = "Your message has been sent.<br />Thank you!";
}
catch (Exception)
{

throw;
}

}
}
}

User avatar
Member
Member
steven4733-gmail - 1/3/2012 3:37:11 PM
   
RE:Two user control conflict issue
@Jiveabillion,
Thank you very much for your help, fantastic!! I thought I have to add validationGroup property at frontend. Anyway, thank you.

But I still need help on the UpdateProgress part. It shows at the master page even whenever the page is load. However, it supposed to show right after clicking the send button. This UpdateProgress part works just fine in the content page. Any idea?

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 1/3/2012 3:57:57 PM
   
RE:Two user control conflict issue
I'm not sure about the UpdateProgress part. You may need to manually handle the update panel in your code to get it to work the way you want. I haven't written any controls that use an updatepanel in Kentico and I have a subscription to Telerik ASP.Net controls and use their custom update panel since it allows more control.

Does the UpdateProgress show when you just load the page? Does it show in both places?

User avatar
Member
Member
steven4733-gmail - 1/3/2012 4:18:00 PM
   
RE:Two user control conflict issue
It only shows in the user control which located in masterpage. For the user control that located in a content page, it works fine. This is very weird that I once thought it is because the script manager of Kentico is loaded after the user control, but after review the page template of master page I found it is loaded before any thing in master page.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 1/3/2012 4:27:53 PM
   
RE:Two user control conflict issue
This might not be a Kentico related issue. Try manually controlling the update panel. Google for how to do that. Other than that, I can't be of more help without troubleshooting it myself, which I don't have time to do right now. best of luck to you though.

User avatar
Member
Member
steven4733-gmail - 1/3/2012 4:32:55 PM
   
RE:Two user control conflict issue
Thank you all the same. You already helped me a lot.