Home All Groups Group Topic Archive Search About
Author
24 Jan 2007 4:21 PM
David Thielen
Hi;

I have a page with several submit buttons on it. Is there a way to tie
TextBox controls to specific buttons so if the user hits the ENTER button
while in a text box, it is the submit button tied to it?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Author
25 Jan 2007 5:19 AM
Walter Wang [MSFT]
Hi Dave,

The form tag has an optional attribute "defaultbutton" which could specify
which button to submit when enter is pressed. However, it will only use one
default submit button on the whole form. I understand that you purpose is
to use different default submit button when focused in different input
fields.

One possible workaround is to handle each fields' client-side event
onkeypress and check the key there. ASP.NET 2.0 has an embedded javascript
source which has a function called WebForm_FireDefaultButton() could do
that:

    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="text1" runat="server">
    </asp:TextBox>
    <asp:TextBox ID="text2" runat="server"></asp:TextBox>
    <asp:Button ID="button1" runat="server" Text="button1"
OnClick="button1_Click" />
    <asp:Button ID="button2" runat="server" Text="button2"
OnClick="button2_Click"/>
    </div>
    </form>


    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptResource(typeof(Page),
"WebForms.js");
        text1.Attributes.Add("onkeypress", "javascript:return
WebForm_FireDefaultButton(event, 'button1')");
        text2.Attributes.Add("onkeypress", "javascript:return
WebForm_FireDefaultButton(event, 'button2')");
    }
    protected void button1_Click(object sender, EventArgs e)
    {
        Response.Write("button1");
    }
    protected void button2_Click(object sender, EventArgs e)
    {
        Response.Write("button2");
    }


Warning: this is using some undocumented javascript resource and function
in ASP.NET 2.0.

Regards,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
25 Jan 2007 4:42 PM
David Thielen
ok - I'll play around with this.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Show quoteHide quote
"Walter Wang [MSFT]" wrote:

> Hi Dave,
>
> The form tag has an optional attribute "defaultbutton" which could specify
> which button to submit when enter is pressed. However, it will only use one
> default submit button on the whole form. I understand that you purpose is
> to use different default submit button when focused in different input
> fields.
>
> One possible workaround is to handle each fields' client-side event
> onkeypress and check the key there. ASP.NET 2.0 has an embedded javascript
> source which has a function called WebForm_FireDefaultButton() could do
> that:
>
>     <form id="form1" runat="server">
>     <div>
>     <asp:TextBox ID="text1" runat="server">
>     </asp:TextBox>
>     <asp:TextBox ID="text2" runat="server"></asp:TextBox>
>     <asp:Button ID="button1" runat="server" Text="button1"
> OnClick="button1_Click" />
>     <asp:Button ID="button2" runat="server" Text="button2"
> OnClick="button2_Click"/>
>     </div>
>     </form>
>
>
>     protected void Page_Load(object sender, EventArgs e)
>     {
>         ClientScript.RegisterClientScriptResource(typeof(Page),
> "WebForms.js");
>         text1.Attributes.Add("onkeypress", "javascript:return
> WebForm_FireDefaultButton(event, 'button1')");
>         text2.Attributes.Add("onkeypress", "javascript:return
> WebForm_FireDefaultButton(event, 'button2')");
>     }
>     protected void button1_Click(object sender, EventArgs e)
>     {
>         Response.Write("button1");
>     }
>     protected void button2_Click(object sender, EventArgs e)
>     {
>         Response.Write("button2");
>     }
>
>
> Warning: this is using some undocumented javascript resource and function
> in ASP.NET 2.0.
>
> Regards,
> Walter Wang (waw***@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>