Code to "click" a button -- is this possible?

G

Guest

Hello

I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

is this possible?
thanks.
ben
 
G

Guest

Why not to use window.location.href and javascript for this ?
I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Ben said:
Hello

I have frames set up in an asp.net application and need one frame to
refresh
another. Seeing as events need to be registered at the time the page is
sent
from the server, I was wondering if I could place a hidden button in a
frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

is this possible?

you can refresh any of the frames using javascript, you can either Refresh
the frame, submit the form , or call a method to a control (like button
click ) these actions will refresh the form
 
G

Guest

could you please supply the code that can be called from the C# method that
will refresh another frame called "Main"?

thanks. I am not familiar with javascripting.

ben
 
G

Guest

Ben,

I would not be C# code. It would be javascript. Once the client receives
the HTML output from the ASP.NET page, C# no longer has any control (its all
on the client now). It is important to understand the distinction between
server-side and client-side which is inherit in all dynamic web applications.
The server sends HTML, CSS, and javascript (or jscript or vbscript) to the
client. The client (an Internet Browser, like IE or FireFox) then decides
what to do with the HTML, CSS, and script that was sent to it. One way to
look at is that the web server simply makes a suggestion to the browser. It
suggests it should render certain UI elements and run certain client-side
scripts. Its up to the browser to comply. It doesn't have to. The web
server can't control the actions of the browser. For instance, if a browser
has scripting disabled or isn't compliant with all the CSS that was sent to
it... then it will look and act very differently than it was designed to.
There are ways around this, but it involves detecting the capabilities of the
browser that made a request for the page.

Now, to answer your question. Here is the javascript used to reload a iframe:

<iframe name='content'></iframe>
<script language='javascript'>
parent.frames.item('content').location.href = 'mypage.aspx';
</script>
 
G

Guest

Use Page.FindControl() with the name of your control to get you button, cast
to the Button class and call .Click.Invoke method
Smth like (Page.FindControl("Button2") as Button).Click.Invoke(this, new
EventArgs());
thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
Michael Nemtsev said:
Why not to use window.location.href and javascript for this ?

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Nate
thank you for your reply

are you telling me there is no way that i can then refresh a specific frame
AFTER the server side code is run? that is what i require...ie a way of
calling the javascript you provided.

thanks.
 
G

Guest

Michael

thank you for the reply...this is looking like what i was trying to achieve.
I have tried the code and recieved the following error:

The event 'System.Web.UI.WebControls.Button.Click' can only appear on the
left hand side of += or -=

any suggestions?

thanks.
ben
Michael Nemtsev said:
Use Page.FindControl() with the name of your control to get you button, cast
to the Button class and call .Click.Invoke method
Smth like (Page.FindControl("Button2") as Button).Click.Invoke(this, new
EventArgs());
thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
Michael Nemtsev said:
Why not to use window.location.href and javascript for this ?

I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Not jut Click but Button2.Click.Invoke(...);

Ben said:
Michael

thank you for the reply...this is looking like what i was trying to achieve.
I have tried the code and recieved the following error:

The event 'System.Web.UI.WebControls.Button.Click' can only appear on the
left hand side of += or -=

any suggestions?

thanks.
ben
Michael Nemtsev said:
Use Page.FindControl() with the name of your control to get you button, cast
to the Button class and call .Click.Invoke method
Smth like (Page.FindControl("Button2") as Button).Click.Invoke(this, new
EventArgs());
thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
:

Why not to use window.location.href and javascript for this ?

I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Sorry, I guess I forgot to paste my exact code line aswell...here it is:

(Page.FindControl("btnTest") as Button).Click.Invoke(this, new EventArgs());

And I am recieving the error as described by my last post.

Thanks again for all the help! Hopefully this will be resolved soon.
Ben
 
G

Guest

Sorry, I guess I forgot to paste my exact code line aswell...here it is:

(Page.FindControl("btnTest") as Button).Click.Invoke(this, new EventArgs());

And I am recieving the error as described by my last post.

Thanks again for all the help! Hopefully this will be resolved soon.
Ben


Michael Nemtsev said:
Not jut Click but Button2.Click.Invoke(...);

Ben said:
Michael

thank you for the reply...this is looking like what i was trying to achieve.
I have tried the code and recieved the following error:

The event 'System.Web.UI.WebControls.Button.Click' can only appear on the
left hand side of += or -=

any suggestions?

thanks.
ben
Michael Nemtsev said:
Use Page.FindControl() with the name of your control to get you button, cast
to the Button class and call .Click.Invoke method
Smth like (Page.FindControl("Button2") as Button).Click.Invoke(this, new
EventArgs());

thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
:

Why not to use window.location.href and javascript for this ?

I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Check that you already have the button's handler.
Because you are trying to call non-exited button's handler;

Ben said:
Sorry, I guess I forgot to paste my exact code line aswell...here it is:

(Page.FindControl("btnTest") as Button).Click.Invoke(this, new EventArgs());

And I am recieving the error as described by my last post.

Thanks again for all the help! Hopefully this will be resolved soon.
Ben


Michael Nemtsev said:
Not jut Click but Button2.Click.Invoke(...);

Ben said:
Michael

thank you for the reply...this is looking like what i was trying to achieve.
I have tried the code and recieved the following error:

The event 'System.Web.UI.WebControls.Button.Click' can only appear on the
left hand side of += or -=

any suggestions?

thanks.
ben
:

Use Page.FindControl() with the name of your control to get you button, cast
to the Button class and call .Click.Invoke method
Smth like (Page.FindControl("Button2") as Button).Click.Invoke(this, new
EventArgs());

thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
:

Why not to use window.location.href and javascript for this ?

I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Michael

The error I am getting is a compile time error and not a runtime error. I'm
using VS2003 (C#).

I hate to keep bugging you, but I don't have an idea on how to fix this.

Thanks.

Michael Nemtsev said:
Check that you already have the button's handler.
Because you are trying to call non-exited button's handler;

Ben said:
Sorry, I guess I forgot to paste my exact code line aswell...here it is:

(Page.FindControl("btnTest") as Button).Click.Invoke(this, new EventArgs());

And I am recieving the error as described by my last post.

Thanks again for all the help! Hopefully this will be resolved soon.
Ben


Michael Nemtsev said:
Not jut Click but Button2.Click.Invoke(...);

:

Michael

thank you for the reply...this is looking like what i was trying to achieve.
I have tried the code and recieved the following error:

The event 'System.Web.UI.WebControls.Button.Click' can only appear on the
left hand side of += or -=

any suggestions?

thanks.
ben
:

Use Page.FindControl() with the name of your control to get you button, cast
to the Button class and call .Click.Invoke method
Smth like (Page.FindControl("Button2") as Button).Click.Invoke(this, new
EventArgs());

thank you for your reply.
first...im new to web development so i do not understand all the concepts.

the application im creating has 2 frames. the left is a treeview menu and
the right shows the data. When a user navigates through the right frame, i
have code to update the left frame:

private void dgGroup_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string id = "g" + e.Item.Cells[3].Text;
e.Item.Cells[2].Attributes.Add("onclick", "if(syncNode('" + id +
"')){}else{return false}");
}

however, another control on the same page would be to delete the item (and
hense, delete the item from the leftfram aswell). If i use this same
principle to refresh the left frame, it occurs BEFORE the item gets deleted.
I need the refresh to occur after. Hense why I was looking to see if i can
programmically click a button from my C# code to fire the onclick event of
the button.

I hope this clears it up a little, i need this to work. thanks.

ben
:

Why not to use window.location.href and javascript for this ?

I have frames set up in an asp.net application and need one frame to refresh
another. Seeing as events need to be registered at the time the page is sent
from the server, I was wondering if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Seems that you don't have event handler binding to you btnTest

Check InitializeComponent() function for the lines below
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);

this line creates eventHandler and binds btnTest_Click function as handler
of you button.
And exactly this function is called by (Page.FindControl("btnTest") as
Button).Click.Invoke(this, new EventArgs());
The error I am getting is a compile time error and not a runtime error. I'm
using VS2003 (C#).

I hate to keep bugging you, but I don't have an idea on how to fix this.

Thanks.
--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI,

Ben said:
Nate
thank you for your reply

are you telling me there is no way that i can then refresh a specific
frame
AFTER the server side code is run? that is what i require...ie a way of
calling the javascript you provided.

Yes, but not by the server but by the client. in the server you insert code
that will be executed in the client to force a refresh:

like
in aspx page:
<body runat="server" id=Thebody>

in the .cs:
protected HtmlgenericControl theBody;

theBody.Attributes.Add("onLoad","the code to refresh the frame OR a method
to call that does it");
 
G

Guest

Hellow again

Yup. the event handler has been initialized for the btnTest.

I feel like this is becoming a lost cause...
 
G

Guest

Post you working code demonstrating the problem.
Hellow again
Yup. the event handler has been initialized for the btnTest.
I feel like this is becoming a lost cause...

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Sorry it took so long for the reply, but here is the code:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.btnTest.Attributes.Add("onclick", "if(confirm('Are you sure you
want to update the
grouping?')){javascript:parent.menu.location.reload(true);}else{return
false}");
(Page.FindControl("btnTest") as Button).Click.Invoke(this, new EventArgs());

}
}
 
G

Guest

heh,

your onClick code (confirm message) is javascript one, and you are trying to
call client code from the server one by Click.Invoke and that's why you got
such error that you post in previous messages - you server-side environment
doesn't see the handler for you button.

this.btnTest.Attributes.Add doesn't create handler for you server control,
it just set button's property for the client, nothing else.

In this situation you need to choose the side you are trying to keep either
client or server, but not mixing both.
Sorry it took so long for the reply, but here is the code:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.btnTest.Attributes.Add("onclick", "if(confirm('Are you sure you
want to update the
grouping?')){javascript:parent.menu.location.reload(true);}else{return
false}");
(Page.FindControl("btnTest") as Button).Click.Invoke(this, new EventArgs());

}
}

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

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