How create server control using javascript?

Last post 08-18-2009 12:29 PM by nonstick monkey. 5 replies.

Sort Posts:

  • How create server control using javascript?

    08-18-2009, 9:32 AM
    • Member
      point Member
    • nitinjain
    • Member since 12-16-2008, 1:43 PM
    • Posts 3

     I want to create asp.net control like panel,label etc inside javascript file within TD element.

    Also onmouseover and onmouseout event of TD element, i want to show and hide respectively.

    Is there any way to achive this?

     

     

  • Re: How create server control using javascript?

    08-18-2009, 10:25 AM
    • Contributor
      7,266 point Contributor
    • sirdneo
    • Member since 12-16-2008, 5:45 AM
    • Karachi, Pakistan
    • Posts 1,141

     Server side controls can't be created through javascript, with javascript you can only create client side controls.

    You can dynamically create client side textbox, labels through javascript . For details see this post:-

     

    http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=26696

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
  • Re: How create server control using javascript?

    08-18-2009, 10:27 AM
    Answer
    • All-Star
      75,081 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,963
    • TrustedFriends-MVPs

    You can't create server controls with JavaScript. Only HTML controls. This example adds a new row to a table tag with a TextBox inside of the row.

    <script type="text/javascript">
    <!--
    function addTextBox()
    {
     var tableRef = document.getElementById('yourTable');
     var lastRow = tableRef.rows.length;

     var newRow = tableRef.insertRow(lastRow-1);
     var newCell = newRow.insertCell(0);

     var controlRef = document.createElement('input');
     controlRef.type = 'text';
     controlRef.name = 'clientSideDynamicTextBox_' + lastRow;
     controlRef.id = 'clientSideDynamicTextBox_' + lastRow;
     controlRef.size = 20;

     newCell.appendChild(controlRef);
    }
    // -->
    </script>

    NC...

  • Re: How create server control using javascript?

    08-18-2009, 11:02 AM

    Adding server controls via javascript instead of just dom elements may not be simple to do, but it is possible. I'd suggest you go NC01's route unless you have a specific reason to do otherwise. 

    That said, the best way to do exactly what you are asking is to use JSON objects/AJAX. You basicly have a mirror of your serverside objects (at least the parts you care about) in the javascript side. Do a google search on IScriptControl to see how you can pass down information to the javascript object.

    Look into System.Web.Script.Serialization.JavaScriptSerializer for methods on how to pass data back and forth. You can use this to directly create objects from javascript to serverside and vice versa (there's a javascript version of this). This way you don't have to worry about manually setting every property.

    Once you have the objects passed back and forth, it's only a matter of dynamicly creating controls serverside based on the data given from the javascript.

    As an alterantive, if you want to create dyanmic serverside objects and update panels aren't fast enough, you may want to look into javascript webrequests. With these you can send only the data you care about and update a single dom element. A good trick is to simply create a page that handles only the bits you want to update and override render so you don't have extranious head tags sent.


    edit: Almost didn't notice your second question. A quick google search should help with the mouseover part. Look for the keywords onmouseover and onmouseout. If you want to go the ajax/json route so you can add things dynamicly, look up addhandler and clearhandler. Just remember to use delegates so you can access the "this" member.

  • Re: How create server control using javascript?

    08-18-2009, 12:08 PM
    • All-Star
      75,081 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,963
    • TrustedFriends-MVPs

    It is NOT possible to add server controls with JavaScript. The only possible way would be to call some server-side code that adds the controls, either through a PostBack or AJAX, and that would really not be adding them through JavaScript.

    NC...

  • Re: How create server control using javascript?

    08-18-2009, 12:29 PM

    Right, the serverside is what is adding them. The ajax is telling the serverside what to add. 

    In my example, the javascript tells the serverside to add the controls dynamicly, but doesn't add them itself. 

    NC01's clarification is correct, sorry if my explination was misleading.

Page 1 of 1 (6 items)