Hi Alex,
To achieve the requirement I think it would be better to use JavaScript. I
wrote a sample here that demonstrates how to do this:
Default.aspx:
<form id="form1" runat="server" defaultbutton="Button1">
<asp:Button ID="Button1" runat="server" Text="Button" />
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
<uc2:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
WebUserControl1.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
WebUserControl1.ascx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterStartupScript("samekey",@"<script
type=""text/javascript"">
function Func(event, target) {
var __TestnonMSDOMBrowser =
(window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
if (event.keyCode == 13 && !(event.srcElement &&
(event.srcElement.tagName.toLowerCase() == ""textarea""))) {
var defaultButton;
if (__TestnonMSDOMBrowser)
{
defaultButton = document.getElementById(target);
}
else {
defaultButton = document.all[target];
}
if (defaultButton && typeof(defaultButton.click) != ""undefined"") {
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
</script>");
this.TextBox1.Attributes.Add("onkeypress", "javascript:return
Func(event,'" + this.Button1.ClientID + "')");
}
protected void Button1_Click(object sender, EventArgs e)
{
Page.RegisterStartupScript("key1", "<script
type=\"text/javascript\">alert('from WebUserControl1')</script>");
}
}
}
WebUserControl2.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl2.ascx.cs"
Inherits="WebApplication1.WebUserControl2" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click" />
WebUserControl2.ascx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class WebUserControl2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterStartupScript("samekey", @"<script
type=""text/javascript"">
function Func(event, target) {
var __TestnonMSDOMBrowser =
(window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
if (event.keyCode == 13 && !(event.srcElement &&
(event.srcElement.tagName.toLowerCase() == ""textarea""))) {
var defaultButton;
if (__TestnonMSDOMBrowser)
{
defaultButton = document.getElementById(target);
}
else {
defaultButton = document.all[target];
}
if (defaultButton && typeof(defaultButton.click) != ""undefined"") {
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
</script>");
this.TextBox1.Attributes.Add("onkeypress", "javascript:return
Func(event,'" + this.Button1.ClientID + "')");
}
protected void Button1_Click(object sender, EventArgs e)
{
Page.RegisterStartupScript("key1", "<script
type=\"text/javascript\">alert('from WebUserControl2')</script>");
}
}
}
You can see even if the default button is set on the Page's form the
WebUserControls can still work. The key point is to register the javascript
via RegisterStartupScript method. We catch the press event of the input and
force clicking the button.
Please have a try and let me know if it works. If you need further
assistance please feel free to ask.
Regards,
Allen Chen
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.