Professional Web Applications Themes

how to make imagebutton respond to click but not return key - ASP.NET Building Controls

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

  1. #1

    Default how to make imagebutton respond to click but not return key

    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 Guest

  2. #2

    Default Re: how to make imagebutton respond to click but not return key

    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 Guest

Similar Threads

  1. ImageButton's Click Event not firing
    By sam in forum ASP.NET Building Controls
    Replies: 0
    Last Post: May 16th, 07:58 PM
  2. Does not Posts Back the form on ImagebUtton click asp.net
    By KhurramAtk in forum ASP.NET Security
    Replies: 1
    Last Post: March 26th, 06:11 AM
  3. ImageButton.click question
    By John Saunders in forum ASP.NET Building Controls
    Replies: 1
    Last Post: June 8th, 01:16 PM
  4. Custom ImageButton will not Fire on button click..HELP! Thanks. :)
    By Teemu Keiski in forum ASP.NET Building Controls
    Replies: 0
    Last Post: April 8th, 07:38 PM
  5. Click event on ImageButton column in a datagrid
    By J McD in forum ASP.NET Building Controls
    Replies: 0
    Last Post: February 23rd, 11:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139