asp.net HiddenField ClientId

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • imbirek8

    asp.net HiddenField ClientId

    Hi!

    I would like to write something in Javascript to HiddenField:
    <asp:HiddenFiel d ID="hiddenMy" runat="server" Value="" />
    And in the code behind read this value.

    But this HiddenField is in user control. I use this control in many pages
    and in every page this field has different value..
    I don't know why, but it's error when I do:
    var hidden = document.GetEle mentById('<%= this.gvClient.C lientID %>');

    How can I find ClientId of the HiddenField ?

    Thanks for help


  • Alberto Poblacion

    #2
    Re: asp.net HiddenField ClientId

    "imbirek8" <imbirek8@op.pl wrote in message
    news:gdnu8q$sp5 $1@news.onet.pl ...
    I would like to write something in Javascript to HiddenField:
    <asp:HiddenFiel d ID="hiddenMy" runat="server" Value="" />
    And in the code behind read this value.
    >
    But this HiddenField is in user control. I use this control in many pages
    and in every page this field has different value..
    I don't know why, but it's error when I do:
    var hidden = document.GetEle mentById('<%= this.gvClient.C lientID %>');
    >
    How can I find ClientId of the HiddenField ?
    You can use hiddenMy.Client ID.Replace("_", "$")


    Comment

    • imbirek8

      #3
      Re: asp.net HiddenField ClientId

      "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
      in message news:en4dW%23HN JHA.2348@TK2MSF TNGP05.phx.gbl. ..
      [...]
      >How can I find ClientId of the HiddenField ?
      You can use hiddenMy.Client ID.Replace("_", "$")
      [...]

      I don't understant what exacly it would do..

      Comment

      • Alberto Poblacion

        #4
        Re: asp.net HiddenField ClientId


        "imbirek8" <imbirek8@op.pl wrote in message
        news:gdo2et$dmu $1@news.onet.pl ...
        "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
        in message news:en4dW%23HN JHA.2348@TK2MSF TNGP05.phx.gbl. ..
        [...]
        >>How can I find ClientId of the HiddenField ?
        > You can use hiddenMy.Client ID.Replace("_", "$")
        [...]
        >
        I don't understant what exacly it would do..
        The ClientID property returns an ID that separates its various parts
        with "_", such as "ctl00_ctrlA1_r ptB1_ctl00_ibB1 ", but the Name that is
        actually rendered into the html uses "$" to separate the same parts, such as
        "ctl00$ctrlA1$r ptB1$ctl01$ibB1 ". At first I thought that you were doing a
        finding the element by Name, so you would have needed the previous
        replacement to find the control on the client side.

        Upon rereading your message, I see that this is not the case. You mention
        that you are doing document.GetEle mentById('<%= this.gvClient.C lientID
        %>'); and that you get an error, but you don't mention what the error is.
        You also don't mention wether you are writing this code in the markup of the
        Page or the UserControl. If you are writing it on the Page, the "this" is
        interpreted on the page class, so "this.gvCli ent" needs to refer to a public
        field or property on the page, it won't work if gvClient is inside a
        usercontrol that is inside the page, since this is not automatically visible
        to the page class. You may wish to add a public property to expose the
        information that you then render in the <%= ... %>.

        Comment

        • imbirek8

          #5
          Re: asp.net HiddenField ClientId

          "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
          in message news:%231nAcsNN JHA.2348@TK2MSF TNGP05.phx.gbl. ..
          [...]

          And what about this idea. I would like to add for example label and set
          style="display: none;". and in JavaScript change the the text and in code
          behind read new value. I have:
          <asp:Label ID="labelSelect edRow" runat="server" Text="aaa" />
          and i JavaScript I do:
          (document.getEl ementById('<%= this.labelSelec tedRow.ClientID %>')).Text=
          "bbb";

          but nothing has changed... why ?

          Thanks for help

          Comment

          • Alberto Poblacion

            #6
            Re: asp.net HiddenField ClientId

            "imbirek8" <imbirek8@op.pl wrote in message
            news:ge35p4$l91 $1@news.onet.pl ...
            "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
            in message news:%231nAcsNN JHA.2348@TK2MSF TNGP05.phx.gbl. ..
            [...]
            >
            And what about this idea. I would like to add for example label and set
            style="display: none;". and in JavaScript change the the text and in code
            behind read new value. I have:
            <asp:Label ID="labelSelect edRow" runat="server" Text="aaa" />
            and i JavaScript I do:
            (document.getEl ementById('<%= this.labelSelec tedRow.ClientID %>')).Text=
            "bbb";
            >
            but nothing has changed... why ?
            It *should* change... on the display in the browser, if it weren't
            hidden. But you can't read it back in the code behind because the label does
            not get posted back from the browser into the server.
            You can do it with a HiddenField, which is rendered as <input
            type="hidden"(o r you can use a TextBox with style="display: none;"). If you
            change its value in javscript, it will be available to the server after the
            form gets submitted.

            Comment

            • imbirek8

              #7
              Re: asp.net HiddenField ClientId

              "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
              in message news:eO7p3IAOJH A.1668@TK2MSFTN GP06.phx.gbl...
              [...]
              I do not understand anything. I have the simpliest code I can have and it
              doesn't work. I can't see even alert.. in firebug I don't have any error.
              Why it doesn't work ?

              <body>
              <script type="text/javascript">
              function click()
              {
              alert('here1');
              (document.getEl ementById('<%=t his.txtSelected Row.ClientID%>' )).Text
              = "bbb";
              }
              </script>
              <form id="form1" runat="server">
              <div>
              <asp:TextBox ID="txtSelected Row" runat="server" ></asp:TextBox>
              <asp:Button ID="Button1" runat="server" Text="Button"
              OnClientClick=" click()" />
              </div>
              </form>
              </body>

              Thanks for help

              Comment

              • Alberto Poblacion

                #8
                Re: asp.net HiddenField ClientId

                Two changes:

                - First, change the name of the function from "click" into something else,
                such as "b_click" (click is already in use somewhere else in the hierarchy
                of dhtml objects).

                - Second, you should use ".value" instead of ".Text". We are using the
                client-side object model, not the server-side. In other words, asp:TextBox
                has a .Text property, but it gets rendered into the browser as an <input
                type=text.../>, which has a .value but not a .Text.

                I have copied a corrected version at the bottom.


                "imbirek8" <imbirek8@op.pl wrote in message
                news:ge5c0o$bqb $1@news.onet.pl ...
                "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
                in message news:eO7p3IAOJH A.1668@TK2MSFTN GP06.phx.gbl...
                [...]
                I do not understand anything. I have the simpliest code I can have and it
                doesn't work. I can't see even alert.. in firebug I don't have any error.
                Why it doesn't work ?
                >
                <body>
                <script type="text/javascript">
                function click()
                {
                alert('here1');
                (document.getEl ementById('<%=t his.txtSelected Row.ClientID%>' )).Text
                = "bbb";
                }
                </script>
                <form id="form1" runat="server">
                <div>
                <asp:TextBox ID="txtSelected Row" runat="server" ></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Button"
                OnClientClick=" click()" />
                </div>
                </form>
                </body>
                >
                Thanks for help

                <body>
                <script type="text/javascript">
                function b_click()
                {
                alert('here1');
                (document.getEl ementById('<%=t his.txtSelected Row.ClientID%>' )).value
                = "bbb";
                }
                </script>
                <form id="form1" runat="server">
                <div>
                <asp:TextBox ID="txtSelected Row" runat="server" ></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Button"
                OnClientClick=" b_click();" />
                </div>
                </form>
                </body>


                Comment

                Working...