Call code behind functions with html controls

I have a simple function that I want to call in code behind the file name Move and I was trying to see how it can be done and I am not using the asp image button because I am not trying to use the asp server side controls as they are like generally don't work well with ASP.net MVC .. the way it is configured now, it will find a javascript function named Move, but I want it to call a function named move in code behind the same view

 <img alt='move' id="Move" src="/Content/img/hPrevious.png" onclick="Move()"/>


protected void Move(){

}

      

// based on search criteria update the new table

 protected void Search(object sender EventArgs e)
{
 for (int i = 0; i < data.Count; i++){
 HtmlTableRow row = new HtmlTableRow();
 HtmlTableCell CheckCell = new HtmlTableCell();
 HtmlTableCell firstCell = new HtmlTableCell();
 HtmlTableCell SecondCell = new HtmlTableCell();
 CheckBox Check = new CheckBox();
 Check.ID = data[i].ID;
 CheckCell.Controls.Add(Check);
 lbl1.Text = data[i].Date;
 lbl2.Text = data[i].Name;
  row.Cells.Add(CheckCell);
  row.Cells.Add(firstCell);
  row.Cells.Add(SecondCell); 
  Table.Rows.Add(row);
}

      

}

0


source to share


2 answers


Scott Guthrie has a very good example on how to do this using routing rules.

This will give you the option to navigate to a URL in the format / Search / [Query] / [PageNumber] like http: // site / Search / Hippopotamus / 3 and it will show page 3 of the search results for the hippo.

Then, in your opinion, just click the following button: http: // site / Search / Hippopotamus / 4 ", no javascript required.



Of course, if you want to use javascript, you can do something like this:

function Move() {
    var href = 'http://blah/Search/Hippopotamus/2';
    var slashPos = href.lastIndexOf('/');
    var page = parseInt(href.substring(slashPos + 1, href.length));
    href = href.substring(0, slashPos + 1);

    window.location = href + (++page);
}

      

But this is much more complicated than just incrementing the page number parameter in the controller and setting the url of the next button.

+1


source


You cannot make callbacks or call anything on a view from JavaScript in an ASP.NET MVC application. Anything you want to call from JavaScript must be an action on a controller. It's hard to say more without knowing in more detail what you are trying to do, but if you want to call some sort of Move method in your web application from JavaScript, then Move must be an action on the controller.

Based on the comments, I'm going to update this answer with a more complete description of how you can implement what I understand as the problem described in the question. However, this question is missing some information, so I am speculating here. Hopefully the general idea gets through, even if some of the details don't match the exact TStamper code.

Let's start with the controller action:

public ActionResult ShowMyPage();
{
    return View();
}

      

Now I know I want to re-render this page and do it using the argument passed from the JavaScript function on the page. Since I will be showing the same page again, I will just change the action to accept the argument. String arguments are NULL, so I can continue to do the initial page rendering as always, without having to worry about specifying some default value for the argument. Here's the new version:

public ActionResult ShowMyPage(string searchQuery);
{
    ViewData["SearchQuery"] = searchQuery;
    return View();
}

      

Now I need to call this page again in JavaScript. So I use the same url I used to display the page, but add a query string parameter with the table name:



http://example.com/MyControllerName/ShowMyPage?searchQuery=tableName

Finally, in my aspx, I can call the code behind the function by passing the searchQuery from the view data. Once again, I have serious caveats about using code in an MVC application, but this will work.

How to call the code-behind function in aspx:

<% Search(ViewData["searchQuery"]); %>

      

I changed the arguments. Since you are not handling the event (with some exceptions such as Page_Load, not in MVC), the search function does not need an event handler signature. But I added a "tablename" argument so you can pass this from aspx.

Once again, I'll comment on this in the code. It looks to me like you are trying to use ASP.NET standard techniques inside the MVC framework when MVC works differently. I highly recommend going through the MVC tutorials to see examples of more standard ways to do this sort of thing.

0


source







All Articles