DefaultButton when using masterpages ASP.net - c#

I am using masterpage in my solution. In that masterpage there is a imagebutton named "Save" using as the saving option for all my pages, which is set as Defaultbutton in masterpage. The problem is that, in one of my page i have a textchanged event which i want to work when pressing "Enter" key. But when i press "Enter" key "Save" function works after the textchanged event. I dont want to happen "Save" function on "Enter" key press. I don't want there to be any default button on my page

You can set the button's UseSubmitBehavior = "false"
similar question is here Canceling the default submit button in ASP.NET

I think you have to prevent enter key while you are work in text-box
just put this function in ur page.
just add onkeydown event to ur text-box
<input type='text' onkeydown="return (event.keyCode!=13);" />
another one
this function need jquery
$('input').keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
}
});

Related

Assign textbox's value to another texbox without a botton

I want to know what is the best way to assign a value to textbox 2 instantly as the user write this value in textbox 1 ie. Directly showing what the user is entering in textbox1 also in textbox2 at the same. I'm using MVC5 aspx pages..
Any help is highly appreciated. Thanks in advance
Add a onkeyup event listener to your first element(Whenever a key is pressed, the value in first textbox is also entered in second.). Then call the function like
function enterAmt(ev) {
document.getElementById('amt2').value = ev.value;
}
You have to use javascript or jquery for it, suppose you have two textboxes.
HTML:
<input type="text" id="txtBox1"/>
<input type="text" id="txtBox2"/>
You have to write keyup event for the textbox and copy its value in second.
JQUERY:
$("#txtBox1").on('keyup', function () {
$("#txtBox2").val($(this).val())
})
Here you can find the solution to the absolutely same question: Textbox onchange in ASP.NET

To stop reloading web page In Asp.net on any button click

how stop postback on any button click. My page is reloading as soon as i click on reset button on the registration page, i want to reset the form without reloading the page itself.
You have two possibilities:
Simply set the attribute AutoPostBack="false" on your button or whatever control.
As an alternative you could also add the following javascript to the click event of the button :
onclick="return false"
This prevents the button from submitting.
Try following:
<asp:button runat="server".... OnClientClick="return false;" />
First you have to know about Sever Control and normal HTML control.
If you used Server Button Control then your Page reload on each click.
If you wan to stop it then you have to use AutoPostBack="false", using this your server side method calling is stop.
Otherwise use Normal HTML Button Control and use JavaScript to reset your form.

type submit vs type button

I have a button which right now is set as a type "submit". This calls the controller, execute some code and returns back to the view. When I use jquery to hide the button, I see that when I click on the button, what I have hides the button but as soon as the view is returned, the button is not hidden no more. Whereas with type "button", when I click the button, this hides the button but doesnt execute the code in the controller. Is there a way to hide the type "submit" button so when the view returns, the button is still hidden?
$('#btnAdd').click(function() {
$('#btnAdd').hide();
});
<input type='submit'> creates a button that submits a form to a server and triggers your server code. If you want the button hidden when the page comes back, you need to add logic to your page to do that. How you do this will depend on your server technology (php, .net, etc.).
The reason the behavior with <button> is different is that <button>s don't submit the form (unless you add more code to make them do that)...so the above mentioned stuff never happens. It's not so much that a <button> stays hidden as much as the page never changes/reloads. If you added code to the <button> to make it refresh the page, it'd reappear, too.
The button is shown because the page is newly displayed after submiting the form. Your "old" page, where clicked and hid the button is history.
What do you want?
Pressing a button, do something on serverside, do not change your current page:
Use a button of type button, use ajax to call the server side.
Or use a button of type submit and do what Pablo said http://en.wikipedia.org/wiki/Post/Redirect/Get on serverside.
Pressing a button, do something on serverside, give user feedback:
Use <form method="post" to markup your form. Use a submit button to call the server side. On serverside hide the submit button, if it is called by method post (calling a page with link or typing it into the address field is calling it with method = get).
What is the difference between type submit and type button?
A submit button works without javascript to send some input to serverside. The surrounding form is send to the server and the response is rendered in browser.
A button button needs a javascript onclick handler, a javascript function. The onclick handler is called when the user pressed the button.
Since the page will be reloaded upon pressing the submit button, the button will reappear. One quick and dirty to get what you want is...
First, create a hidden field
<input type="hidden" id="hidden" value="" />
Then, when you press submit, in a click event for submit button, do something like this..
$('#submit').click(function() {
$('#hiddenField').val("1");
$('#form').submit();
return false;
});
Now in your controller, use the value of hiddenField of pass some variable to the view which can be used like this...
<?php if($hidden == "1"): ?>
<input type="submit" id="submit" value="Submit" />
<?php endif; ?>
As far as the button not submitting the form is concerned, it won't submit the form, until you submit the form yourself on the click event of button. Something like this...
$('button').click(function() {
$('#form').submit();
});
Of course, as I mentioned this is a quick and dirty way to implement the function you want, there are better ways - using AJAX, also the implementation can change depending on what server side language you use (I used php over here).
When your form is submitted and your controller process the data, if certain criteria is met, you can set a temporary session variable or a cookie in server side code. So, basically the page will check for this variable on every page load. Example in PHP:
if( empty($_SESSION['temp']['hideSubmitButton']) ) {
$submitButton = '<button type="submit">Normal Button</button>';
} else {
$submitButton = '<button type="submit" disabled="disabled">Disabled Button</button>';
// or $submitButton = '';
}
But then you have to decide when to unset() the $_SESSION['temp'] or $_COOKIE['temp'] variable.

ModalPopup inside the UpdatePanel with default form values

I have a ModalPopupExtender inside an UpdatePanel which opens an input form. The problem is when I click the "Edit" button (which is also inside the UpdatePanel) I want to fill the form with existing values using server side code. But it OnClick method of the button doesn't seem to work.
My question is: How can I make the serverside code run first, than show the edit form?
You need to show the ModalPopupExtender from server side.
First, link the ModalPopupExtender's TargetControlID to a dummy hiddenfield or a button with style="display:none" instead of the "Edit" button. I know it's sound stupid, but it's a know workaround.
Then make sure the asp.net the "Edit" button is set as a asyncpostbacktrigger if children as trigger is set to false.
Also set CausesValidation="false" to avoid the postback to be blocked by unrelated validators on the page.
Finally, At the end of "Edit" button's click event, call ModalPopupExtender.Show() to display the pop up.

Call a specific button onClick event when the enter key is pressed C#

I'm trying to get a specific asp:button onclick event to fire when I press the enter key in a specific asp:textbox control.
The other factor to be taken into account is that the button is within a asp:Login control template.
I've no idea how to do this, suggestions on a postcard please.
You could look at the DefaultButton property of the panel control.
You could set the DefaultButton property on the form. Either as an attribute of the form tag in your markup DefaultButton = "btnSubmit" or using something like this in your code-behind:
Page.Form.DefaultButton = "btnSubmit"
Its HtmlForm.DefaultButton
You need to do it with javascript. It's really easy with jQuery.
You can do something like (off the top of my head, not tested):
$('#myTextBox').keypress(function(e){
if(e.which == 13)
$('#myBtn').click();
});
Edit: Be aware that although jQuery works exceptionally cross browser, there are some quirks with keypress described here.
Whoops i didnt see you said the "enter key" i thought you said "any key", yeah in that case use DefaultButton on asp:panel

Categories

Resources