简体   繁体   中英

jquery helper like htmlgenericcontrol in asp.net

I want to build a webpage containing jquery draggables in asp.net (c#). How can I add my jquery code to my page in an object oriented way?

So I want to make ac# myDraggables.cs class that I add to the asp page with the htmlgenericcontrol. How do I add this jquery code (eg: $('#mydraggables').draggable(); ) in my class. I could do it with the htmlgenericcontrol by adding a "script"-tag. But isn't there a way to do it with a jquery helper (can't find that)? tx

A sample: Basicly, in my myDraggables-class I could have;

public HtmlControl Draw()
{
    HtmlGenericControl aDiv= new HtmlGenericControl("div");
    aDiv.Attributes.Add("id", "dragtest");
    return aDiv;
}

public HtmlControl DrawJquery()
{
    HtmlGenericControl js = new HtmlGenericControl("script");
    js.Attributes["type"] = "text/javascript";
    js.InnerHtml = "$('#dragtest').draggable();";   
    return js; 
}

And the code behind of my asp.page would look like;

protected void Page_Load(object sender, EventArgs e)
{
    myDraggables md = new myDraggables ('..some props ..');
    Page.Header.Controls.Add(md.DrawJquery());
    myPlaceHolder.Controls.Add(md.Draw());
}

That works, but jquery-code will become more complex. And will not be readable anymore... how can this be achieved in json? Or another more readable way?

Thanks

If I understand you correctly, you want to write a strongly-typed .NET class in the back-end which also functions as a jQuery object on the front-end, right? If so, the best way to do it is probably to serialize your .NET object into JSON format, store it in a variable and print it out in a script tag when generating the markup. Then, on the front-end, parse the JSON string into a Javascript object and you can use it from there.

Can you post any .NET and JS code you have already?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM