Create Asp.Net Elements On The Fly Javascript - Stack Overflow

I am attempting to create asp elements on the fly using javascript. I can successfully create html elem

I am attempting to create asp elements on the fly using javascript. I can successfully create html elements on the fly like so

var _upload = document.createElement("input");

_upload.setAttribute("type", "file");

_upload.setAttribute("ID", "upload" + rownum);

_upload.setAttribute("runat", "server");

_upload.setAttribute("name", "uploads" + rownum);

A few questions:

  1. Is this the equivalent to the asp FileUpload control?

  2. How can I create the exact asp controls in javascript?

What can an asp fileupload do that my control I created above cannot do if anything?

I am attempting to create asp elements on the fly using javascript. I can successfully create html elements on the fly like so

var _upload = document.createElement("input");

_upload.setAttribute("type", "file");

_upload.setAttribute("ID", "upload" + rownum);

_upload.setAttribute("runat", "server");

_upload.setAttribute("name", "uploads" + rownum);

A few questions:

  1. Is this the equivalent to the asp FileUpload control?

  2. How can I create the exact asp controls in javascript?

What can an asp fileupload do that my control I created above cannot do if anything?

Share Improve this question edited Jun 8, 2012 at 14:45 Nick LaMarca asked Jun 8, 2012 at 14:27 Nick LaMarcaNick LaMarca 8,19832 gold badges96 silver badges154 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

ASP.NET is a server side language, whereas JavaScript in this case is being used client side. Elements created on the client side in this manner will not be visible to ASP.NET on the server side.

You can't. ASP.NET controls are piled before being rendered on the server side. And the browsers don't know anything about ASP.NET.

Your best bet is to do an ajax request to your server so that you get piled ASP.NET controls - i.e. normal HTML.

Florians idea is useful, but (depending on your case of course) there may be an even simpler fix: create the fileupload control when rendering the page but put in inside a hidden div. Then all you have to do in JavaScript is show the div.

Some example code (asp controls may be wrong, it's been a while since I used them):

mypage.aspx:

<div id="fileupload-div">
   <FileUpload id="myupload" runat="server" />
</div>
<button id="mybutton">Show upload control</button>

style.css

#fileupload-div { display: none }

mypage.js

$("#mybutton").click(function() {
    $("#fileupload-div").show();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742420958a4440669.html

相关推荐

  • Create Asp.Net Elements On The Fly Javascript - Stack Overflow

    I am attempting to create asp elements on the fly using javascript. I can successfully create html elem

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信