Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 simulate button click event

Author  Topic 

alpoor
Starting Member

29 Posts

Posted - 2005-10-24 : 12:38:33
I have two buttons in a webform and two separate button click events. I have to perform both button click events on one button click event. How can I simulate button click event in asp.net. I tried with javascript code using response.write("window.document.formname.bt2.click();"). But it's giving error

Can anybody have ideas???

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-10-24 : 13:16:02
Can you just call the event handler method for the 2nd button inside of the first button event handler and visa versa.

bool shouldCallOtherButtonEventHandler = true;

private void OnButton1_Click(object sender, EventArgs e)
{
if(shouldCallOtherButtonEventHandler == true)
{
shouldCallOtherButtonEventHandler = false;
OnButton2_Click(null, null);
}
//code for the first button event handler.
}

private void OnButton2_Click(sender, EventArgs e)
{
if(shouldCallOtherButtonEventHandler == true)
{
shouldCallOtherButtonEventHandler = false;
OnButton1_Click(null, null);
}

//code for the second button event handler
}
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-10-24 : 13:18:34
I just thought of a better alternative. Just wrap the code that gets executed for each button into a function and call both functions in the button click events.

private void OnButton1_Click(object sender, EventArgs e)
{
Button1Function();
Button2Function();
}

private void OnButton2_Click(sender, EventArgs e)
{
Button1Function();
Button2Function();
}
Go to Top of Page

alpoor
Starting Member

29 Posts

Posted - 2005-10-24 : 13:36:46
Thanks for reply. I actually tried both ways. I am getting following problem
"Collection was modified; enumeration operation may not execute"

Let me explain what I am trying to do in my application. I have two buttons in my webform. Once I click "button1", form displays datagrid with check box in one of the columns. User select one or more checkboxes in datagrid and click on other button "Button2" which basically deletes the selected records from the database. What I want here as soon as records deleted I want datagrid to be refreshed without deleted records.

I tried the following
response.write("<script language='JavaScript'>window.document.MainForm.bt.click()" + "<" + "/script>")
which is supposed to click the button1 to refresh my datagrid but it gave me javascript error

and I tried the following by opening other window closed immediately by executing following code which works great, but I don't want to open another window

response.write("<script language='JavaScript'>window.opener.document.MainForm.bt.click()" + "<" + "/script>")


Any suggestions
Go to Top of Page
   

- Advertisement -