Default key for a button inside the gridview ASP.NET C# - asp.net

I am trying to assign the Enter key to a button inside of a gridview. Does anyone know how this can be accomplished? Thank you.

You can give the button an id on one of the rendering events of the GridView, and then with jQuery bind the keypress() to a main div that wraps your site, and have it simulate the click() event on the button in your gridview when the 'enter' key (keyCode 13) was detected.
Like this :
$(document).ready(function() {
$('#mainDivWrapperId').keypress(function(event) {
if (event.keyCode == '13') {
$('#buttonId').click();
}
});
});
Edited Addition :
If you have a button in a gridview, then you just need to catch the 'enter' key press event like I've shown above, and then you can have it trigger the 'click' event of your button.
Like this :
$(document).ready(function() {
$('#mainDivWrapperId').keypress(function(event) {
if (event.keyCode == '13') {
$('#buttonId').trigger('click');
}
});
});

actually to make it simple,
1st declare the button on click event handler.
2nd declare a on keydown/keypress event handler for grid view.
inside the gridview's keypress/keydown event handler, check for keycode 13 from the event argument.
if the event.keycode==13, do button.click().
what ever logic inside the button's click event handler will be processed.

Related

ASP.net - Gridview Textbox Enter Button Postback

I have a GridView with a Textbox and when the user changes the text within that box, I want them to be able to hit the enter button and postback and update the changes they made within that textbox on the row in the GridView.
I can't figure out how to do this?
Thanks,
Mark
You need to use some JavaScript code to do that - its on the page part. Here is one that I use (jQuery)
$(document).ready(function(){
// capture the editors
var AllEditors = jQuery('#<%= gvGridViewID.ClientID %> :input[type=text]');
AllEditors.keydown(function (e) {
if (e.keyCode == 13)
{
e.preventDefault();
// the [value=Update] is the default value to Update control of GridView
jQuery(this).parents("tr").find("input[value=Update]").click();
}
});
});
If you have it inside UpdatePanel, you need to initialize it each time the UpatePanel fires. If you have it inside a custom control, you need to add extra variables on the function names to avoid conflicts.

How to show Ajaxtoolkit modal popup extendar only if textbox1.text="show" esle do not show?

I have a textbox1 and button1 and panel1 (which is used as a popup control)
i want if textbox1.text="show" then modalpopup control whose id is panel1 will be visible on buttonclick event other wise .... modal popup control panel1 will not be shown ...
how to do this ? using vb.net ?
Use Javascript's getElementById method to determine if the text has that specific value and then call showPopup() if you want to.
function showPopup() {
var modalPopupBehavior = $find('programmaticModalPopupBehavior');
modalPopupBehavior.show();
}
function hidepopup() {
var modalPopupBehavior = $find('programmaticModalPopupBehavior');
modalPopupBehavior.hide();
}
I know you stated, you want to do this in vb.net, but then you're at server-side and it is far easier to deal with this on client-side if you don't have something not to.
Here's how you do in code-behind. Add this to your button click event:
If TextBox1.Text = "something" Then
ModalPopupExtender1.Show()
Else
ModalPopupExtender1.Hide()
End If

How to capture 'Update' click event in ASP.NET GridView with jQuery

I need to capture the 'Update' click event with jQuery in an asp.net GridView and have no way of knowing where to start. I'm still rather new to jQuery. My GridView is attached to a SQLDataSource and, naturally, has all the bells and whistles that that combination affords. Any help would be greatly appreciated.
Simply add the script block anywhere after the GridView is declared and it should work with the default non-templated GridView column. No code in the codebehind as it is purely a Javascript solution.
Use this if you are using a Link-type GridView column:
<script type="text/javascript">
// a:contains(The text of the link here)
$('#<%= theGridViewID.ClientID %> a:contains(Update)').click(function () {
alert('Update click event captured from the link!');
// return false: stop the postback from happening
// return true or don't return anything: continue with the postback
});
</script>
Use this if you are using a Button-type GridView column and you don't want your Javascript to block the postback:
<script type="text/javascript">
// :button[value=The text of the button here]
$('#<%= theGridViewID.ClientID %> :button[value=Update]').click(function () {
alert('Update click event captured from the button!');
});
</script>
Use this if you are using a Button-type GridView column and you want to have control whether to continue with the postback or not:
<script type="text/javascript">
// :button[value=The text of the button here]
var updateButtons = $('#<%= theGridViewID.ClientID %> :button[value=Update]');
updateButtons
.attr('onclick', null)
.click(function () {
alert('Update click event captured from the button!');
var doPostBack = true; // decide whether to do postback or not
if (doPostBack) {
var index = updateButtons.index($(this));
// 'Update$' refers to the GridView command name + dollar sign
__doPostBack('<%= theGridViewID.UniqueID %>', 'Update$' + index);
}
});
</script>
Update: I think this would be a better solution in replacement of the last (3rd) script block I presented above, since you won't need to update the __doPostBack function call manually based on the command name, and as such, it should be less error-prone:
<script type="text/javascript">
// :button[value=The text of the button here]
var updateButtons = $('#<%= theGridViewID.ClientID %> :button[value=Update]');
updateButtons.each(function () {
var onclick = $(this).attr('onclick');
$(this).attr('onclick', null).click(function () {
alert('Update click event captured from the button!');
var doPostBack = true; // decide whether to do postback or not
if (doPostBack) {
onclick();
}
});
});
</script>
Credit to Aristos for this idea. :)
Ok here is my solution to capture only one update (or more) from a button.
This is the javascript code that I run on update click
<script type="text/javascript">
function NowRunTheUpdate(){
alert("ok I capture you");
}
</script>
and here is the page code
`<asp:GridView ID="MyGridView" runat="server" OnRowDataBound="MyGridView_RowDataBound" ... >`
<asp:ButtonField Text="update" CommandName="Update" ButtonType="Button" />
...
Here is the code thats run behind and set the javascript.
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// I go to get the button if exist
Button button = control as Button;
if (button != null && button.CommandName == "Update")
// Add delete confirmation
button.OnClientClick = "NowRunTheUpdate();";
}
}
}
}
You need to attach a client-side event listener to the click event of the Update [link]button. I don't think it can be done using AutoGenerateEditButton="true" if you are doing it that way. You'll need to use a TemplateField so that you can manipulate the button. Then you can use jQuery to bind to the click event of the button.
Add the update column to the column templates. Convert it to a custom column, and modify it in such a way you can hook to it with jquery i.e. like adding a css class to it.
Gridview is nothing but a table with a bunch of "tr" and "td". If you understand that concept then it would be easy for you to handle anything at client side. If you have enabled auto everything then it will be a link which would result for Edit, Delete, Update or Cancel (Check View Source). The code given below should capture the update click event:
$("a:contains(Update)").live("click", function() {
//alert("hi"); do what needs to be done
return false;//would not sent the control back to server
});
HTH

How do I target a GridViewRow with an AJAX.Net Toolkit PopupExtender?

I have a lot of data to display in a GridView. Because there's so much information per row, I'd like to be able to display additional information when a user clicks on the row, so I thought a PopupExtender from the AJAX Toolkit would be perfect.
Ideally, I want the popup to display whenever any of the controls within the row are selected. I've been able to successfully attach the PopupExtender to a single control within the row, but I can't get the pop-up to attach to the row itself.
I would have thought that setting the PopupExtender's TargetControlId to the Row's ClientID within the RowDataBound event would work, but when I do this I get a runtime error:
TargetControlID of 'popupExtId' is not valid.
A control with ID 'gvList_ctl02' could not be found.
I noticed that the GridViewRow is rendered, the tr element does not include an id, so I also tried extending the GridView control to override the CreateRow method to render the id - using this method I was able to render the row's ID (e.g. gvList_ctl02), but the same runtime error was thrown when I added the PopupExtender back into the code.
I also tried binding the showPopup() javascript command to the row's onclick event to get the popup to display manually; whilst the click event is registered OK and is definitely triggered, the popup is still not shown.
Does anyone have any idea how to / if you can bind a PopupExtender to a GridViewRow?
My row bound code is as follows:
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Bind the popup extender's target ID to the row ID
// This will cause a runtime error
PopupControlExtender pop = e.Row.FindControl("popupExtId") as PopupControlExtender;
pop.TargetControlID = e.Row.ClientID;
// Also bind the client side click handler to try to get the popup to show
// The alert is triggered and no javascript error is generated, but the popup does not display
e.Row.Attributes.Add("onclick", "alert('Row Clicked'); $find('" + pop.BehaviorID + "').showPopup();");
}
}
Many thanks.
If you're not opposed to using an ajax ModalPopupExtender, I use a little bit of javascript and some sneaky hidden button clicks to fire off my modal popups from within a grid view. I usually make my modal popup extender's target control id my hidden button, then, via javascript, fire my hidden button's click event to show the modal popup.
Here's my modal popup and hidden button markup.
<asp:Button ID="hiddenButton" runat="server" Text="" style="display:none"></asp:Button>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
TargetControlID="hiddenButton" PopupControlID="Panel1" CancelControlID="CancelButton"
BackgroundCssClass="modalBackground" Drag="True"/>
Here's my javascript to show my popup.
function showModal(btnID) {
btn = document.getElementById(btnID);
btn.click();
}
In my rowdatabound event, I call the javascript function showModal from button's onclick event.
Button myButton = (Button)e.Row.Cells[9].Controls[1];
matchButton.Attributes.Add("onclick", "showModal('" + hiddenButton.ClientID + "');");
Hope this might help point you in the right direction.

Javascript : Enter Key Press

Good Morning...
I am using java script in each page to trigger the Enter key press Event inside the textbox.
It is working fine. Now i want to place the code in the .js file for global access.
function EnterKeyPress(id,e) {
// look for window.event in case event isn't passed in
if (window.event) { e = window.event; }
if (e.keyCode == 13 || e.keyCode == 121) {
window.document.getElementById('<%= ibtnSubmit.ClientID %>').click();
}
}
I dont want to hard code the control id. Can anybody help me please..
You can use your id parameter instead of the ControlID, and when you use this function in your pages, you can pass the ControlID as a parameter:
function EnterKeyPress(id,e) {
if (window.event) { e = window.event; }
if (e.keyCode == 13 || e.keyCode == 121) {
document.getElementById(id).click();
}
}
You are using ASP .NET, so in your code-behind you can assign the keypress event:
TextBox1.Attributes.Add("onkeypress",
string.Format("EnterPressKey('{0}', event);", ibtnSubmit.ClientID));
But wait, that functionality is already done on ASP .NET, you can use ASP:Panel controls to wrap your common controls, and you can assign a DefaultButton for the panel to indicate which button gets clicked when the Panel control has focus and the user presses the Enter key.
If you're using jQuery you can do it like this:
$("input[id*=ibtnSubmit]").click()
If you want the form to submit when the user presses the Enter key, then you don't need javascript. It will do so automatically. Just put the textbox inside a form element with an action:
<form action="process.php">
<input type="text">when the enter key is pressed, the form is submitted and sent to process.php</input>
</form>

Resources