Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Line is not executed

Hi,
I see that such line which is 1st line inside the relevant event, is not fired when the button is pressed
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "tes0", true);
            ...
         }

why?
I don't know why I get these messages when the button is clicked and the click event is actually the one I mentioned in the above.
http://dl.dropbox.com/u/40211031/t145.png

or

https://skydrive.live.com/#cid=17EC75244BAC022F&id=17EC75244BAC022F!225

And here is the connection string within Web.config
    <connectionStrings>
        <add name="Mssqlconn" connectionString="Data Source=.\192.168.168.1\ss2012;Initial Catalog=web_schema;Integrated Security=True;User ID=ws_login;Password=mypass" providerName="System.Data.SqlClient"/>
    </connectionStrings>

Can you please tell me, what really has been executed by Web developer itself before the above click event after I've clicked the relevant button?

Many Thanks & Best Regards,
HuaMin
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Chan

ASKER

Thanks. Even if I've removed this
Integrated Security=True

in the connection and then have rerun the project , I am still getting the same. Any advice to adjust the project?

Many Thanks & Best Regards,
HuaMin
Any advice to this? Thanks.
Why can't the 1st line inside the event be executed before any other issues?

Many Thanks & Best Regards,
HuaMin
And if you debug to that point what do you notice about execution on that line does the debugger get there?
Thanks. I tried by having a breakpoint on this line and have seen the control did go through it. But why didn't I see any output from such line?
I don't understand what you are trying to do.

Take a look here http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx 

You are inserting a script "tes0"

Also you doing a registerstartupscript in response to a button click - and not in the page load?
Thanks.I want to show a message by that line, to see how the control goes through the codes. If such line is wrong, how to show a message there?
Any advice to this?
So when the button is clicked you want to show a javascript message in the browser - is that correct?
Yes
Take a look at these links

http://www.dotnetspider.com/forum/172420-how-call-javascript-button-click.aspx
http://stackoverflow.com/questions/5855156/where-to-put-jquery-code-in-a-web-content-form-and-how-to-make-it-work

Depending on how you want to do it - the first link shows you how to add an onclick event to the button (there are two alternatives)

The second is how to include a JQuery script to do similar
I've had the same problem in the past. The thing is, if the script doesn't exist at page load, it won't be executed. Since button click occurs after page load, you can't add it that way.

I assume that you want to run some code and then show the messagebox. In which case, a simple javascript code won't work. It works independently of your code behind. The only way I've found to get something like this working is to create a chain of buttons. That is, on button click I show another button and link it to javascript. I'm sure there's a better solution, but I failed to find it within a reasonable time, so I just stuck with this.
Thanks all. Cluskitt, oes the other button reside on one other page or not?
No. What I did was something like this (my case was to open a new page):
-Declare two buttons in aspx, one visible (btn1), one not visible (btn2).
-On button click of the btn1, I run my code, then I add:
btn2.Attributes.Add("onclick","window.open('MyUrlToOpen.aspx')")
(this is VB.Net, btw)

This causes the user to have to press 2 buttons. To make it look a little better, I added a third button and one opened a new window, the other redirected to the same window.

Now, I know that there must be a way to open a messagebox after you run the code behind. But I have no idea how to do that. So I tried a workaround which isn't as fancy, but works.

One thing you might want to test, which just occurred to me, is to declare the script at page load. Then, when you run it on button click, it might work. You may have to fiddle with it, though, to ensure it won't run at page load as well.

One other thing you might want to test is to declare the function on aspx, then call it from code behind.
Thanks a lot Cluskitt. Do you have a way to directly show a message within the button-clicked event?

Many Thanks & Best Regards,
HuaMin
Put a content container on your page and then add the JScript directly to that
<script type="text/javascript">
$(function() {
  $('#id_of_your_button').click(function(e) {
    alert("Your message here");
  });
});
</script>

Open in new window

Refer this link http://www.dotnetcurry.com/ShowArticle.aspx?ID=274

Specifically point 4
Thanks. I put these to the Markup page
    <title>My Page</title>
    <script type="text/javascript">
        $(function() {
          $('#tb_userid').click(function(e) {
            alert("testing");
          });
        });
    </script>
    ...

But I don't see any prompt message after I've clicked the button while this below is the 1st line in the button-clicked event.
ScriptManager.RegisterStartupScript(Page, this.GetType(), "msgbox", "alert('test1')", true);

Many Thanks & Best Regards,
HuaMin
If you just want it as the first thing to happen, despite other code going on, just add this to page load:
Button1.Attributes.Add("onclick","alert('test1')")
or this to the aspx:
<asp:Button ID="Button1" runat="server" OnClientClick="alert('test1')" />

This will cause the messagebox to show and then the code behind to run. If you want to make a yes no thing, create a function:
  <script type="text/javascript">
    function isconfirm() {
      var ck = confirm('Tem a certeza que deseja eliminar o registo?');
      if (ck == true)
        return true;
      else
        return false;
    }
  </script>
Then, either:
OnClientClick="return isconfirm()"
or
Button1.Attributes.Add("onclick","return isconfirm()")
I mean I want to show that line I mentioned as the 1st line in the button-clicked event.

Many Thanks & Best Regards,
HuaMin
Is the JQuery library included in your page?

That is required for that code to run.
If you just want to show a static message on button click, you can do as I first suggested. Simply have two code behind's running. Client code behind (javascript) will always run first. The fact that it's separated from the actual VB/C# makes no difference. If, however, you want to show some variable content on the messagebox, that is more complicated. Did you try all the above suggestions?
Julian,
What should be adjusted above to use Jquery?

Cluskitt,
Do you think it is troublesome to have extra page for showing that?
Either include this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Or go to jquery.com and download the script file there and included it.
I have these now
    ...
    <title>My Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript">
        $(function() {
          $('#tb_userid').click(function(e) {
            alert("testing");
          });
        });
    </script>
    ...

But it is the same that I don't see any prompt message after I've clicked the button while this below is the 1st line in the button-clicked event.
ScriptManager.RegisterStartupScript(Page, this.GetType(), "msgbox", "alert('test1')", true);

Many Thanks & Best Regards,
HuaMin
Do it like this rather
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<!-- keep the loading of the library separate from the code below -->
<script type="text/javascript">
        $(function() {
          $('#tb_userid').click(function(e) {
            alert("testing");
          });
        });
</script>

Open in new window

I have these
    ...
    <title>My Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
          $('#tb_userid').click(function(e) {
            alert("testing");
          });
        });
    </script>
    ...

But it is the same that I don't see any prompt message after I've clicked the button while this below is the 1st line in the button-clicked event.
ScriptManager.RegisterStartupScript(Page, this.GetType(), "msgbox", "alert('test1')", true);

Many Thanks & Best Regards,
HuaMin
Then the id you are using for the click event does not match the button you are linking it to

And the button_click event is not going to do anything because that is executed on the server.
Just ignore that first line. Do this on page load:
Button1.Attributes.Add("onclick","alert('test1')")
or this to the aspx:
<asp:Button ID="Button1" runat="server" OnClientClick="alert('test1')" />

Then just comment or delete that script line. This will cause a messagebox to show up and the code to run.
Julian,
"And the button_click event is not going to do anything because that is executed on the server. "

For this above, does it mean I can't expect to show that inside my event originally showed?

Cluskitt,
Is there no way for that I expect to show the prompt within my original event?
The event is the same. It's still a button click. There are two phases to button click (or any event):
1- Client side code (if any)
2- Server side code (if any)

Even if both are written in different places, they're still both the original event.
If you want the messagebox to show from server side code behind, you can't. All you can do is as has been suggested.
Cluskitt,
So what I expect to see by the original event to show the message in the client side, is not the proper approach, is it?
You approach would be good for a desktop application. For a web application, things are much more (needlessly, IMO) complicated. The proper approach would be to split between client and server code, like as been suggested. If you need to use variables on your messagebox, or a dynamic message, there are other ways to achieve it as well. However, what you want can't be done in the way you're trying. You can't just keep the code all in one place and expect it to be executed on both places (client and server) at the same time.