문제

I have a HtmlGenericControl

HtmlGenericControl a = new HtmlGenericControl("a");

    a.Attributes.Add("href", "test.aspx");

    a.InnerText = "foo";

I want to make an ajax call to return this object and render it on a page. I tried serializing it to json object in c# and pass it to the page, but don't know how to deserialze it to html control in jquery.

Also, if what i am trying is impossible, is there a way to convert the HtmlGenericControl into a string?

도움이 되었습니까?

해결책

Instead of trying to convert the HtmlGenericControl to a string I would instead retrieve the href attribute value (in your example "text.aspx") from the method using a jQuery ajax call to the method (return "href" as a JSON value) and then use jQuery to render the tag using one of the JavaScript document.write(), jQuery.html() or jQuery.append() methods, incorporating the JSON value from the ajax call for the href attribute into the inserted string.

$.getJSON('/home/getHref', function(data) {
   var $newAnchor = $('<a href="' + data.href + '"/>')
   $("my_div").append($newAnchor); 
}

Is this for a WebForms site or an MVC website? If you are using MVC then my solution is the proper way of doing this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top