GetElementByID question

Last post 05-31-2007 3:03 AM by Prashant Kumar. 11 replies.

Sort Posts:

  • GetElementByID question

    05-29-2007, 10:11 PM
    • Loading...
    • rbunn83815
    • Joined on 02-02-2007, 8:38 AM
    • Posts 87

    Hello Everyone,

          I am really new to Javascript and I am trying to make a slight modification to previously written Javascript.  I had my asp controls SearchTextBox and SearchButton inside a panel within an ajax toolkit tab panel control (ID = "Tabs") .  Now I am using a MultiView control (ID = "MultiView1") instead of the tab panel.  The problem is I don't know how to name the element to accomodate for this change.  Originally one of the lines of code read this way:

    document.getElementById('Tabs_VisualSearchTabPanel_SearchButton').click()   (I think I could fix the rest if I know how to do this one)

    I'm not sure exactly how to change this line of code properly.  If it's easier to solve this problem the page can be viewed at www.visual-search.net 

    Also, please remember I am just beginning in JavaScript so please try to be clear with your answer.  Thanks in advance.

                                                                                                                                         Robert
     

    On the border of insanity and reason lies the realm of genius.
  • Re: GetElementByID question

    05-30-2007, 2:02 AM
    Answer
    • Loading...
    • e_screw
    • Joined on 10-20-2004, 1:22 PM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,852

    To access the webcontrols in your javascript, always use the ClientID property.  This will get you the ID of the control, with what it will be rendered. If you server control name is "SearchButton" you should use

    document.getElementById('<%= SearchButton.ClientID %>').click() ;

    SearchButton.ClientID, gets you the actual ID of the control on the html page, with the container names appending to it in the order of hierarchy.

    Thanks

    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
  • Re: GetElementByID question

    05-30-2007, 3:30 AM
    • Loading...
    • Nitinkcv
    • Joined on 03-13-2007, 11:07 AM
    • Posts 264

     Hi,

     What if my elemets are inside a repeater. i tried using  document.getElementById('<%= MyLabel.ClientID %>').value ;

     where mylabel is the id of my label inside a repeater. but its showing an error The type or namespace name 'MyLabel_Vert' could not be found (are you missing a using directive or an assembly reference?)

    So is there any way i could access the repeater elemenst value using javascript

    Thanks 

    Thanks
  • Re: GetElementByID question

    05-30-2007, 4:09 AM
    • Loading...
    • e_screw
    • Joined on 10-20-2004, 1:22 PM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,852

    Well, you cannot directly access the controls inside a repeater. You need to hookup the javascript to the controls inside the repeater events. Lets us know what you are trying to do, so that we can provided the required solution.

    Thanks

    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
  • Re: GetElementByID question

    05-30-2007, 4:22 AM

    yea e_screw is correct we hookup javascript Smile 

    i believe you are using Templates in Repeater and one of itemtemplates contains lable if i'm not wrong!!

    normally in datacontrols in RowDataBound event for example of GridView we do a findControl() and pass field values etc to javascript function. say for example if i want to call a javascript function on checkbox column's check/uncheck

    Checkbox chk = FindControl

    chk.attributes.add("event","javascript function('+grd.Rows[CurrentRowIndex].Cells[index].Text+')

    something like that.

    thanks,

    satish.

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: GetElementByID question

    05-30-2007, 8:56 AM
    • Loading...
    • Nitinkcv
    • Joined on 03-13-2007, 11:07 AM
    • Posts 264

    e_screw:

    Well, you cannot directly access the controls inside a repeater. You need to hookup the javascript to the controls inside the repeater events. Lets us know what you are trying to do, so that we can provided the required solution.

    Thanks

     

    I have a repeater control having 4 lables two textboxes and 2 Buttons. The labels contain details such as name, age, addr etc.

    So on click of one of the buttons i need to move the details of a person from one location to the other. The problem is i want to use a confirm dialogue in JS.

    In my confirm box i need to something like ' are u sure u want to move <FirstName>':

    So the code is something like: 

    function moveUser()

    {

    var name=document.getElementById('<%= lblFirstName.ClientID %>').value() ;

    var answer=confirm("Are you sure you want to move this candidate?"+ name);

    if(answer) return true;

    else return false;

    }

    But this is showing an error that The type or namespace name 'lblFirstName' could not be found (are you missing a using directive or an assembly reference?)

    This is obviously because the element lblFirstName being inside a repeater cannot be detected.

    So is there any way i could access that particular value using JavaScript.

     Thanks

    Thanks
  • Re: GetElementByID question

    05-30-2007, 9:14 AM

    Well in this case you will find your button by FindControl in the itemcreated event and then add the javascript. An example is as below

     

    if (e.Item.ItemType == ListItemType.Footer)

    {

    HtmlInputButton Ibtn0 = (HtmlInputButton)(e.Item.FindControl("btnDelete"));

    Ibtn0.Attributes.Add("onclick", "return confirm (\'Are you sure?\');");

    }

    Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
  • Re: GetElementByID question

    05-30-2007, 9:24 AM
    • Loading...
    • Nitinkcv
    • Joined on 03-13-2007, 11:07 AM
    • Posts 264

    naturehermit:

    Well in this case you will find your button by FindControl in the itemcreated event and then add the javascript. An example is as below

     

    if (e.Item.ItemType == ListItemType.Footer)

    {

    HtmlInputButton Ibtn0 = (HtmlInputButton)(e.Item.FindControl("btnDelete"));

    Ibtn0.Attributes.Add("onclick", "return confirm (\'Are you sure?\');");

    }

     

    Hi,

    i know how to find the button. however i do not know how to get the value of a particular label inside the repeater.

    my confirm message should be like: " Are you sure u want to move JOHN" where JOHNis the value of the lblFirstName which is inside the repeater.

    I need to get it by JS.

    Thanks 

    Thanks
  • Re: GetElementByID question

    05-30-2007, 9:30 AM

    well for label you pass the id of the label instead of button. And for John, i suggest make a property and append it to the message.

    Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
  • Re: GetElementByID question

    05-30-2007, 10:23 AM
    • Loading...
    • Nitinkcv
    • Joined on 03-13-2007, 11:07 AM
    • Posts 264

    Hi,

    This is what i did:

    in the item_datatbound event of the repeater:

    Label lblFirstName = (Label) e.Item.FindControl("lblFirstName");

    Button btnMoveUser =(Button)e.Item.FindControl("btnMoveUser");                              


    btnMoveUser.Attributes.Add("onclick","return moveUser('" + lblFirstName.ClientID.ToString() +"');");

    in the aspx page:

    please note this is inside the repeater
    <asp:Button id="btnMoveUser" text="Move User" runat="server"  onclick=" moveUser(' lblFirstName_Vert.ClientID ');" /></td>

    Javascript:

    function moveUser(lblID)
    {
      var lbl = document.getElementById('lblId');
      alert(lbl);
      var answer=confirm("Are you sure you want to move this candidate" + lblID.value +"?");
      if(answer)
      return true;
      else
      return false;
     
    }

    But im getting an error in the aspx page stating: Too many characters in character literal


    i tried the following but they too were showing error:

    onclick=" moveUser(lblFirstName_Vert.ClientID);" Error:  ) expected
                                                        

    onclick=' moveUser(lblFirstName_Vert.ClientID);"  Error:  ) expected

    What could be the solution

    Thanks 


                                    

     

    Thanks
  • Re: GetElementByID question

    05-30-2007, 11:08 AM
    • Loading...
    • Nitinkcv
    • Joined on 03-13-2007, 11:07 AM
    • Posts 264

    Hi,

    I have been able to fix that error.

    the javascript code is:

    function moveUser(lblID)

    {

    debugger;

    alert('in');

    var lbl = document.getElementById(lblID);

    var answer=confirm("Are you sure you want to move this candidate" + lbl.value +"?");

    if(answer) return true;

    else return false; 

    }

    But now when i debug the code the lblID is showing the correct client id. but the lbl is showing some value {...} and the lbl.value is undefined.

    Whjat could be the  problem

    THanks 

     

    Thanks
  • Re: GetElementByID question

    05-31-2007, 3:03 AM

     A Label is rendered as a span element in HTML which does not have a property called value.

    In IE use lbl.innerText or lbl.innerHTML

    In Firefox, use lbl.textContent or lbl.innerHTML.
    Regards,
    Prashant


    Dont forget to click "Mark as Answer" on the post that helped you.
Page 1 of 1 (12 items)
Microsoft Communities
Page view counter