How to assign Action property to the Form

  • Thread starter Thread starter Peter Afonin
  • Start date Start date
P

Peter Afonin

Hello,

In my application on the Webform I need to assign an action property to the
form in code, because the URL is a variable.

In MSDN there is an example - everything is simple:

Form1.Action=URL

However, this applies only to the Mobile controls. How can I do it in the
regular Webform?

I would appreciate your help.

Thank you,
 
Peter,

You can emit javascript code assigning action property to the form from
server side:

Response.Write("<script language='Javascript'>");
Response.Write(String.Format("myForm.action={0}",myUrl);
Response.Write("</script>");

This might not work since the document object model might not be built at
the loading time.
I think, the following should work:

Response.Write("<script language='Javascript'>");
Response.Write(String.Format("document.getElementById('myForm').action={0}",
myUrl);
Response.Write("</script>");

In any case, this should work always:

In the html view set <body onload="onLoad()"> and make onLoad() on fly as
Response.Write("<script language='Javascript'>");
Response.Write(String.Format("function onLoad(){myForm.action={0};}",myUrl);
Response.Write("</script>");

Eliyahu
 
Thank you very much, Eliyahu. I'll give it a try.

Peter

Eliyahu Goldin said:
Peter,

You can emit javascript code assigning action property to the form from
server side:

Response.Write("<script language='Javascript'>");
Response.Write(String.Format("myForm.action={0}",myUrl);
Response.Write("</script>");

This might not work since the document object model might not be built at
the loading time.
I think, the following should work:

Response.Write("<script language='Javascript'>");
Response.Write(String.Format("document.getElementById('myForm').action={0}",
myUrl);
Response.Write("</script>");

In any case, this should work always:

In the html view set <body onload="onLoad()"> and make onLoad() on fly as
Response.Write("<script language='Javascript'>");
Response.Write(String.Format("function onLoad(){myForm.action={0};}",myUrl);
Response.Write("</script>");

Eliyahu
 
You may wanna implement a Front Controller pattern on the server. My personal
opinion, that injecting a client code that would (potentially) expose your
inner logic to a client is a bad idea.

So, refer to Microsoft Enterprise Solution Patterns book (obtained online)
for implementation of the mentioned pattern (again, Front Controller).
Basically, the idea is to first, determine, what type of client you're
dealing with, and then using server.transfer() redirect the execution to a
more proper page/logic.

Hope this would be helpful for your work.

Andrew.

Eliyahu Goldin said:
Peter,

You can emit javascript code assigning action property to the form from
server side:

Response.Write("<script language='Javascript'>");
Response.Write(String.Format("myForm.action={0}",myUrl);
Response.Write("</script>");

This might not work since the document object model might not be built at
the loading time.
I think, the following should work:

Response.Write("<script language='Javascript'>");
Response.Write(String.Format("document.getElementById('myForm').action={0}",
myUrl);
Response.Write("</script>");

In any case, this should work always:

In the html view set <body onload="onLoad()"> and make onLoad() on fly as
Response.Write("<script language='Javascript'>");
Response.Write(String.Format("function onLoad(){myForm.action={0};}",myUrl);
Response.Write("</script>");

Eliyahu
 
Thank you, Andrew.

Peter

Andrew_Revinsky said:
You may wanna implement a Front Controller pattern on the server. My personal
opinion, that injecting a client code that would (potentially) expose your
inner logic to a client is a bad idea.

So, refer to Microsoft Enterprise Solution Patterns book (obtained online)
for implementation of the mentioned pattern (again, Front Controller).
Basically, the idea is to first, determine, what type of client you're
dealing with, and then using server.transfer() redirect the execution to a
more proper page/logic.

Hope this would be helpful for your work.

Andrew.
 
Back
Top