I haven't found a solution for my problem.
I am using asp.net mvc. In my View I am trying to call a Method, on a Buttonclick.
This is how the button looks like at the moment
<button type="button" id="#openButtonID" data-loading-text=" öffnen..." class="btn btn-primary" autocomplete="off" onclick="location.href='#Url.Action("Open", "Home", new {index =i} )'" disabled="#notConnected">
So every time I am clicking the button the website reloads itself.
At the website there is a camera stream which disappears for some seconds during the refresh. Now I have the task to avoid this, so the stream can been seen during the whole time. I have tried to call the method via Razor like this:
<button type="button" id="#openButtonID" data-loading-text=" öffnen..." class="btn btn-primary" autocomplete="off" onclick="#{( (GateControl.Controllers.HomeController)this.ViewContext.Controller ).Open( i );}" disabled="#notConnected">
But this just calls the Method if the view is loaded. As I tried this I set the return of the method to void.
Also I tried it like it is described like in this. Either the website is reloaded or I wasn't able to get in the method. I also tried several code snippets from stack overflow, but it wasn't working.
This is how the Method looks like
[CustomAuthorize]
public void Open( int index )
{
//some code
}
Since you already tried something with jQuery I suppose you use the library.
What you can do is to bind a click event to your button and inside that to make a GET ajax call.
You can use a "data-" attribute in your button to have your url generated on server and available on client.
<button id="button-id" class="btn btn-primary" data-loading-text=" öffnen..." data-url="#Url.Action("Open", "Home", new {index =i} )" disabled="#notConnected" autocomplete="off">
This should do it:
$("#button-id").click(function(){
var url = $("#button-id").data("url");
$.get(url , function(result) {
//do something with the result if you ever need to
});
});
This way you won't get the page refresh. You could also give us a little more details about that server method and what it does, maybe it needs some changes also.
Here is the full code I am using
<script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<button type="button" id="#openButtonID" data-loading-text=" öffnen..." class="btn btn-primary" autocomplete="off" data-url="#Url.Action("Open", "Home" , new {index=i+1} )" disabled="#notConnected">
Öffnen
</button><br /><br />
<script type="text/jscript">
$("##openButtonID").click(function () {
var url = $("##openButtonID").data("url");
$.get(url, function (result) {
//do something with the result if you ever need to
});
});
</script>
Related
How can I access a row through data-id attribute in asp.net webforms? I have an edit button in a column on my table and I need to access the User_ID in that row. I I've seen examples with MVC but not webforms.
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#EditMenu" id="btnEdit" data-id="<%=TableId.Rows["User_ID"] %>">
Edit
</button>
You can achieve this via jQuery as follows:
$('#btnEdit').on('click', function(){
var userId = $(this).data('id');
//do something with userId
});
Please note the above snippet will work only if you are using jQuery version 1.4.3 or greater. If it's not then use the below snippet:
$('#btnEdit').on('click', function(){
var userId = $(this).attr("data-id")
//do something with userId
});
I have a IActionResult on my controller that returns a Partial View via AJAX:
[HttpGet]
[Route("/propertycoverages/loss")]
public IActionResult GetNewLoss()
{
return PartialView("_Loss");
}
If I put an <a> tag in my Razor view with the following:
<a asp-controller="PropertyCoverages" asp-action="GetNewLoss" id="loss-btn" data-ajax="true" data-ajax-update="losses" data-ajax-success="addLoss" data-ajax-method="GET">Add</a>
the following HTML attribute gets generated in the <a> tag: href="/propertycoverages/loss"
it works as expected and the partial view is returned within the page. However, if I try to use a button:
<button asp-controller="PropertyCoverages" asp-action="GetNewLoss" id="loss-btn" type="submit" data-ajax="true" data-ajax-update="losses" data-ajax-success="addLoss" data-ajax-method="GET">Add</button>
the following HTML attribute gets generated in the <button> tag: formaction="/propertycoverages/loss"
and I get redirected to /propertycoverages/loss which is not what I want. Is there a way I can make the button behave like the <a> tag?
Note: These elements are inside a <form>. I also tried switching the <button> from type="submit" to type="button" but the controller action doesn't get called.
You will want to attach a JavaScript method to your button click action.
<button onclick="myFunction()">Click me</button>
In the JS method you can call back to the action get the HTML and lay in on the page in the AJAX call success method.
function onClick() {
var url = [setURL];
$.ajax({
type: "GET",
url: url,
data: { },
success: function (response) {
$('#[setElementToReplace]').html(response);
}
});
}
Hi there are mainly three ways for calling the controller action from the submit button.
Way1: Using simple Form
#using (Html.BeginForm("ActionName", "ControllerName"))
{
<input type="submit" name="add" value="Add" />
}
Note: By default it is a get request, If required you can change formmethod = post
Way 2: Using html Attributes
#using (Html.BeginForm())
{
<input type="submit" name="add" value="Add" formaction="/anycontrollername/anyactionname" formmethod="get"
/>
}
Way 3 : using jquery
#using (Html.BeginForm())
{
<input type="submit" name="add" value="Add" id=”save”
/>
}
$(document).ready(function () {
$("#save").click(function () {
$("form").attr("action", "/anycontroller /anyaction");
});
});
It seems that 2nd way will be suitable for your requirement,
Hope the above answer was useful.
I am new to Meteor this is how I kept the button in html file.
<input type="button" class="number" value="1">
<input type="button" class="number" value="2">
<input type="button" class="number" value="3">
How to get the value of these buttons in the js file.
Any one help me.
Thanks in advance.
Get the value using an event:
'click .number': function(event, template) {
console.log(event.currentTarget.value);
}
Or plain Jquery:
$($('.number')[0]).val()
$($('.number')[1]).val()
$($('.number')[2]).val()
well to determine which buttons get clicked you can simply use event handlers for your template. And its very important to know that the buttons are identified through their class names. Therefor you have to choose unique classnames.
For example if you have the buttons inside a template called template1 you just do the following:
//in your .html inside your template1
<button class="button1">Button1</button>
<button class="button2">Button2</button>
<button class="button3">Button3</button>
and the corresponding JS:
//in your clientside JS
Template.template1.events({
"click.button1": function () {
//exec code when button1 clicked
//[...]
},
"click.button2": function () {
//exec code when button2 clicked
//[...]
},
"click.button3": function () {
//exec code when button3 clicked
//[...]
}
});
if your buttons aren't in an template, but just inside your just use the area as template. for example Template.body.events will handle events in your body.
Can be done like below:
Template.TemplateName.events({
'click .number': function (event,template) {
return event.target.value;
}
});
I have a page with table. Each table row, has two links "delete", and "edit". "delete" works fine.
I would like to do this scenario:
When user clicks on row "edit" link, a small window appears with the fields of this row.
User decide to edit or not.
User may press "Save Changes", or "Cancel".
I did the option of small window with JavaScript document.createElement(), and the window appears successfully.
But I would like to add helpers for "Save Changes", and "Cancel" buttons.
I can't do this using helpers
Template.codesList.events({
'submit form#newForm': function (events) {
// some actions
};
},
'click #edit': function () {
var px = 'px';
// Create an Overlay
var myOverlay = createOverlay();
document.body.appendChild(myOverlay);
// Create edit window display it over the Overlay
var editWindow = createPopup(300, 400);
// Create elements and append it to edit window
var editForm = editWindowForm(this._id, this.name);
editWindow.appendChild(editForm);
document.body.appendChild(editWindow);
},
'click #delete': function () {
if (confirm("Are you sure?")) {
Codes.remove({_id: this._id})
}
},
'submit form#editForm': function (event) {
event.preventDefault();
console.log("Clicked"); // This doesn't displayed
}
});
And this is the form after displaying it.
<form id="editForm" style="margin-right: 3em; margin-left: 3em;">
<div class="form-group">
<label for="itemCode" class="control-label">Code</label>
<input id="itemCode" name="itemCode" class="form-control" placeholder="Enter code">
</div>
<div class="form-group">
<label for="itemName" class="control-label">Name</label>
<input id="itemName" name="itemName" class="form-control" placeholder="Enter name">
</div>
<input type="submit" value="Save Changes" class="btn btn-primary">
<input type="button" value="Cancel" class="btn btn-info">
</form>
But when I press on "Save Changes" button, no print from console.log() and the form is making the normal submit and the page reloads again.
So, what I'm missing?
By the way that's the output of the console:
document.querySelector('form#editForm')
<form id="editForm" style="margin-right: 3em; margin-left: 3em;">…</form>
Define edit form as a template. Use event handlers as usual to handle save and cancel button clicks.
To render it, either just put it inside the page template with '#if sessionEditPopup' or if you must do it yourself then use UI.renderWithData docs here
BTW manually modifying DOM using jquery etc is something to be avoided unless there is no other way and is not the Meteor way of doing things.
Add event on click save the data into Session because Session can access globally.
And Take the data from Template.name.Healper using session,
So when u change the session value that will automatically change your page content.
Here is the link may be useful for U
http://meteortips.com/first-meteor-tutorial/sessions/
I am new to jQuery, trying to populate a GridView or Telerik RadGrid using jQuery. Not sure how to go about it and unable to find any examples. Any help is appreciated. Thanks.
Essentially I am trying to display a modal window with a textbox and button. The user enters a search criteria presses the button and a gridview in the same modal window is populated with the results.
The user than selects records in the grid presses another button and the selected users are inserted into the database table, modal window is closed and a grid on the parent page is refreshed showing the new added users.
<input type="button" id="btnAddNewUserj" value="Add New User" />
$(document).ready(function() {
$("#btnAddNewUserj")
.click(function() { ShowNewUserDialog(); return false });
$("#btnSearch")
.click(function() { FindUsers(); return false });
});
function ShowNewUserDialog() {
$("#newuserDialog").dialog({ modal: true, bgiframe: true }).dialog("open");
}
function FindUsers() {
// HOW TO DO THIS?
// Show selectable list of users from the database in grid.
}
<div id="newuserDialog" title="Add New User" style="display:none;">
<div>
<input id="txtSearchFor" type="text" />
<input id="btnSearch" type="button" value="Search" class="Button" /></div>
<p> DISPLAY RESULTS HERE </p>
<div style="margin:10px 6px;">
<input type="button" id="btnjAdd" value="Add" class="Button" />
<input type="button" id="btnjCancel" value="Cancel" class="Button" />
</div>
</div>
A couple of thoughts here. You cannot populate a GridView or Telerik Grid using jQuery. jQuery is a client side technology and those two grids are server side.
You can use jQuery to hit a web service and build out and HTML table with the results (which is basically what a GridView does).
I'm guessing however, that you would be better served just using native GridView databinding. You can use a .Net UpdatePanel around the grid if you want to prevent full post backs.
I think telerik is not the answer.
Use telerik radgrid's client side binding, see this for an example: http://demos.telerik.com/aspnet-ajax/grid/examples/clientbinding/defaultcs.aspx
Note that sample shows how to bind to a WCF Web Service and an ADO.NET Data Service. There are other samples around.
Other variations of binding: http://demos.telerik.com/aspnet-ajax/grid/examples/client/declarativedatabinding/defaultcs.aspx
More on client side binding: http://demos.telerik.com/aspnet-ajax/grid/examples/client/databinding/defaultcs.aspx