Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top