Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
XML
I am trying to figure out a better way to execute ajax code. Right now from aspx file  I call a ashx file in which I put html code combined with C# code.

However it seems not the best solution. Is there a way to call an html or aspx that will return the results after C# code executed? Like a template where you replace placeholders with actual values returned by the C# code.

Thank you.

Here is a sample code:

ASPX file:

 var accountID = $("#<%= hdnAccountID.ClientID %>").val();

            $.ajax({
                type: "POST",
                url: "/MyCourses/Handlers/defaultCourse.ashx?&AccountID=" + accountID,
                data: 'AccountID=' + accountID,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data != "0") {

                        $("#divAjaxDefaultCourse").html(data);
                        $('#divAjaxDefaultCourse').show();
                    }
                },
                error: function () {
                    alert('Sorry some error occurred');
                }
            });

//*****************************************

ashx file

      string htmlOutput =
             "<div class='col-lg-6 mid_right'>"+
                "<div class='course_banner'>"+
                    "<div class='banner_left'>"+
                        "<img src='/images/banner_right_icon.png'>"+
                      "</div>"+
                      "<div class='banner_content'>"+
                          "<div class='formatCourseHeader' style='font-size: 24px; color: #000000; font-weight: 500; width: 50%; margin-top: 43px; float: left;'>"+
                               "<div id='courseNameDefault'>"+ defaultCourse.CourseTitle + "</div>" +
                               "<div id='daysDefault'>Days: " + defaultCourse.MeetingDays + "</div>" +
                               "<div id='timesDefault'>Time: "  + defaultCourse.MeetingTime + "</div>" +
                               "<div runat='server' id='divClassSizeDefault'>" + classSize + " Classmates</div>"+
                           "</div>"+
                      "</div>"+
                "</div>"+

     return htmlOutput;
        }
Posted
Comments
TcJoshJohnson 16-Mar-14 1:14am    
This is exactly the type of problem the Razor view engine was designed to resolve. Have you looked into making that switch?

You can add WebMethod on aspx page code behind. You can call that from the aspx page.

If you want to do this from HTML page, then you have to create a WebService or WCF Service. Call that Service from HTML or aspx page using jQuery Ajax.
 
Share this answer
 
Comments
Albert83 16-Mar-14 1:17am    
Thank you for your reply. I know about WebMethods. But it's not a solution for this case.
I actually want to have an aspx or html file and in that file have placeholders to replace C# code.
For example:
<html>
...
<p> Course Name: { $courseName } </p>

$courseName will be retrieved from a C# code page.
..
</html>
If the aspx or HTML files are different files, then for that you have to read that HTML or aspx page using C# File operation classes. Then replace the values directly.

Is that what you are doing?
Something similar to this idea
C#
var Golfers = [

       { ID: "1", "Name": "Bobby Jones", "Birthday": "1902-03-17" },

       { ID: "2", "Name": "Sam Snead", "Birthday": "1912-05-27" },

       { ID: "3", "Name": "Tiger Woods", "Birthday": "1975-12-30" }

       ];

XML
<script id="GolferTemplate1" type="text/html">

       {{=ID}}: <b>{{=Name}}</b> <i>{{=Birthday}}</i> <br />

   </script>



   <script id="GolferTemplate2" type="text/html">

       <tr>

           <td>{{=ID}}</td>

           <td><b>{{=Name}}</b></td>

           <td><i>{{=Birthday}}</i> </td>

       </tr>

   </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900