Adding controls at client side
-
Is there any way to add controls dynamically at client side without any postback?
-
Is there any way to add controls dynamically at client side without any postback?
Hi there, You may reconsider your approach here as a web server control basically exists at the server side, and at the client side what you see is the static html markup rendered from the control. So you are only able to do that with the server side code, however, you can provide some client side script to add a html element dynamically. For example:
function AddDynamicCheckBox()
{
var chkBox = document.createElement("<INPUT style='display:inline' type=checkbox tabindex=-1 type=checkbox/>");
window.Form1.appendChild(chkBox);
} -
Hi there, You may reconsider your approach here as a web server control basically exists at the server side, and at the client side what you see is the static html markup rendered from the control. So you are only able to do that with the server side code, however, you can provide some client side script to add a html element dynamically. For example:
function AddDynamicCheckBox()
{
var chkBox = document.createElement("<INPUT style='display:inline' type=checkbox tabindex=-1 type=checkbox/>");
window.Form1.appendChild(chkBox);
}Thanks for your answer. I can add now the controls dynamically at client side and access the values of the added controls at the server side.
-
Hi there, You may reconsider your approach here as a web server control basically exists at the server side, and at the client side what you see is the static html markup rendered from the control. So you are only able to do that with the server side code, however, you can provide some client side script to add a html element dynamically. For example:
function AddDynamicCheckBox()
{
var chkBox = document.createElement("<INPUT style='display:inline' type=checkbox tabindex=-1 type=checkbox/>");
window.Form1.appendChild(chkBox);
}Hi minhpc_bk, Everything is working fine. The problem what I have now is the control added through client side script disappears when the page is refreshed. Can we prevent this?
-
Hi minhpc_bk, Everything is working fine. The problem what I have now is the control added through client side script disappears when the page is refreshed. Can we prevent this?
Unfortunately no, because when you hit Refresh, a http request will be posted gain and you only see the result markup returned from the server, of course it does not contain any html element dynamically added at the previous step. You may think of persisting the dynamically generated markup by calling web services at the client side, it certainly requires you to provide more code. It should work in the way that every time the page is loaded, your client side script will invoke a web method to pull out the dynamically added markup and attach to the document, also you need to save any added element by the user for loading later when the user hit Refresh. For the sample code of working with web service in client side script, you can see this blog entry: http://weblogs.asp.net/mschwarz/archive/2004/07/16/184748.aspx[^] Or you may need a work-around solution here. Just some ideas.
-
Unfortunately no, because when you hit Refresh, a http request will be posted gain and you only see the result markup returned from the server, of course it does not contain any html element dynamically added at the previous step. You may think of persisting the dynamically generated markup by calling web services at the client side, it certainly requires you to provide more code. It should work in the way that every time the page is loaded, your client side script will invoke a web method to pull out the dynamically added markup and attach to the document, also you need to save any added element by the user for loading later when the user hit Refresh. For the sample code of working with web service in client side script, you can see this blog entry: http://weblogs.asp.net/mschwarz/archive/2004/07/16/184748.aspx[^] Or you may need a work-around solution here. Just some ideas.
Ok, thanks for your answer. I have little time to finish the work and hence I cannot go for the web service solution. So I want to leave the refresh problem as it is now. Now what I am doing is reading the values of dynamically added controls from the page and creating server side controls to show them again after a form submit. In the client side control creation, I am appending a br tag to bring the generated control to the next line. I cannot replicate the same br after a form submit. When I use a HtmlGenericControl to produce the br, it generates
. So I am getting an intermediate line between two controls during the re-creation. Is there any way to solve this?