how to make imagebutton respond to click but not return key

M

mhylden

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");
....
 
M

mhylden

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.
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top