Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. User control - how to access the element using javascript

User control - how to access the element using javascript

Scheduled Pinned Locked Moved JavaScript
javascripthelpcomtoolstutorial
25 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 7763270

    Bob, I am running out of ideas on how to code using this approach <%= (some expression) ? "style=\"display:none\"" : "" %> /> can you please help me with some code snippet?

    B Offline
    B Offline
    BobJanova
    wrote on last edited by
    #10

    Well you showed some code yourself using <%= ... %>; that just causes the ASP.net renderer to output something. There must be some expression which you use to determine whether the text field should be displayed in the rendered control when it is requested, unless you want it to either always be hidden or always shown when the page segment is loaded (remember, you're putting it in an UpdatePanel, so this includes whenever that panel is updated). If you're not familiar with the ? : syntax I suggest a quick visit to the documentation[^] (or if you have some books on any C family language you can find it in there too).

    A U 2 Replies Last reply
    0
    • A AspDotNetDev

      First, you should check if you really are getting a postback. Put this in your update panel:

      <%= Page.IsPostBack.ToString()%>

      It could be that you have some JavaScript that is clearing the textbox on page load. Or the textbox is re-getting focus on page load and you are typing over what is already in the textbox.

      Thou mewling ill-breeding pignut!

      U Offline
      U Offline
      User 7763270
      wrote on last edited by
      #11

      Hi, The idea of putting <%= Page.IsPostBack.ToString()%> on my usercontrol helped me realize that it is not the issue with the Text disappearing in the textbox. But the issue is that the page is getting post back twice and that is why the text in the text box is disappearing. When page is loaded for the first time the value of <%= Page.IsPostBack.ToString()%> is False and in few seconds a page post back is taking place all by itself and the value is true. I couldn't figure out what is causing the second post back yet. But when I searched online I found that there could be two reasons for posting back twice. 1) It may be because of any client side scripting either java script or jQuery that may be causing the post back. In my case I do not see any client side that is written to cause a post back. 2) The other reason for the second time post back could be that if any Image tags are used and then if no src attribute is provided then this might also cause a post back. In my case, we are using a default.master and I see quite a few tags are defined in the master page and I do not see either src or imageurl attribute being used in them. I just see that a skinID is being used. I am not sure if I need to use imageurl in all of my tags to fix the page post back issue Does any body have any inputs? Thanks in advance, L

      A B 2 Replies Last reply
      0
      • U User 7763270

        Hi, The idea of putting <%= Page.IsPostBack.ToString()%> on my usercontrol helped me realize that it is not the issue with the Text disappearing in the textbox. But the issue is that the page is getting post back twice and that is why the text in the text box is disappearing. When page is loaded for the first time the value of <%= Page.IsPostBack.ToString()%> is False and in few seconds a page post back is taking place all by itself and the value is true. I couldn't figure out what is causing the second post back yet. But when I searched online I found that there could be two reasons for posting back twice. 1) It may be because of any client side scripting either java script or jQuery that may be causing the post back. In my case I do not see any client side that is written to cause a post back. 2) The other reason for the second time post back could be that if any Image tags are used and then if no src attribute is provided then this might also cause a post back. In my case, we are using a default.master and I see quite a few tags are defined in the master page and I do not see either src or imageurl attribute being used in them. I just see that a skinID is being used. I am not sure if I need to use imageurl in all of my tags to fix the page post back issue Does any body have any inputs? Thanks in advance, L

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #12

        Wow, that is crazy. I would never have suspected that an empty src attribute in an image control could possibly cause a double postback. However, from what I read online, it is indeed possible. I recommend using something like Fiddler2 to intercept the HTML that the ASP.net gets rendered to. That way, you can search for all the image tags and see if any have strange src attribute values. Also, you might want to try disabling JavaScript in your browser. That way, you can exclude the possibility that there is any JavaScript causing the postback.

        Thou mewling ill-breeding pignut!

        U 5 Replies Last reply
        0
        • U User 7763270

          Hi, The idea of putting <%= Page.IsPostBack.ToString()%> on my usercontrol helped me realize that it is not the issue with the Text disappearing in the textbox. But the issue is that the page is getting post back twice and that is why the text in the text box is disappearing. When page is loaded for the first time the value of <%= Page.IsPostBack.ToString()%> is False and in few seconds a page post back is taking place all by itself and the value is true. I couldn't figure out what is causing the second post back yet. But when I searched online I found that there could be two reasons for posting back twice. 1) It may be because of any client side scripting either java script or jQuery that may be causing the post back. In my case I do not see any client side that is written to cause a post back. 2) The other reason for the second time post back could be that if any Image tags are used and then if no src attribute is provided then this might also cause a post back. In my case, we are using a default.master and I see quite a few tags are defined in the master page and I do not see either src or imageurl attribute being used in them. I just see that a skinID is being used. I am not sure if I need to use imageurl in all of my tags to fix the page post back issue Does any body have any inputs? Thanks in advance, L

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #13

          Heh, this kind of thing is why I never liked ASP.

          1 Reply Last reply
          0
          • A AspDotNetDev

            Wow, that is crazy. I would never have suspected that an empty src attribute in an image control could possibly cause a double postback. However, from what I read online, it is indeed possible. I recommend using something like Fiddler2 to intercept the HTML that the ASP.net gets rendered to. That way, you can search for all the image tags and see if any have strange src attribute values. Also, you might want to try disabling JavaScript in your browser. That way, you can exclude the possibility that there is any JavaScript causing the postback.

            Thou mewling ill-breeding pignut!

            U Offline
            U Offline
            User 7763270
            wrote on last edited by
            #14

            I think I am getting close to figure out what is causing the issue. I think it is the Java script that may be causing the issue. I just disable the java script (ttos/internet optionts/security and licked on custom level and disabled the Active scripting) and when I do this the value of page.ispostback doesn't change and it is always false when it loaded for the first and it doesn't change to true and the page doesn't post back. On our when we mouse over on our application tabs all the options slide down. But now that I disabled the javascript I am not seeing the options slide when I mouse over. So I had click on the tab that takes me to the page I am having page post issue with. When I enable the javascript all look good and it post back as usual. Now, how do I figure out which javascript function is causing the issue. Out application is really huge. So just to make sure I am thinking it right. Once the page, that I am having issue, loads I am thinking to view page source and try to find all the javascripts and try to find that is causing the post back. But what do I need to look in the view source for me to know which one causing the postback? I will keep at it and I need to fix it. Thanks, L

            1 Reply Last reply
            0
            • A AspDotNetDev

              Wow, that is crazy. I would never have suspected that an empty src attribute in an image control could possibly cause a double postback. However, from what I read online, it is indeed possible. I recommend using something like Fiddler2 to intercept the HTML that the ASP.net gets rendered to. That way, you can search for all the image tags and see if any have strange src attribute values. Also, you might want to try disabling JavaScript in your browser. That way, you can exclude the possibility that there is any JavaScript causing the postback.

              Thou mewling ill-breeding pignut!

              U Offline
              U Offline
              User 7763270
              wrote on last edited by
              #15

              I am looking at the page view source and found this javscript function function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } and I do see __doPostBack being called in so many instances. But all of those calls make sense to me say for instance it is being called like this openeing a tag.. id="ctl00_LoginStatusValue" class="headerLoginStatus" hre..="javascript:__doPostBack('ctl00$LoginStatusValue$ctl00','')">Log Out closing a tag and other instances are like for a dropdown onchange events.. like onchange="javascript:setTimeout('__doPostBack(\'ctl00$DefaultContent.... let us see what else causing it. I will keep posted when I find some thing new and or when I fixed the issue.

              1 Reply Last reply
              0
              • A AspDotNetDev

                Wow, that is crazy. I would never have suspected that an empty src attribute in an image control could possibly cause a double postback. However, from what I read online, it is indeed possible. I recommend using something like Fiddler2 to intercept the HTML that the ASP.net gets rendered to. That way, you can search for all the image tags and see if any have strange src attribute values. Also, you might want to try disabling JavaScript in your browser. That way, you can exclude the possibility that there is any JavaScript causing the postback.

                Thou mewling ill-breeding pignut!

                U Offline
                U Offline
                User 7763270
                wrote on last edited by
                #16

                I found this function on our default.master // function onCancel() { // __doPostBack('RedoPageLoad', ''); // } and commented this out but still it is still posting back.

                A 1 Reply Last reply
                0
                • B BobJanova

                  Well you showed some code yourself using <%= ... %>; that just causes the ASP.net renderer to output something. There must be some expression which you use to determine whether the text field should be displayed in the rendered control when it is requested, unless you want it to either always be hidden or always shown when the page segment is loaded (remember, you're putting it in an UpdatePanel, so this includes whenever that panel is updated). If you're not familiar with the ? : syntax I suggest a quick visit to the documentation[^] (or if you have some books on any C family language you can find it in there too).

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #17

                  The ? : syntax can't be used with VB.net, which is the language the OP is using. Instead, the OP would use the If (condition, option1, option2) syntax.

                  Thou mewling ill-breeding pignut!

                  B 1 Reply Last reply
                  0
                  • U User 7763270

                    I found this function on our default.master // function onCancel() { // __doPostBack('RedoPageLoad', ''); // } and commented this out but still it is still posting back.

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #18

                    According to this, you can use Request.Form("__EVENTTARGET") to see which control caused the postback. If that doesn't help, you have a couple options. For one, you could search the HTML source for "doPostBack" to see if there is anything funky that you missed. Also, you can use FireBug or the Chrome debugger to inspect all the JavaScript referenced by the page. If that doesn't get you anywhere, start removing things one by one until the second postback doesn't happen. That will help you narrow down what is causing the problem. And like I mentioned before, Fiddler2 is a great help for figuring this type of stuff out. It will show you all requests/responses, and all of the data being sent with them to/from the server. And I'm pretty sure you can use some setting to monitor local traffic (that is, web traffic from a Visual Studio web application that is being run locally).

                    Thou mewling ill-breeding pignut!

                    U 1 Reply Last reply
                    0
                    • A AspDotNetDev

                      Wow, that is crazy. I would never have suspected that an empty src attribute in an image control could possibly cause a double postback. However, from what I read online, it is indeed possible. I recommend using something like Fiddler2 to intercept the HTML that the ASP.net gets rendered to. That way, you can search for all the image tags and see if any have strange src attribute values. Also, you might want to try disabling JavaScript in your browser. That way, you can exclude the possibility that there is any JavaScript causing the postback.

                      Thou mewling ill-breeding pignut!

                      U Offline
                      U Offline
                      User 7763270
                      wrote on last edited by
                      #19

                      I kind of figured the issue. AS I have mentioned I have two User Controls , say UC-A and UC-B. The textBox that is being refreshed is located in UC-A and this is being used by UC-B and UC-B is used by a .aspx page so that is how it is structured. The UC-B has two DropDownList controls whose AutoPostBack property set to true. And programatically it is a must to set the focus to one of these DropDowlists. So when the page is loaded the DropDownList gets focus and when I try get the focus to the textBox to type in some thing then the DropDowlist is doing a post back as its AutoPostBack property has been set to true. So I am trying to figure out a way where, if I do not change the of the selectedIndex of the dropdown and just make its focus lost then do not do a post back. I think may be I can try to come up with a javascript function where I can nullify the post back in the javascript function. Any inputs if this sounds doable?

                      A 1 Reply Last reply
                      0
                      • A AspDotNetDev

                        Wow, that is crazy. I would never have suspected that an empty src attribute in an image control could possibly cause a double postback. However, from what I read online, it is indeed possible. I recommend using something like Fiddler2 to intercept the HTML that the ASP.net gets rendered to. That way, you can search for all the image tags and see if any have strange src attribute values. Also, you might want to try disabling JavaScript in your browser. That way, you can exclude the possibility that there is any JavaScript causing the postback.

                        Thou mewling ill-breeding pignut!

                        U Offline
                        U Offline
                        User 7763270
                        wrote on last edited by
                        #20

                        The other way I am thinking is to set the dropwdowns.Focus() some where in one of the the page life cycle events (init, Load, validate, Event, Render, Unload) but not in the same location where it is being set right now? Even then, no matter what by the time it loads the page the dropdownlist would get the focus? Please provide if this way provides any solution to my issue.

                        1 Reply Last reply
                        0
                        • U User 7763270

                          I kind of figured the issue. AS I have mentioned I have two User Controls , say UC-A and UC-B. The textBox that is being refreshed is located in UC-A and this is being used by UC-B and UC-B is used by a .aspx page so that is how it is structured. The UC-B has two DropDownList controls whose AutoPostBack property set to true. And programatically it is a must to set the focus to one of these DropDowlists. So when the page is loaded the DropDownList gets focus and when I try get the focus to the textBox to type in some thing then the DropDowlist is doing a post back as its AutoPostBack property has been set to true. So I am trying to figure out a way where, if I do not change the of the selectedIndex of the dropdown and just make its focus lost then do not do a post back. I think may be I can try to come up with a javascript function where I can nullify the post back in the javascript function. Any inputs if this sounds doable?

                          A Offline
                          A Offline
                          AspDotNetDev
                          wrote on last edited by
                          #21

                          So, your theory is that you set focus to the drop down list, then when you click on the textbox the focus is lost on the drop down list and causes a postback? That doesn't make sense to me, as I just created a test page and I couldn't replicate that behavior:

                          <%@ Page Language="vb" AutoEventWireup="false" %>

                          <script runat="server">
                          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                          ddlTest.Focus()
                          End Sub
                          </script>

                          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                          <html xmlns="http://www.w3.org/1999/xhtml">
                          <head runat="server">
                          <title>Test PostBack</title>
                          </head>
                          <body>
                          <form id="form1" runat="server">
                          <div>
                          Postback: <%= Page.IsPostBack.ToString() %>
                          <br />
                          <asp:DropDownList runat="server" ID="ddlTest" AutoPostBack="true">
                          <asp:ListItem Text="Item 1" Value="1" />
                          <asp:ListItem Text="Item 2" Value="2" />
                          <asp:ListItem Text="Item 3" Value="3" />
                          </asp:DropDownList>
                          <br />
                          <asp:TextBox runat="server" />
                          </div>
                          </form>
                          </body>
                          </html>

                          The focus starts on the drop down list. When I click the textbox, there is no postback. Can you create a test page that replicates the behavior?

                          Thou mewling ill-breeding pignut!

                          U 1 Reply Last reply
                          0
                          • A AspDotNetDev

                            The ? : syntax can't be used with VB.net, which is the language the OP is using. Instead, the OP would use the If (condition, option1, option2) syntax.

                            Thou mewling ill-breeding pignut!

                            B Offline
                            B Offline
                            BobJanova
                            wrote on last edited by
                            #22

                            Oh, good catch, I missed that. I've just been spending a month writing a C#-based ASP.net website and forgot about the existence of VB.

                            1 Reply Last reply
                            0
                            • A AspDotNetDev

                              So, your theory is that you set focus to the drop down list, then when you click on the textbox the focus is lost on the drop down list and causes a postback? That doesn't make sense to me, as I just created a test page and I couldn't replicate that behavior:

                              <%@ Page Language="vb" AutoEventWireup="false" %>

                              <script runat="server">
                              Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                              ddlTest.Focus()
                              End Sub
                              </script>

                              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                              <html xmlns="http://www.w3.org/1999/xhtml">
                              <head runat="server">
                              <title>Test PostBack</title>
                              </head>
                              <body>
                              <form id="form1" runat="server">
                              <div>
                              Postback: <%= Page.IsPostBack.ToString() %>
                              <br />
                              <asp:DropDownList runat="server" ID="ddlTest" AutoPostBack="true">
                              <asp:ListItem Text="Item 1" Value="1" />
                              <asp:ListItem Text="Item 2" Value="2" />
                              <asp:ListItem Text="Item 3" Value="3" />
                              </asp:DropDownList>
                              <br />
                              <asp:TextBox runat="server" />
                              </div>
                              </form>
                              </body>
                              </html>

                              The focus starts on the drop down list. When I click the textbox, there is no postback. Can you create a test page that replicates the behavior?

                              Thou mewling ill-breeding pignut!

                              U Offline
                              U Offline
                              User 7763270
                              wrote on last edited by
                              #23

                              Hi, That is exactly right. It shouldn't postback just because it lost the focus. I too tested it and it doesn't happen in my test page. I couldn't figure out what the issue is but when I debug our application it loads the page twice. And when the page got loaded I when I click on any where on the page the dropdown looses the focus and it is doing a post back. It is very annoying to me. I am not sure loading the page twice, which I realized when I debugged the app is somehow may be causing this issue. I am kind of exhausted with this issues as I couldn't figure what in the world is going on to cause the page load twice. I really appreciate for replying to all my messages. If I had any luck I will post it here. Thanks again. L

                              1 Reply Last reply
                              0
                              • A AspDotNetDev

                                According to this, you can use Request.Form("__EVENTTARGET") to see which control caused the postback. If that doesn't help, you have a couple options. For one, you could search the HTML source for "doPostBack" to see if there is anything funky that you missed. Also, you can use FireBug or the Chrome debugger to inspect all the JavaScript referenced by the page. If that doesn't get you anywhere, start removing things one by one until the second postback doesn't happen. That will help you narrow down what is causing the problem. And like I mentioned before, Fiddler2 is a great help for figuring this type of stuff out. It will show you all requests/responses, and all of the data being sent with them to/from the server. And I'm pretty sure you can use some setting to monitor local traffic (that is, web traffic from a Visual Studio web application that is being run locally).

                                Thou mewling ill-breeding pignut!

                                U Offline
                                U Offline
                                User 7763270
                                wrote on last edited by
                                #24

                                When I debugged and used the Request.Form("__EVENTTARGET") to see which one is causing the post back then as I thought the dropdown that had the focus is the culprit. I do not know why or couldn't figure out just because it lost the focus it is doing a post back. As you menioned with your tested example, and I did the same thing and for some reason it is doing a post back which it shouldn't as we tested it.

                                1 Reply Last reply
                                0
                                • B BobJanova

                                  Well you showed some code yourself using <%= ... %>; that just causes the ASP.net renderer to output something. There must be some expression which you use to determine whether the text field should be displayed in the rendered control when it is requested, unless you want it to either always be hidden or always shown when the page segment is loaded (remember, you're putting it in an UpdatePanel, so this includes whenever that panel is updated). If you're not familiar with the ? : syntax I suggest a quick visit to the documentation[^] (or if you have some books on any C family language you can find it in there too).

                                  U Offline
                                  U Offline
                                  User 7763270
                                  wrote on last edited by
                                  #25

                                  I am not sure if I understand it right what you have said. Can you please re frame your idea? Yes I am using vb.net but not sure what you mean and what you want me to suggest to use or code how?

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups