Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 121939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:02:59+00:00 2026-05-11T04:02:59+00:00

I have a bit of a problem with user controls. Basically what I want

  • 0

I have a bit of a problem with user controls. Basically what I want to accomplish is the following:

  1. I have a view for editing an invoice.
  2. In this view I have a usercontrol with a list of invoice items
  3. I also have a div that is activated with jQuery for adding a new invoice item
  4. When I add the invoice item I want to refresh just the user control with the list of items

How would I do this without hacks? Something I was thinking of was the following:

[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken] public ActionResult Create(InvoiceLine line) { if (Request.IsAjaxRequest()) {     if (!ModelState.IsValid)     {         return PartialView('CreateLineControl', product);     } }            return PartialView('DisplayLinesControl', product); } 
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:03:00+00:00Added an answer on May 11, 2026 at 4:03 am

    First you would call an aspx page w/ jQuery (we will use an http handler that we will map in the web.config below, more on this later)

    the basic idea is that we want the server side to render the user control as xhtml and dump this ‘updated’ markup back into the DOM in our success method (client side)

    $.ajax({     type: 'GET',     url: 'UserDetails.aspx?id=' + id,     dataType: 'html',     error: function(XMLHttpRequest, textStatus, errorThrown)     {         alert(XMLHttpRequest.responseText);     },     success: function(xhtml)     {         var container = document.createElement('div');          container.innerHTML = xhtml;          document.getElementById('someElement').appendChild(container);     } }); 

    The technique below is what I used to leverage a user control via the HttpHandler to reuse the control for both ajax and .net work

    The below was done w/ .NET 1.1 (but i’m sure you can do it in .NET 2.0+) the class below implements IHttpHandler, and the real work is in the process request sub as you can see below

    The only issue I had with this at the time was that asp.net controls would not render w/out a form tag in the user control so I used normal html and all was good

    Public Class AJAXHandler      Implements IHttpHandler      Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable         Get             Return False         End Get     End Property      Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest         Dim Request As System.Web.HttpRequest = context.Request         Dim path As String = Request.ApplicationPath         Dim newHtml As String         Dim pg As New System.Web.UI.Page          context.Response.Cache.SetCacheability(HttpCacheability.NoCache)         context.Response.ContentType = 'text/html'                                  Dim uc As New UserDetail                                 uc = CType(pg.UserControl(path + '/Controls/UserDetail.ascx'), UserDetail)                                 Dim sb As New System.Text.StringBuilder                                 Dim sw As New System.IO.StringWriter(sb)                                 Dim htmlTW As New HtmlTextWriter(sw)                                  uc.LoadPage(custid, CType(pro, Integer))                                 uc.RenderControl(htmlTW)                                 context.Response.Write(sb.ToString())                                 context.Response.End()      End Sub   End Class 

    And finally in your web.config you need to define the handler and map it to the aspx path you listed in your ajax call

      <system.web>     <httpHandlers>         <add verb='*' path='UserDetails.aspx' type='project.AJAXHandler, project' />     </httpHandlers>   </system.web> 

    Now you can call the user control with UserDetails.aspx and render the user control as html. Then after you render this it will return html (after response.end is called)

    then in javascript you can find the parent DOM element to your user control, remove it and append or innerHTML this new html

    Update

    Above is the solution I used with webforms, but with MVC the below will produce the same result with much less work.

    The jQuery function would be the same but on the server side you would simply create a new controller action + PartialView w/ the markup you wanted (basically a user control)

    Function Edit(ByVal id As Integer) As ActionResult     Dim User As User = UserService.GetUserById(id)      Return PartialView('User', User) End Function 

    Now inside my ascx I simply render the html and this is what gets sent back to the browser for the container.innerHTML work (again the client side code is the same for both MVC and Webforms in this scenario)

    <%@ Control Language='VB' Inherits='System.Web.Mvc.ViewUserControl(Of User)' %> <% Dim User As User = DirectCast(Model, User)%> <div id='innerDetail'>     <label for='username'>Username</label>     <input type='text' id='username' name='username' value='<%= User.Username %>' /><br />      <a id='lnkUpdate' href='/Application/User.aspx/Edit/<%= User.ID %>' onclick='UpdateUser(this); return false;'>Update User Information</a>     <span id='lblUpdateStatus' style='display: inline;'></span>     </div> </div> 

    The reason this works with much less code in MVC is that we don’t have to work around the page lifecycle that is required with a normal aspx file in webforms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.