views:

55

answers:

2

In MVC .NET application we can easily use PartialView to print the output of an ASCX file.

For example, I have Book.ascx file, I could have this in the controller

public ActionResult Book(int id)
{
  BookModel model = new BookModel() { bookId = id };
  return PartialView("Book", model);
}

which returns the output of Book.ascx

Is there any way we can do this in a normal .NET website?

I want to be able to use it with AJAX, eg.
When an Update button is clicked, replace the content of <div id="book123"> with the output of /Book.ascx?id=123

Is that possible?

I am looking for something like this

$.get('Book.ascx?id=123', function(data) {
  $("#book123").html(data);
});

But that won't work because you can't call Book.ascx directly...

Thanks in advance

A: 

Its not possible to use partial views in asp.net form page. But you can the traditional way with user controls (.ascx).

This is probably best described in the asp.net documentation: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx

hjortureh
Thanks hjortureh, but I am still not sure how to use it in AJAX. I updated the question to include the code I was after.
A: 

well, it is doable, but not pretty :)

you could instantiate your control, and instead of passing the key via QueryString, do it via exposed property:

var ctrl = new BookModel();
ctrl.BookID = "book123";

than you could render the HTML out of it with something like

private string renderControl(Control ctrl)
{
  System.Text.StringBuilder sb = new System.Text.StringBuilder();
  System.IO.StringWriter tw = new System.IO.StringWriter(sb);
  System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
  ctrl.RenderControl(hw);
  return sb.ToString();
}

than you would pass that html back via your service and insert into the div etc..

Sonic Soul
Thanks Sonic Soul. I am still not sure how to use it in jQuery. Btw BookModel is a ViewModel that you only use in MVC. You can just use query string for Website. I added a sample jQuery code I was after.
ah. that makes sense. in your subject however you indicated .ascx control which will work with above solution
Sonic Soul
oh yea you're right... i'll edit it. So there is really no way to do that without creating a new aspx page is there?
I guess now the question is, can I convert my ASCX into ASPX and still be able to use it in other ASPX pages? Do you know, Sonic Soul?
i have not used MVC in asp.net, so i can't say what the limits with ascx controls are. but: 1. above technique can be contaiend within a method that will use ascx controls and return rendered HTML for you to use. You will have to stray a bit from MVC there, but depending on your architecture it may be viable way of doing things. 2. you cannot use aspx pages inside of other pages (unless you use frames or iframes but that is problematic, in that they can't really communicate in the way an aspx+ascx can communicate).
Sonic Soul
3. you could create a very basic aspx page, that would be a container for your ascx, and used only for your MVC needs. (again my knowledge of MVC is limited so i can't be certain of this implementation but seems like viable option)
Sonic Soul
I will do the third one. This is not MVC, I was just comparing it to MVC on my question but this has nothing to do with MVC. Creating an aspx as a container to the ascx seems to be the only simple way. Thanks Sonic Soul!
if you're doing the 3rd one, you could take it further by making general utility container page, that accepts query parameters for which ascx to load, along with what parameters to pass to it. than dynamically load appropriate one.
Sonic Soul