Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

POST to another page simulating click

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alhambra Eidos Kiquenet - 13 Feb 2008 14:38 GMT
Hi misters,

I want  to  submit form in a console application  to simulate user click
button to submit in browser
There are controls in page. Four TextBoxs and a Button (type submit).

And i have registered an event for button to handle the request:

void button_Click(object sender, EventArgs e)        
{              
Response.Write(userName.Text);  //or do something        
}

We can input data in TextBoxs, then click the button to submit it. The web
application will process the request

What i want to do is  do it in an application.(see code below)

  static void Main(string[] args)
       {

string url = "http://localhost/contact.aspx";
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

NameValueCollection values = new NameValueCollection();
values.Add("userName","xxx");
values.Add("email","xxx");
values.Add("company","xxx");
values.Add("comments","xxx");
values.Add("bSend","send");

byte[] r = client.UploadValues(url, "post", values);
      }

 
But the application only fire the Page_Load event and doesn't raise the
Button.Click event.

any suggestions?
thanks in advance, any help will be very appreciated, regards.

Signature

http://www.alhambra-eidos.es/web2005/index.html
www.kiquenet.net
http://www.setbb.com/putainformatica/viewtopic.php?p=843
www.trabajobasura.com/solusoft

bruce barker - 13 Feb 2008 16:55 GMT
to fire the button event you need to send two fields

1) the button name & value
2) "__VIEWSTATE" and a valid value

to get a valid viewstate to post, your console app needs to do a get of the
page and parse the html for the viewstate value. it can then use this value
for the next post.

note: you can not save the veiwstate for future use, you must ask for a
current value before the post.

-- bruce (sqlwork.com)

> Hi misters,
>
[quoted text clipped - 37 lines]
> any suggestions?
> thanks in advance, any help will be very appreciated, regards.
Alhambra Eidos Kiquenet - 13 Feb 2008 17:48 GMT
Thanks mister,
ok, I get the text of all page and parser it.

any HTML Parser about it (for get input VIEWSTATE, and another inputs...) ?

Regards.
Cowboy (Gregory A. Beamer) - 13 Feb 2008 20:29 GMT
I typed up a bit of an answer and planned on playing this one through and
then asked myself "what are you doing?" The reason is I thought through the
problem and could not see a logical reason why it HAD to be done this way.
The caps on HAD are very important. Let me illustrate.

First, let's look at classes. This could be a class library or a console
application; it really does not matter. But, here is a normal set of actions
on a class.

Class1 cls1 = new Class1();
cls1.InitialAction();
cls1.Save();

In windows forms or web forms, it is the same, only the input is coming from
user actions, so you have events that may hold the code for the actions (not
wise, as you should move business into its own classes, but it is doable in
.NET in both windows and web forms).

What you have asked is for it to work like this:

Class1 cls1 = new Class1();
Class2 cls2 = new Class2();

cls1.InitialAction();
cls2.SaveForClass1();

This is possible in a couple of situations

1. Class 1 Instantiates class 2
2. Class 2 is Instantiated with information from Class 1
3. Class 2 created class 1 (although this only works if
4. Class 2 is used as a link between data and business object (of which
class 1 is one)
5. There is a Class 3 that controls both Class 1 and Class 2

Many of the above are antipatterns. Although possible, they are not wise to
use. The web opens up additional possibilities, as it is stateless. In
theory, you can call any page (class) you desire with information from
another page (class). It is still an antipattern.

So, then I thought of reasons you might want to do this.

1. You have the same basic functionality in two pages if you do not.

Solution:
Move the save functionality into its own class and call from both location

2. You have the same bits on a form as well as same backend functionality

Solution:
Move both UI and code into a User Control. I would still considering moving
the actual business rules to a library anyway, but this is better than
posting to another page.

3. You want to show a confirmation

Solutions:

A) Forward after you save
B) Use Panel controls

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
> Hi misters,
>
[quoted text clipped - 36 lines]
> any suggestions?
> thanks in advance, any help will be very appreciated, regards.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.