how to get ID of embedded control in javascript?

Discussion in 'ASP .Net' started by Dan, Oct 13, 2008.

  1. Dan

    Dan Guest

    Hi,

    There is a detailsview which contains a textbox in editmode.
    How to get the id of the textbox in javascript?
    I tried this:
    <asp:DetailsView ID="DetailsView1" runat="server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    <script type="text/javascript">
    var txt=document.getElementById('TextBox1')
    ....

    Thanks
    Dan
     
    Dan, Oct 13, 2008
    #1
    1. Advertising

  2. I have never used the DetailsView control, but I have used templated
    controls and I have more than enough experience with ASP.NET to know that
    the generated id is almost never the one given to the server control ID
    attribute. You will need to use the FindControl() method and the ClientID
    property.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Dan" <> wrote in message
    news:...
    > Hi,
    >
    > There is a detailsview which contains a textbox in editmode.
    > How to get the id of the textbox in javascript?
    > I tried this:
    > <asp:DetailsView ID="DetailsView1" runat="server" >
    > <asp:TemplateField>
    > <EditItemTemplate>
    > <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    > </EditItemTemplate>
    > </asp:TemplateField>
    > </asp:DetailsView>
    >
    > <script type="text/javascript">
    > var txt=document.getElementById('TextBox1')
    > ...
    >
    > Thanks
    > Dan
    >
     
    Nathan Sokalski, Oct 13, 2008
    #2
    1. Advertising

  3. Dan

    Dan Guest

    Thanks for replying,
    but with my code, i get the error:
    'TextBox1' is not declared'

    Textbox1 is embedded in detailsview1.




    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Dan" <> wrote in message
    > news:...
    >
    >> How to get the id of the TextBox in JavaScript?

    >
    > var txt=document.getElementById('<%=TextBox1.ClientID%>')
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #3
  4. Dan

    Hillbilly Guest

    When written inline into script that is generally linked to a separate file
    the statement implies the <%=TextBox1.ClientID%> expression is parsed when
    the page is compiled. Is this some kind of gift I have been ignorant of?


    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Dan" <> wrote in message
    > news:...
    >
    >> How to get the id of the TextBox in JavaScript?

    >
    > var txt=document.getElementById('<%=TextBox1.ClientID%>')
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Hillbilly, Oct 14, 2008
    #4
  5. Dan

    Dan Guest

    This is my aspx code:

    <asp:DetailsView ID="DetailsView1" runat="server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    and javascript:

    <script type="text/javascript">
    var txt=document.getElementById('<%=TextBox1.ClientID%>')

    With this, i get the error: 'TextBox1' is not declared' and i don't see a
    difference between 'TextBox1' and 'TextBox1'.
    It is surely my problem, but i post this for help. Thanks



    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Dan" <> wrote in message
    > news:%...
    >
    >>>> How to get the id of the TextBox in JavaScript?
    >>>
    >>> var txt=document.getElementById('<%=TextBox1.ClientID%>')

    >>
    >> Thanks for replying, but with my code, I get the error:
    >> 'TextBox1' is not declared'
    >>
    >> Textbox1 is embedded in detailsview1.

    >
    > Well there's your problem...
    >
    > JavaScript, like all languages derived from C, is case-sensitive -
    > TextBox1 is not the same as Textbox1...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #5
  6. Dan

    Dan Guest

    This is my aspx code:

    <asp:DetailsView ID="DetailsView1" runat="server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    and javascript:

    <script type="text/javascript">
    var txt=document.getElementById('<%=TextBox1.ClientID%>')

    With this, i get the error: 'TextBox1' is not declared' and i don't see a
    difference between 'TextBox1' and 'TextBox1'.
    It is surely my problem, but i post this for help. Thanks


    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Dan" <> wrote in message
    > news:%...
    >
    >>>> How to get the id of the TextBox in JavaScript?
    >>>
    >>> var txt=document.getElementById('<%=TextBox1.ClientID%>')

    >>
    >> Thanks for replying, but with my code, I get the error:
    >> 'TextBox1' is not declared'
    >>
    >> Textbox1 is embedded in detailsview1.

    >
    > Well there's your problem...
    >
    > JavaScript, like all languages derived from C, is case-sensitive -
    > TextBox1 is not the same as Textbox1...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #6
  7. Dan

    Dan Guest

    in the page source, i see: 'DetailsView1_TextBox1'

    var txt=document.getElementById('DetailsView1_TextBox1')

    this line works. All i wanted to know is whether there was a better way
    (using ClientID) to get the id. I find this method (searching in source) a
    little bit too basic ..


    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Dan" <> wrote in message
    > news:...
    >
    >> This is my aspx code:
    >>
    >> <asp:DetailsView ID="DetailsView1" runat="server" >
    >> <asp:TemplateField>
    >> <EditItemTemplate>
    >> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    >> </EditItemTemplate>
    >> </asp:TemplateField>
    >> </asp:DetailsView>
    >>
    >> and javascript:
    >>
    >> <script type="text/javascript">
    >> var txt=document.getElementById('<%=TextBox1.ClientID%>')
    >>
    >> With this, i get the error: 'TextBox1' is not declared' and i don't see a
    >> difference between 'TextBox1' and 'TextBox1'.
    >> It is surely my problem, but i post this for help. Thanks

    >
    > Look at your previous post - you wrote:
    >
    > "Textbox1 is embedded in detailsview1"
    >
    > Yet the DetailsView is not called "detailsview1", nor does it contain a
    > TextBox called "Textbox1"
    >
    > If you do a View Source, what is the munged name of the DetailsView and
    > what is the name of the munged TextBox? Please be accurate...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #7
  8. Dan

    Dan Guest

    This gives the error:
    Object reference not set to an instance of an object


    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Dan" <> wrote in message
    > news:...
    >
    >>> If you do a View Source, what is the munged name of the DetailsView and
    >>> what is the name of the munged TextBox? Please be accurate...

    >>
    >> In the page source, I see: 'DetailsView1_TextBox1'
    >>
    >> var txt=document.getElementById('DetailsView1_TextBox1')
    >>
    >> This line works. All I wanted to know is whether there was a better way
    >> (using ClientID) to get the ID. I find this method (searching in source)
    >> a little bit too basic ..

    >
    > How about this:
    >
    > document.getElementById('<%=DetailsView1.FindControl("TextBox1").ClientID%>');
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #8
  9. Dan

    Dan Guest

    Do you mean you give up?
    Is this so a weird problem?
    Anyway, thanks for your time ...
    I'll do it by searching in the code.

    "Mark Rae [MVP]" <> schreef in bericht
    news:%...
    > "Dan" <> wrote in message
    > news:...
    >
    >>> How about this:
    >>>
    >>> document.getElementById('<%=DetailsView1.FindControl("TextBox1").ClientID%>');

    >>
    >> This gives the error:
    >> Object reference not set to an instance of an object

    >
    > Hmm - OK.
    >
    > This discussion looks similar to your problem:
    > http://forums.asp.net/t/1075056.aspx
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #9
  10. Dan

    Dan Guest

    You saw my code. The textbox is in the editItemtemplate.
    I get that error when the page is loaded. I can't give you more info than
    that.


    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Dan" <> wrote in message
    > news:eJRLF%...
    >
    >>>>> How about this:
    >>>>>
    >>>>> document.getElementById('<%=DetailsView1.FindControl("TextBox1").ClientID%>');
    >>>>
    >>>> This gives the error:
    >>>> Object reference not set to an instance of an object
    >>>
    >>> Hmm - OK.
    >>>
    >>> This discussion looks similar to your problem:
    >>> http://forums.asp.net/t/1075056.aspx

    >>
    >> Do you mean you give up?

    >
    > Not at all.
    >
    >> Is this so a weird problem?

    >
    > No idea - it worked fine for me...
    >
    > Was the DetailsView definitely in Edit mode when you were trying to
    > reference the TextBox...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Dan, Oct 14, 2008
    #10
  11. Dan

    Dan Guest

    unsolved problem ...
    if any has an idea ...

    "Dan" <> schreef in bericht
    news:%...
    > You saw my code. The textbox is in the editItemtemplate.
    > I get that error when the page is loaded. I can't give you more info than
    > that.
    >
    >
    > "Mark Rae [MVP]" <> schreef in bericht
    > news:...
    >> "Dan" <> wrote in message
    >> news:eJRLF%...
    >>
    >>>>>> How about this:
    >>>>>>
    >>>>>> document.getElementById('<%=DetailsView1.FindControl("TextBox1").ClientID%>');
    >>>>>
    >>>>> This gives the error:
    >>>>> Object reference not set to an instance of an object
    >>>>
    >>>> Hmm - OK.
    >>>>
    >>>> This discussion looks similar to your problem:
    >>>> http://forums.asp.net/t/1075056.aspx
    >>>
    >>> Do you mean you give up?

    >>
    >> Not at all.
    >>
    >>> Is this so a weird problem?

    >>
    >> No idea - it worked fine for me...
    >>
    >> Was the DetailsView definitely in Edit mode when you were trying to
    >> reference the TextBox...?
    >>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net

    >
    >
     
    Dan, Oct 15, 2008
    #11
  12. Dan

    Norm Guest

    On Oct 15, 2:28 pm, "Dan" <> wrote:
    > unsolved problem ...
    > if any has an idea ...
    >
    > "Dan" <> schreef in berichtnews:%...
    >
    > > You saw my code. The textbox is in the editItemtemplate.
    > > I get that error when the page is loaded. I can't give you more info than
    > > that.

    >
    > > "Mark Rae [MVP]" <> schreef in bericht
    > >news:...
    > >> "Dan" <> wrote in message
    > >>news:eJRLF%...

    >
    > >>>>>> How about this:

    >
    > >>>>>> document.getElementById('<%=DetailsView1.FindControl("TextBox1")..ClientID%>');

    >
    > >>>>> This gives the error:
    > >>>>> Object reference not set to an instance of an object

    >
    > >>>> Hmm - OK.

    >
    > >>>> This discussion looks similar to your problem:
    > >>>>http://forums.asp.net/t/1075056.aspx

    >
    > >>> Do you mean you give up?

    >
    > >> Not at all.

    >
    > >>> Is this so a weird problem?

    >
    > >> No idea - it worked fine for me...

    >
    > >> Was the DetailsView definitely in Edit mode when you were trying to
    > >> reference the TextBox...?

    >
    > >> --
    > >> Mark Rae
    > >> ASP.NET MVP
    > >>http://www.markrae.net


    Alright ... I may be able to help a little bit with this.

    This is what I would do: (Note: code is off the top of my head and in
    VB.net)

    PAGE:
    <asp:DetailsView ID="DetailsView1" runat="server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"
    OnPreRender="TextBox1_PreRender"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    CODEBEHIND/BASE CLASS
    Protected Sub TextBox1_PreRender(sender as object, e as eventargs)
    ' First two parameters uniquely identify the JS to be rendered.
    The third is the JS itself and the fourth wraps the JS in script tags
    Page.ClientScript.RegisterStartUpScript(gettype(<insert page
    type>),"any string","var txt=document.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript",true)
    End Sub


    This way the JS code will only be rendered when the Textbox is being
    rendered. Also what you might want to do is write a JS function in a
    separate JS file that takes the id as a parameter and then does the
    work on it. It will simplify the JS that you are rendering.

    i.e.
    "var txt=document.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript" = "myFunction('" & DirectCast(sender,WebControl).ClientID
    & "');"

    Hope this helps!

    Norm
     
    Norm, Oct 16, 2008
    #12
  13. Dan

    Dan Guest

    Yes, thank you.

    "Norm" <> schreef in bericht
    news:...
    On Oct 15, 2:28 pm, "Dan" <> wrote:
    > unsolved problem ...
    > if any has an idea ...
    >
    > "Dan" <> schreef in
    > berichtnews:%...
    >
    > > You saw my code. The textbox is in the editItemtemplate.
    > > I get that error when the page is loaded. I can't give you more info
    > > than
    > > that.

    >
    > > "Mark Rae [MVP]" <> schreef in bericht
    > >news:...
    > >> "Dan" <> wrote in message
    > >>news:eJRLF%...

    >
    > >>>>>> How about this:

    >
    > >>>>>> document.getElementById('<%=DetailsView1.FindControl("TextBox1").ClientID%>');

    >
    > >>>>> This gives the error:
    > >>>>> Object reference not set to an instance of an object

    >
    > >>>> Hmm - OK.

    >
    > >>>> This discussion looks similar to your problem:
    > >>>>http://forums.asp.net/t/1075056.aspx

    >
    > >>> Do you mean you give up?

    >
    > >> Not at all.

    >
    > >>> Is this so a weird problem?

    >
    > >> No idea - it worked fine for me...

    >
    > >> Was the DetailsView definitely in Edit mode when you were trying to
    > >> reference the TextBox...?

    >
    > >> --
    > >> Mark Rae
    > >> ASP.NET MVP
    > >>http://www.markrae.net


    Alright ... I may be able to help a little bit with this.

    This is what I would do: (Note: code is off the top of my head and in
    VB.net)

    PAGE:
    <asp:DetailsView ID="DetailsView1" runat="server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"
    OnPreRender="TextBox1_PreRender"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    CODEBEHIND/BASE CLASS
    Protected Sub TextBox1_PreRender(sender as object, e as eventargs)
    ' First two parameters uniquely identify the JS to be rendered.
    The third is the JS itself and the fourth wraps the JS in script tags
    Page.ClientScript.RegisterStartUpScript(gettype(<insert page
    type>),"any string","var txt=document.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript",true)
    End Sub


    This way the JS code will only be rendered when the Textbox is being
    rendered. Also what you might want to do is write a JS function in a
    separate JS file that takes the id as a parameter and then does the
    work on it. It will simplify the JS that you are rendering.

    i.e.
    "var txt=document.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript" = "myFunction('" & DirectCast(sender,WebControl).ClientID
    & "');"

    Hope this helps!

    Norm
     
    Dan, Oct 16, 2008
    #13
  14. Dan

    Peter Ivanov Guest

    Now I realise that this is a bit of an long time to wait to give a reply, but none the less, for anyone reading up on this what I did that worked for me is when adding the control (eg. CheckBox) you can give it a OnClick (or what ever you may need) event that looks like this.

    OnClick='myFunction(this.id);'

    Passing this to this to what ever function you need to perform in JavaScript does pick it up. I've tested this from GridViews, Repeaters and DataLists.

    I hope this helps.



    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    15-Oct-08

    unsolved problem ..
    if any has an idea ..

    "Dan" <> schreef in bericht

    Previous Posts In This Thread:

    On Monday, October 13, 2008 3:55 PM
    Dan wrote:

    how to get ID of embedded control in javascript?
    Hi

    There is a detailsview which contains a textbox in editmode
    How to get the id of the textbox in javascript
    I tried this
    <asp:DetailsView ID="DetailsView1" runat="server"
    <asp:TemplateField
    <EditItemTemplate
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox
    </EditItemTemplate
    </asp:TemplateField
    </asp:DetailsView

    <script type="text/javascript"
    var txt=document.getElementById('TextBox1'
    ...

    Thank
    Dan

    On Monday, October 13, 2008 6:03 PM
    Nathan Sokalski wrote:

    I have never used the DetailsView control, but I have used templated controls
    I have never used the DetailsView control, but I have used templated
    controls and I have more than enough experience with ASP.NET to know that
    the generated id is almost never the one given to the server control ID
    attribute. You will need to use the FindControl() method and the ClientID
    property
    --
    Nathan Sokalsk

    http://www.nathansokalski.com

    "Dan" <> wrote in message
    news:...

    On Monday, October 13, 2008 6:09 PM
    Mark Rae [MVP] wrote:

    Re: how to get ID of embedded control in javascript?
    var txt=document.getElementById('<%=TextBox1.ClientID%>'

    -
    Mark Ra
    ASP.NET MV
    http://www.markrae.net

    On Tuesday, October 14, 2008 10:19 AM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    Thanks for replying
    but with my code, i get the error
    'TextBox1' is not declared

    Textbox1 is embedded in detailsview1


    "Mark Rae [MVP]" <> schreef in bericht

    On Tuesday, October 14, 2008 10:46 AM
    Hillbilly wrote:

    When written inline into script that is generally linked to a separate file
    When written inline into script that is generally linked to a separate file
    the statement implies the <%=TextBox1.ClientID%> expression is parsed when
    the page is compiled. Is this some kind of gift I have been ignorant of

    "Mark Rae [MVP]" <> wrote in message
    news:...

    On Tuesday, October 14, 2008 10:47 AM
    Mark Rae [MVP] wrote:

    Re: how to get ID of embedded control in javascript?
    Well there is your problem..

    JavaScript, like all languages derived from C, is case-sensitive - TextBox
    is not the same as Textbox1..

    -
    Mark Ra
    ASP.NET MV
    http://www.markrae.net

    On Tuesday, October 14, 2008 11:08 AM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    This is my aspx code

    <asp:DetailsView ID="DetailsView1" runat="server"
    <asp:TemplateField
    <EditItemTemplate
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox
    </EditItemTemplate
    </asp:TemplateField
    </asp:DetailsView

    and javascript

    <script type="text/javascript"
    var txt=document.getElementById('<%=TextBox1.ClientID%>'

    With this, i get the error: 'TextBox1' is not declared' and i don't see a
    difference between 'TextBox1' and 'TextBox1'
    It is surely my problem, but i post this for help. Thank


    "Mark Rae [MVP]" <> schreef in bericht
    news:...

    On Tuesday, October 14, 2008 11:09 AM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    This is my aspx code

    <asp:DetailsView ID="DetailsView1" runat="server"
    <asp:TemplateField
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    and javascript:

    <script type="text/javascript">
    var txt=document.getElementById('<%=TextBox1.ClientID%>')

    With this, i get the error: 'TextBox1' is not declared' and i don't see a
    difference between 'TextBox1' and 'TextBox1'.
    It is surely my problem, but i post this for help. Thanks


    "Mark Rae [MVP]" <> schreef in bericht
    news:...

    On Tuesday, October 14, 2008 11:29 AM
    Mark Rae [MVP] wrote:

    Re: how to get ID of embedded control in javascript?
    "Dan" <> wrote in message
    news:...


    Look at your previous post - you wrote:

    "Textbox1 is embedded in detailsview1"

    Yet the DetailsView is not called "detailsview1", nor does it contain a
    TextBox called "Textbox1"

    If you do a View Source, what is the munged name of the DetailsView and what
    is the name of the munged TextBox? Please be accurate...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net

    On Tuesday, October 14, 2008 11:43 AM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    in the page source, i see: 'DetailsView1_TextBox1'

    var txt=document.getElementById('DetailsView1_TextBox1')

    this line works. All i wanted to know is whether there was a better way
    (using ClientID) to get the id. I find this method (searching in source) a
    little bit too basic ..


    "Mark Rae [MVP]" <> schreef in bericht
    news:...

    On Tuesday, October 14, 2008 12:02 PM
    Mark Rae [MVP] wrote:

    Re: how to get ID of embedded control in javascript?
    How about this:

    document.getElementById('<%=DetailsView1.FindControl("TextBox1").ClientID%>');


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net

    On Tuesday, October 14, 2008 12:10 PM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    This gives the error:
    Object reference not set to an instance of an object


    "Mark Rae [MVP]" <> schreef in bericht

    On Tuesday, October 14, 2008 12:39 PM
    Mark Rae [MVP] wrote:

    Re: how to get ID of embedded control in javascript?
    Hmm - OK.

    This discussion looks similar to your problem:
    http://forums.asp.net/t/1075056.aspx


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net

    On Tuesday, October 14, 2008 1:08 PM
    Dan wrote:

    Do you mean you give up?Is this so a weird problem?
    Do you mean you give up?
    Is this so a weird problem?
    Anyway, thanks for your time ...
    I will do it by searching in the code.

    "Mark Rae [MVP]" <> schreef in bericht

    On Tuesday, October 14, 2008 2:35 PM
    Mark Rae [MVP] wrote:

    Re: how to get ID of embedded control in javascript?
    Not at all.


    No idea - it worked fine for me...

    Was the DetailsView definitely in Edit mode when you were trying to
    reference the TextBox...?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net

    On Tuesday, October 14, 2008 4:27 PM
    Dan wrote:

    You saw my code. The textbox is in the editItemtemplate.
    You saw my code. The textbox is in the editItemtemplate.
    I get that error when the page is loaded. I can't give you more info than
    that.


    "Mark Rae [MVP]" <> schreef in bericht
    news:...

    On Wednesday, October 15, 2008 5:28 PM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    unsolved problem ...
    if any has an idea ...

    "Dan" <> schreef in bericht

    On Thursday, October 16, 2008 10:19 AM
    Dan wrote:

    Re: how to get ID of embedded control in javascript?
    Yes, thank you.

    "Norm" <> schreef in bericht
    news:...
    On Oct 15, 2:28 pm, "Dan" <> wrote:

    Alright ... I may be able to help a little bit with this.

    This is what I would do: (Note: code is off the top of my head and in
    VB.net)

    PAGE:
    <asp:DetailsView ID="DetailsView1" runat="server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"
    OnPreRender="TextBox1_PreRender"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    CODEBEHIND/BASE CLASS
    Protected Sub TextBox1_PreRender(sender as object, e as eventargs)
    ' First two parameters uniquely identify the JS to be rendered.
    The third is the JS itself and the fourth wraps the JS in script tags
    Page.ClientScript.RegisterStartUpScript(gettype(<insert page
    type>),"any string","var txt=document.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript",true)
    End Sub


    This way the JS code will only be rendered when the Textbox is being
    rendered. Also what you might want to do is write a JS function in a
    separate JS file that takes the id as a parameter and then does the
    work on it. It will simplify the JS that you are rendering.

    i.e.
    "var txt=document.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript" = "myFunction('" & DirectCast(sender,WebControl).ClientID
    & "');"

    Hope this helps!

    Norm

    On Friday, October 17, 2008 7:14 AM
    Norm wrote:

    Re: how to get ID of embedded control in javascript?
    On Oct 15, 2:28=A0pm, "Dan" <> wrote:
    P04.phx.gbl...
    an
    ..ClientID%>');

    Alright ... I may be able to help a little bit with this.

    This is what I would do: (Note: code is off the top of my head and in
    VB.net)

    PAGE:
    <asp:DetailsView ID=3D"DetailsView1" runat=3D"server" >
    <asp:TemplateField>
    <EditItemTemplate>
    <asp:TextBox ID=3D"TextBox1" runat=3D"server"
    OnPreRender=3D"TextBox1_PreRender"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </asp:DetailsView>

    CODEBEHIND/BASE CLASS
    Protected Sub TextBox1_PreRender(sender as object, e as eventargs)
    ' First two parameters uniquely identify the JS to be rendered.
    The third is the JS itself and the fourth wraps the JS in script tags
    Page.ClientScript.RegisterStartUpScript(gettype(<insert page
    type>),"any string","var txt=3Ddocument.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript",true)
    End Sub


    This way the JS code will only be rendered when the Textbox is being
    rendered. Also what you might want to do is write a JS function in a
    separate JS file that takes the id as a parameter and then does the
    work on it. It will simplify the JS that you are rendering.

    i.e.
    "var txt=3Ddocument.getElementById('" &
    DirectCast(sender,WebControl).ClientID & "'); //insert other
    javascript" =3D "myFunction('" & DirectCast(sender,WebControl).ClientID
    & "');"

    Hope this helps!

    Norm


    Submitted via EggHeadCafe - Software Developer Portal of Choice
    BOOK REVIEW: Effective C#, Second Edition [Addison Wesley]
    http://www.eggheadcafe.com/tutorial...7af-c38852b3b455/book-review-effective-c.aspx
     
    Peter Ivanov, Jun 3, 2010
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Paul Gloster
    Replies:
    48
    Views:
    1,895
    Colin Paul Gloster
    Apr 10, 2007
  2. Dan
    Replies:
    3
    Views:
    350
  3. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    405
    Thomas Dodds
    Jul 26, 2004
  4. Trans
    Replies:
    11
    Views:
    306
    micathom
    Sep 5, 2007
  5. Num GG
    Replies:
    2
    Views:
    369
    Num GG
    Nov 17, 2008
Loading...

Share This Page