By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,149 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,149 IT Pros & Developers. It's quick & easy.

programmaticaly invoke the button onclick method InvokeOnClick no

P: n/a
I have a form, has javascript registered so a modal pops up.
Button click will close form.
Now I need to do an update with modal form data before it closes.
I can put a second button and register the onclick attribute of the second
button
to the javascript to close the form after user clicks upload button
click upload do the update to database then programmatically click the
second button the use on onclick attribute that sees the registered
javascript and closes the modal
HOw do I programmatically invoke the button onclick method
found another thread that say use
this.InvokeOnClick(button1, EventArgs.Empty);
but my form does not compile says there is not definition for
InvokeOnClick.?????
--
cindy
Apr 24 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi Cindy,

Thank you for posting.

From your description, the problem you mentioned is within an ASP.NET web
application. And the page that contains the button will use some
client-side script to invoke the button's click operation, correct?

Based on my understanding , for client-side script, we can use
"document.getElementById(buttonid).click();" to invoke the certain button
element's click event. And as for server-side code, since the button's
click event handler is just a normal function, you can just call it in your
certain function. What I'm not quite sure so far is the

"found another thread that say use this.InvokeOnClick(button1,
EventArgs.Empty); "

you mentioned, is it also specific to ASP.NET application or winform?

Please feel free to post here if there is anything I didn't quite get.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 25 '06 #2

P: n/a
public class frmAuthor : System.Web.UI.Page
{
//parent form with field for data entry has upload button,
//user clicks upload button first client script opens modal window
//user action in modal window causes the close of the modal window
//execution returns to parent in the CmdUpload button onclick event.
//this side is all good
protected void AddClientScript()
{
const string scriptName = "CALL_POPUP";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language =\"javascript\">");
sb.Append("\nfunction getData(checkBoxListId)");
sb.Append("\n{");
sb.Append("\nvar mystr = window.showModalDialog('PopFrame.aspx');");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
}
private void Page_Load(object sender, System.EventArgs e)
{
AddClientScript();
CmdUpload.Attributes["onClick"] = "getData('checkBoxListTest')";
}
private void InitializeComponent()
{
this.CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);
}

private void CmdUpload_Click(object sender, EventArgs e)
{
try {
//code to upload form data
}
}

}

the modal window
public class frmAttach : System.Web.UI.Page
{ //modal window is opened in iframe
//form has fields and 2 buttons, one is CmdUpload where data is processed
//after processing I want to programmatically invoke click of the cmdClose
//to call the javascript which closes the window.
//manually clicking the button works I want to automate this.
private void Page_Load(object sender, System.EventArgs e)
{ AddClientScript(); }
private void InitializeComponent()
{ CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);}
protected void AddClientScript()
{
string scriptName = "FIND_SELECTED_ITEM";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language=\"javascript\">");
sb.Append("\nfunction Item_Selected()");
sb.Append("\n{");
sb.Append("\nwindow.close();");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
CmdClose.Attributes["OnClick"] = "Item_Selected();return false;";
}
private void CmdUpload_Click(object sender, EventArgs e)
{
//code to upload form data

//in this forum there is a thread on programmatically
//invoking the onclick method and they say to use code below
//but it does not compile for me I get no definition for InvokeOnClick
//this is not compact application
this.InvokeOnClick(this.CmdClose, EventArgs.Empty);
}
}
}
--
cindy
"Steven Cheng[MSFT]" wrote:
Hi Cindy,

Thank you for posting.

From your description, the problem you mentioned is within an ASP.NET web
application. And the page that contains the button will use some
client-side script to invoke the button's click operation, correct?

Based on my understanding , for client-side script, we can use
"document.getElementById(buttonid).click();" to invoke the certain button
element's click event. And as for server-side code, since the button's
click event handler is just a normal function, you can just call it in your
certain function. What I'm not quite sure so far is the

"found another thread that say use this.InvokeOnClick(button1,
EventArgs.Empty); "

you mentioned, is it also specific to ASP.NET application or winform?

Please feel free to post here if there is anything I didn't quite get.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 25 '06 #3

P: n/a
Thank your for the response Cindy,

Well, I think I've got your appliation's code logic. You want to let the
dialog web page's server-side code to call a method in the opener page's
server-side code, yes? If this is the case, I'm afraid this could not be
done because the ASP.NET pages have no sense of other pages at server-side,
each page's server-side lifecycle is based on a request from client to
server. So we can not call make one page to call server-sdie function in
another page.

Also, the "InvokeOnClick" method is a protected method of the winform
Control class rather than ASP.NET control, so this approach is apparently
incorrect.

Based on my experience, if you want to make the two pages do some
communication at server-side, you may consider let one page store some data
or info in the SessionState and then the other page can access that session
state to get the updated value.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Apr 26 '06 #4

P: n/a
Almost. The parent window is just using javascript to open a modal window.
In the modal window is a form with 3 fields and 2 buttons.
The first button uploads the form fields to database.
The second button is tied to the javascript that I have registered. It says
just
close the modal window this is because I want to automate closing the window.
I want the user to click the first button it says upload, move the data into
the
database and then have the window close. So after the code executes in the
upload onclick method I want to programmatically click the second button
You see when the page loaded I tied the onclick method of the button to the
javascript to close the modal window.

the modal window
public class frmAttach : System.Web.UI.Page
{ //modal window is opened in iframe
//form has fields and 2 buttons, one is CmdUpload where data is processed
//after processing I want to programmatically invoke click of the cmdClose
//to call the javascript which closes the window.
//manually clicking the button works I want to automate this.
private void Page_Load(object sender, System.EventArgs e)
{ AddClientScript(); }
private void InitializeComponent()
{ CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);}
protected void AddClientScript()
{
string scriptName = "FIND_SELECTED_ITEM";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language=\"javascript\">");
sb.Append("\nfunction Item_Selected()");
sb.Append("\n{");
sb.Append("\nwindow.close();");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
CmdClose.Attributes["OnClick"] = "Item_Selected();return false;";
}
private void CmdUpload_Click(object sender, EventArgs e)
{
//upload form data and then programmatically click CmdClose

--
cindy
"Steven Cheng[MSFT]" wrote:
Thank your for the response Cindy,

Well, I think I've got your appliation's code logic. You want to let the
dialog web page's server-side code to call a method in the opener page's
server-side code, yes? If this is the case, I'm afraid this could not be
done because the ASP.NET pages have no sense of other pages at server-side,
each page's server-side lifecycle is based on a request from client to
server. So we can not call make one page to call server-sdie function in
another page.

Also, the "InvokeOnClick" method is a protected method of the winform
Control class rather than ASP.NET control, so this approach is apparently
incorrect.

Based on my experience, if you want to make the two pages do some
communication at server-side, you may consider let one page store some data
or info in the SessionState and then the other page can access that session
state to get the updated value.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Apr 26 '06 #5

P: n/a
Thanks for the response cindy,

IMO, for such scenario, the main flow of the dialog and opener page should
be as below:

In Model dialog page, the user upload the file and then the page register
some client-script and postback, in the script, if necessary, we can use
script to invoke another button's postback event(then do some additional
work at server-side). After that, still register some script to
programmatically close the model dialog itself.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Apr 27 '06 #6

P: n/a
Unfortunately you are only validating the flow not giving any example of how
to invoke the CmdClose onclick method programmatically. I have registered see
the code some client script to close the form. I have tied that script to
CmdClose button if I manually click the button the modal will close. The
code I have in the CmdUpload button works great, the form data gets uploaded.
Please help me now with the next part. How do I invoke the CmdClose button
onClick method after completion of the upload, for example
cmdUpload onClick()
{
//data uploaded
// how do I force
cmdClose.CLICK(); //?????????????????????????????????????????/
}
--
cindy
"Steven Cheng[MSFT]" wrote:
Thanks for the response cindy,

IMO, for such scenario, the main flow of the dialog and opener page should
be as below:

In Model dialog page, the user upload the file and then the page register
some client-script and postback, in the script, if necessary, we can use
script to invoke another button's postback event(then do some additional
work at server-side). After that, still register some script to
programmatically close the model dialog itself.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Apr 27 '06 #7

P: n/a
Thanks for your response Cindy,

All these should be done through client-side script, here is a simple
dialog page's markup and codebehind(registering the client-script
dynamically):

=========aspx===========

<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" /></div>
</form>
</body>
======codebehind function=========

protected void btnUpload_Click(object sender, EventArgs e)
{
Response.Write("<br/>FileUploaded............");

string script = @"<script language='javascript'>
document.getElementById('btnSubmit').click();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"btnSubmit", script);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<br/>btnSubmit_Click.............");

string script = @"<script language='javascript'>

window.close();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"closedialog", script);
}
==========================

Javascript is case sensitive, so we should use "close()" rather than
"CLOSE()".

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 28 '06 #8

P: n/a
First off so I am not missing it these are the only relevant lines in the
modal form.
WHY is this so difficult to get syntax?

protected void btnUpload_Click(object sender, EventArgs e)
{ ////???? all code inside click procedure??????

///first do some stuff like execute non query

/////// next line inside click procedure

string script = @"<script language='javascript'>
document.getElementById'btnSubmit').click();
</script>";

////OK there is Page.ClientID and Page.ClientTarget NO Page.ClientScript
// I am using C#

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"btnSubmit", script);

///////so no compile
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
///again all inside click procedure, do nothing
// I just want to close so no Response.write???????
Response.Write("<br/>btnSubmit_Click.............");

string script = @"<script language='javascript'>
window.close(); </script>";

////There is NO Page.ClientScript

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"closedialog", script);
}

--
cindy
"Steven Cheng[MSFT]" wrote:
Thanks for your response Cindy,

All these should be done through client-side script, here is a simple
dialog page's markup and codebehind(registering the client-script
dynamically):

=========aspx===========

<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" /></div>
</form>
</body>
======codebehind function=========

protected void btnUpload_Click(object sender, EventArgs e)
{
Response.Write("<br/>FileUploaded............");

string script = @"<script language='javascript'>
document.getElementById('btnSubmit').click();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"btnSubmit", script);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<br/>btnSubmit_Click.............");

string script = @"<script language='javascript'>

window.close();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"closedialog", script);
}
==========================

Javascript is case sensitive, so we should use "close()" rather than
"CLOSE()".

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 3 '06 #9

P: n/a
This does not work, Page.ClientScript does NOT compile.

<form id="form1" method="post" encType="multipart/form-data" runat="server">
<INPUT id="File1" type="file" name="File1" runat="server">
<asp:button id="btnUpload" Runat="server" Text="UpLoad"></asp:button>
<asp:Button id="btnSubmit" runat="server" Text="Close"></asp:Button>
</form>

public class frmAttach : System.Web.UI.Page
{

protected System.Web.UI.HtmlControls.HtmlForm form1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button btnUpload;
protected System.Web.UI.WebControls.Button btnSubmit;

private void Page_Load(object sender, System.EventArgs e)
{

}
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void btnUpload_Click(object sender, EventArgs e)
{
if (File1.PostedFile !=null) //Checking for valid file
{
/// do some upload thing
/// then I want to close the form, just execute btnSubmit
}
string script = @"<script
language='javascript'>document.getElementById('btn Submit').click();</script>";
Page.ClientScript.RegisterStartupScript(this.GetTy pe(), "btnSubmit",
script);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
////just close
string script = @"<script language='javascript'>window.close();</script>";
Page.ClientScript.RegisterStartupScript(this.GetTy pe(),"closedialog",
script);
}
}

--
cindy
"Steven Cheng[MSFT]" wrote:
Thanks for your response Cindy,

All these should be done through client-side script, here is a simple
dialog page's markup and codebehind(registering the client-script
dynamically):

=========aspx===========

<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" /></div>
</form>
</body>
======codebehind function=========

protected void btnUpload_Click(object sender, EventArgs e)
{
Response.Write("<br/>FileUploaded............");

string script = @"<script language='javascript'>
document.getElementById('btnSubmit').click();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"btnSubmit", script);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<br/>btnSubmit_Click.............");

string script = @"<script language='javascript'>

window.close();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"closedialog", script);
}
==========================

Javascript is case sensitive, so we should use "close()" rather than
"CLOSE()".

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 3 '06 #10

P: n/a

private void Page_Load(object sender, System.EventArgs e){
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e){
InitializeComponent();
base.OnInit(e); }
private void InitializeComponent(){
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.Load += new System.EventHandler(this.Page_Load);}
#endregion
private void btnUpload_Click(object sender, EventArgs e) {
if (File1.PostedFile !=null) //Checking for valid file
{ //do something}

//please close form
string script = @"<script
language='javascript'>document.getElementById('btn Submit').click();</script>";
Page.RegisterClientScriptBlock("btnSubmit", script); }

private void btnSubmit_Click(object sender, EventArgs e) {
string script = @"<script language='javascript'> window.close(); </script>";
Page.RegisterClientScriptBlock("closedialog", script); }

So I got it to compile but I still get the error below

Microsoft JScript runtime error: 'document.getElementById(...)' is null or
not an object

see debug below
<HTML>
<HEAD>
</HEAD>
<BODY>
<form name="form1" method="post" action="frmAttach.aspx" id="form1"
enctype="multipart/form-data">
<input type="hidden" name="__VIEWSTATE"
value="dDwxODM3MjI5ODg3O3Q8O2w8a........" />
<script language='javascript'>document.getElementById('btn Submit').click();
--
cindy
"Steven Cheng[MSFT]" wrote:
Thanks for your response Cindy,

All these should be done through client-side script, here is a simple
dialog page's markup and codebehind(registering the client-script
dynamically):

=========aspx===========

<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" /></div>
</form>
</body>
======codebehind function=========

protected void btnUpload_Click(object sender, EventArgs e)
{
Response.Write("<br/>FileUploaded............");

string script = @"<script language='javascript'>
document.getElementById('btnSubmit').click();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"btnSubmit", script);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<br/>btnSubmit_Click.............");

string script = @"<script language='javascript'>

window.close();
</script>";

Page.ClientScript.RegisterStartupScript(this.GetTy pe(),
"closedialog", script);
}
==========================

Javascript is case sensitive, so we should use "close()" rather than
"CLOSE()".

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 3 '06 #11

P: n/a
Hey Cindy,

I'm sorry that I assumed that you're using ASP.NET 2.0, and the
"ClientScript" is a new property provided by the Page class in ASP.NET
2.0's built-in class library. If you're using 1.x, you can find the
identical functions directly under the Page class, which are used to
regiser client-side scripts dynamically.

After you can run server-side code corretly, what left is debugging the
client-side script, you can view the source in client browser to see
whether the output script is correct(javascript is case sensitive), also
make sure the id referenced in our script function does exists in page.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 4 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.