?
Solved

Line is not executed

Posted on 2012-08-14
33
Medium Priority
?
296 Views
Last Modified: 2012-08-24
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
0
Comment
Question by:HuaMinChen
  • 16
  • 10
  • 7
33 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38291100
Looks like an error with your connection string. Why do you have a login name and password when you are using Integrated Security?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38291122
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
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38294233
Any advice to this? Thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:HuaMinChen
ID: 38294533
Why can't the 1st line inside the event be executed before any other issues?

Many Thanks & Best Regards,
HuaMin
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38295055
And if you debug to that point what do you notice about execution on that line does the debugger get there?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38295078
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?
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38295159
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?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38295162
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?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38295498
Any advice to this?
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38295637
So when the button is clicked you want to show a javascript message in the browser - is that correct?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38296350
Yes
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38296551
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
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38299547
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.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38299727
Thanks all. Cluskitt, oes the other button reside on one other page or not?
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38299798
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.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38303246
Thanks a lot Cluskitt. Do you have a way to directly show a message within the button-clicked event?

Many Thanks & Best Regards,
HuaMin
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38303710
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
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38303796
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
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38303873
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()")
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38303889
I mean I want to show that line I mentioned as the 1st line in the button-clicked event.

Many Thanks & Best Regards,
HuaMin
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38303900
Is the JQuery library included in your page?

That is required for that code to run.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38303930
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?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38303935
Julian,
What should be adjusted above to use Jquery?

Cluskitt,
Do you think it is troublesome to have extra page for showing that?
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38303956
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.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38303987
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
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38304010
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

0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38304076
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
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38304129
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.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38304131
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.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38304149
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?
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38304165
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.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 38304187
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?
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38304264
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question