Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simulating a "click" via c# in aspx.net

Posted on 2009-12-22
11
Medium Priority
?
812 Views
Last Modified: 2012-05-08
I'm loading up an html page via .aspx if certain conditions are met, I want to automatically (w/o user interaction) click the Submit Button on the page (which is <input name = "submit")  

What Code do I put inside the if statement below?
string html = getHtmlStream("file.htm");  // gets the contents of file and puts them in a single string
Response.Write(html);

if (isMyConditionMet)
{
   // there is a form on "file.htm"
   // here is where I'd like the code to auto click the 
   // "submit button"  (<input name = submit>)
}

Open in new window

0
Comment
Question by:LindyGeek
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 5

Expert Comment

by:danirk2
ID: 26107626
Use ClientScriptManager..::.RegisterStartupScript

example:

String cmd = "document.getElementById('submit').click();";
      Page.ClientScript.RegisterStartupScript(this.GetType(), "JustClickit",  csname1, cmd , true);
0
 
LVL 5

Expert Comment

by:danirk2
ID: 26107639
Hit the submit to early :)

How this works: ASP will add the script automatically to HTML code that downloads to the browser, who executes it when the page loads.
0
 
LVL 11

Expert Comment

by:lenordiste
ID: 26107689
you won't be able to raise the event programmatically without having a class derive from Button and override the OnClick() method to make it public (because OnClick() is a protected method).

What you can do though is simply call the method which is handling your button on click event.
for instance:

            //calling the handling method
            button1_Click(this, new EventArgs());

          //your button handling code
        private void button1_Click(object sender, EventArgs e)
        {
            //code
        }

However I would reconsider your scenario first as having to programmatically raise events on controls is often an indication that something could be made easier and simpler ;)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:lenordiste
ID: 26107708
danirk's answer will work, i did not realize you were working with injected html statements. So submitting via javascript will definitely work.

Still, I am curious as to why you would want that behavior.
0
 

Author Comment

by:LindyGeek
ID: 26107844
@danirk2    What is csname1 ?


@lenordiste   I'm working with legacy code and taking the most hands-off approach I know how.  

0
 

Author Comment

by:LindyGeek
ID: 26107873
On my Studio, I don't have a version of RegisterStartupScript with 5 arguments:  

I have 3 parms : type, key, script
and 4 parms:  + bool addScriptTags
0
 
LVL 5

Expert Comment

by:danirk2
ID: 26107915
Sorry, you are right.

Just Page.ClientScript.RegisterStartupScript(this.GetType(), "JustClickit",  cmd , true);
0
 

Author Comment

by:LindyGeek
ID: 26108246
It is not currently working.  I am calling the following code from Page_Load

        string cmd = "document.getElementById('submit').click();";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "JustClickit", cmd, true);


My browser shows the page as expected, but no submit action occurs.

Should the string in "cmd" show up in the browser's "view source" ?

Should I be calling this from somewhere other than Page_Load ?

Thanks
0
 

Author Comment

by:LindyGeek
ID: 26108530
I am calling the code from Page_Load.  

It also does not show up in the page source when the browser comes up.  

Submit never "auto clicks".  

Is this expected behavior?
0
 
LVL 12

Accepted Solution

by:
CB_Thirumalai earned 435 total points
ID: 26109083
Is Submit button a server-side control or a html control?  If you are using document.getElementById('submit'), it means the the controls id is 'submit'.  But I see you have only kept the name attribute.  In order for this to work,  the following should be met.

1. The HTML control should be there on the form with id="submit"
2. If you use a server-side control, then the ClientID property should be used to get the id using document.getElementById.  This can be done as,
         string cmd = "document.getElementById('" + submit.ClientID + "').click();";
        // where submit is a server-side button
        Page.ClientScript.RegisterStartupScript(this.GetType(), "JustClickit", cmd, true);
0
 

Author Closing Comment

by:LindyGeek
ID: 31669125
Thanks!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question