programmaticaly invoke the button onclick method InvokeOnClick no

G

Guest

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.?????
 
S

Steven Cheng[MSFT]

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.)
 
G

Guest

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);
}
}
}
 
S

Steven Cheng[MSFT]

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.)
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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.)
 
G

Guest

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(); //?????????????????????????????????????????/
}
 
S

Steven Cheng[MSFT]

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.GetType(),
"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.GetType(),
"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.)
 
G

Guest

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.GetType(),
"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.GetType(),
"closedialog", script);
}
 
G

Guest

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('btnSubmit').click();</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "btnSubmit",
script);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
////just close
string script = @"<script language='javascript'>window.close();</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(),"closedialog",
script);
}
}
 
G

Guest

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('btnSubmit').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('btnSubmit').click();
 
S

Steven Cheng[MSFT]

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top