Programmatically Submit in asp.net?

J

Joe

Is the only way to programatically submit a html form to use vbscript or javascript? ie:

<script
function __doPostBack(eventTarget, eventArgument)
var theform = document.WebForm3
theform.__EVENTTARGET.value = eventTarget
theform.__EVENTARGUMENT.value = eventArgument
theform.submit()


</script
In pure C#...

#1 Is there no object to do this

#2 Can you programmatically (except for above) add items to the form collection / params collection

j
 
C

CMA

i can only provide an answer to the first one.
since C# can use only in server side, u dont have a way to use it to submit
a form...

CMA


Joe said:
Is the only way to programatically submit a html form to use vbscript or javascript? ie:

<script>
function __doPostBack(eventTarget, eventArgument) {
var theform = document.WebForm3;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}

</script>
In pure C#....

#1 Is there no object to do this?

#2 Can you programmatically (except for above) add items to the form
collection / params collection ?
 
J

Jeffrey Tan[MSFT]

Hi joedirt,

Thank you for posting in the community!

Based on my understanding, you have 2 questions about the form submit.

=================================================
Yes, to submit a form programatically, you must use Form.submit() method.

For your first question, I think you must have some concept
misunderstanding of Asp.net.

In asp.net, there are 2 types of script code: server side(With runat=server
attribute) and client side.

The server side code, it will execute at server side, and you can use C#,
VB, jscript and other languages to write. It is compile execute.
While the client side script will execute at client side, and you only can
use Javascript(or Jscript) and VBscript. It is interpret execute.

To submit a form, normally, you mean submit the entire form from the client
side to the server side, so it is client script. So you can not use C# to
write code.(The client script engine can not recognize the C# code)

For your #2, you can add programmatically add html element data into the
post that is posted back to the server side.(Normal ASP way)

Again, I will clarify the mechanism for you:
Asp.net introduces the concept of Server side web control. The webcontrol
encapsulates some of the behaviors of the html element. All the webcontrol
use the Form.submit() method to postback to the server side. It uses 2
hiden fields: __EVENTTARGET and __EVENTARGUMENT to store the data.

So it makes no sense for you to add the data into these 2 fields, because
at the server side, there are no server side objects to associate with your
data!

But you can programmatically add the data into the form data that is posted
back to the server side. Then at server side, you can use HttpResponse.Form
to access the added data.

Do like this:

At client side:

<INPUT type="button" value="Button" onclick="addformdata()">

<script language="javascript">
function addformdata()
{
var oNewNode = document.createElement("INPUT");
document.Form1.appendChild(oNewNode);
oNewNode.type="text";
oNewNode.name="addeditem";
oNewNode.value="abcdef";
document.Form1.submit();
}
</script>

At server side, you can write the got data into client side:

private void Page_Load(object sender, System.EventArgs e)
{
this.Response.Write(this.Request.Form["addeditem"]);
}

===============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Joe

Jeff, Thanks, that is what I thought. It is just frustrating to have to post, post, post. It is HTML/web standard I know. It would be a nice thought to let us put any object on the post event. ie: Let the browser/client send an object as one of the things on the form collection, opening up the forms collection to all objects.
 
J

Jeffrey Tan[MSFT]

Hi joedirt,

Thanks very much for your feedback.

Ok, you already know that Asp.net should use the PostBack to work. But I
want to correct you that this is not the HTML/web standard, this is the
mechanism of Asp.net.(Even not the feature of old Asp).

As you stated, you want to send OBJECT from the browser/client in the Form
Collection to the server side. Can you show me the exactly meanning of
"OBJECT"?
Because your "OBJECT" is at client side, I think it is not a server object
of Asp.net class instance. I think it means the client side html element.

Also, what is the exactly meanning of "Form Collection"? Does it mean the
server side web controls collection? If "Form Collection" does mean the
server side web control collection, Yes, you can not add customized client
side element into this "Form Collection", because the collection is
maintained at server side, you can not add into it from the client side.
I think you mean all the element data collection that is posted back to the
server side.

If "OBJECT" means the client side html element and "Form Collection" mean
all the element data collection that is posted back to the server side, I
think the javascript sample I provide you in orignal reply shows you how to
add textbox "OBJECT" into "Form Collection". And also, you can opening up
the forms collection to all objects.

If I fully misunderstand your meanning, please feel free to tell me, then I
think you need to explain some of these words for me:
1). form collection
2). object
3). HTML/web standard

With clearing the meanning of these words, I think I can fully understand
you.

Cheers!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi joedirt,

Does my reply make sense to you?

If you have anything unclear, please feel free to tell me, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top