Default Button in a UserControl

A

Alex Maghen

Let's say I have several UserControls on my page. Each of the UserControls
has a TextBox and an asp:Button. The page that they sit on also have form
fields and buttons.

When the focus is inside one of the UserControl text boxes and the user
presses ENTER, I want this to result in a virtual button press on one of the
buttons in that User Control. How do I do this when I have several
UserControls on the page?

Show I create a <form> around the fields in each of the UserControls? Won't
that then result in nested <form>'s?

Suggestions are most appreciated.

Alex
 
H

Hans Kesting

Alex Maghen presented the following explanation :
Let's say I have several UserControls on my page. Each of the UserControls
has a TextBox and an asp:Button. The page that they sit on also have form
fields and buttons.

When the focus is inside one of the UserControl text boxes and the user
presses ENTER, I want this to result in a virtual button press on one of the
buttons in that User Control. How do I do this when I have several
UserControls on the page?

Show I create a <form> around the fields in each of the UserControls? Won't
that then result in nested <form>'s?

Suggestions are most appreciated.

Alex

You can't use multiple <form>s, but you can use <asp:panel> around the
textbox+button. Set the DefaultButton property (of the Panel) to the ID
of the Button.

Hans Kesting
 
A

Allen Chen [MSFT]

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.
 
A

Allen Chen [MSFT]

Hi Alex,

I didn't notice Hans's reply. If you want to put the TextBox and the Button
in one Panel you can try what Hans suggested. It's a more convenient way.
If not you can try my suggestion. Actually the Panel control internally
renders the JavaScript for us.

Regards,
Allen Chen
Microsoft Online Community Support
 
A

Allen Chen [MSFT]

Hi Alex,

Have you solved this problem?

Regards,
Allen Chen
Microsoft Online Support
 
Joined
Nov 16, 2010
Messages
1
Reaction score
0
Alex never replied (hope he's OK) but I had the same issue. The suggestion by Hans of using panels worked great. Thank You!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top