how to make imagebutton respond to click but not return key

Discussion in 'ASP .Net Web Controls' started by mhylden, Sep 28, 2005.

  1. mhylden

    mhylden Guest

    I need to make an imagebutton that is contained in a datagrid respond
    only to a mouse click, and not a return key press. I have a user
    control on the same page that contains text boxes (HTML controls) and
    link buttons (.NET web controls) to handle the return key press as a
    search. That part works, but when I do that, I still get a js
    confirmation that goes along with the link buttons in the datagrid, as
    if one of those had been clicked. Clicking cancel on the confirm
    dialog lets the response.redirect command from the search linkbutton
    continue on to the search results as expected, but I don't want the
    confirm to come up at all. I'd like to take the return key action off
    of the image buttons entirely, since the user should pick a specific
    row from the grid. I can't just disable the imagebutton though, since
    a click still needs to work.


    I've tried fiddling with the tab indexes on the different controls to
    get it to fire one and not the other, but no luck.


    Any help is greatly appreciated.


    Mikkel


    Code snippets below:
    this is the script for the imagebutton confirm, located at the top of
    my ASPX page that contains the datagrid:
    <script language="javascript">
    function ConfirmQuickComplete()
    {
    return confirm('Are you sure you want to mark
    this action item as
    complete?');
    }
    </script>
    script that checks for the return key press and fires redirect to
    search, located at the top of my user control:
    function CheckEnter()
    {
    var e = window.event;


    var key = e.keyCode;
    var src = e.srcElement;
    var srcName = src.id;


    if (key == 13)
    {
    if (srcName == 'txtFindProjHTML')
    {
    alert(srcName + key);
    __doPostBack('QuickPicks1$lbFindProj','');
    return false;
    }
    }


    }


    LinkButton click event handler, in user control:
    private void lbFindProj_Click(object sender, System.EventArgs e)
    {

    string stxt = Request["txtFindProjHTML"];
    Response.Redirect(Request.ApplicationPath +
    "/ActionTrack/ProjectList.aspx?projName=" + stxt,true);
    }
    The imagebutton is a template column in my datagrid, and I set
    properties on that in the ItemDataBound event on the grid as follows:
    ....
    ImageButton btn = (ImageButton)(e.Item.Cells[6].Controls[1]);
    btn.Attributes.Add("onclick", "return
    ConfirmQuickComplete();");
    btn.Attributes.Add("TabIndex", "0");
    ....
     
    mhylden, Sep 28, 2005
    #1
    1. Advertising

  2. mhylden

    mhylden Guest

    I figured out a solution, even if it may not be the most graceful one.

    I declare a javascript variable outside the keypress function in my
    user control like so:
    var returnPressed = false;
    function CheckEnter()
    {
    var e = window.event;

    var key = e.keyCode;
    var src = e.srcElement;
    var srcName = src.id;

    if (key == 13)
    {
    returnPressed = true;

    if (srcName == 'txtFindProjHTML')
    {
    <%= Page.GetPostBackEventReference(lbFindProj) %>;
    return false;
    }
    }
    }

    Then in the click event handler on my link button that should not fire
    on a return key press, which is in the main page, I check that
    variable:

    function ConfirmQuickComplete()
    {
    if (returnPressed)
    {
    return false;
    }
    else
    {
    return confirm('Are you sure you want to mark this action item as
    complete?');
    }
    }

    This allows the return key press to fire the appropriate search in my
    user control, but if the button is actually pressed, then the confirm
    event fires as it should. The js variable will be set on a lot of
    pages containing the user control where I don't really need it, but if
    similar problems arise it will be a quick scripting fix.
     
    mhylden, Sep 28, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin
    Replies:
    1
    Views:
    664
    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=
    Apr 26, 2005
  2. Paul Tomblin
    Replies:
    15
    Views:
    8,703
    Paul Tomblin
    Oct 24, 2004
  3. mhylden

    how to make imagebutton respond to click but not return key

    mhylden, Sep 28, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    161
    mhylden
    Sep 28, 2005
  4. James Tsao
    Replies:
    0
    Views:
    269
    James Tsao
    Oct 25, 2004
  5. M P
    Replies:
    1
    Views:
    485
Loading...

Share This Page