LindyGeek
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?
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>)
}
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.
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 ;)
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.
Still, I am curious as to why you would want that behavior.
ASKER
@danirk2 What is csname1 ?
@lenordiste I'm working with legacy code and taking the most hands-off approach I know how.
@lenordiste I'm working with legacy code and taking the most hands-off approach I know how.
ASKER
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
I have 3 parms : type, key, script
and 4 parms: + bool addScriptTags
Sorry, you are right.
Just Page.ClientScript.Register StartupScr ipt(this.G etType(), "JustClickit", cmd , true);
Just Page.ClientScript.Register
ASKER
It is not currently working. I am calling the following code from Page_Load
string cmd = "document.getElementById(' submit').c lick();";
Page.ClientScript.Register StartupScr ipt(this.G etType(), "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
string cmd = "document.getElementById('
Page.ClientScript.Register
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
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
example:
String cmd = "document.getElementById('
Page.ClientScript.Register