Link to home
Start Free TrialLog in
Avatar of LindyGeek
LindyGeekFlag for Afghanistan

asked on

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

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

Avatar of danirk2
danirk2

Use ClientScriptManager..::.RegisterStartupScript

example:

String cmd = "document.getElementById('submit').click();";
      Page.ClientScript.RegisterStartupScript(this.GetType(), "JustClickit",  csname1, cmd , true);
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.
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 ;)
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.
Avatar of LindyGeek

ASKER

@danirk2    What is csname1 ?


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

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
Sorry, you are right.

Just Page.ClientScript.RegisterStartupScript(this.GetType(), "JustClickit",  cmd , true);
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
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?
ASKER CERTIFIED SOLUTION
Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!