Get Client ID from inside a formview

Last post 01-13-2009 11:14 AM by NC01. 13 replies.

Sort Posts:

  • Get Client ID from inside a formview

    01-12-2009, 1:10 PM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

    I am using the FCKeditor text editor inside my website.  I am trying to use a plugin with the editor that requires me to find the value of a control using documnet.getElementbyID(). 

    I have NO Javascript experience but from what I can tell I need to do something like the following. 

    document.getElementById('<%= ("TitleTextBox.ClientID") %>');

    It returns null. 

    I have also tried 

    document.getElementById('<%= ("Formview1.Findcontrol("TitleTextBox.ClientID") )%>');

     I have read some about using ClientSrcipt.RegisterClientScriptBlock, but I am not exactly sure this is what I need, and if it is I need some help with where to put it and probably also need to learn exactly how the FCKeditor code works. 

    It does work if I go to the page source and copy the generated Id directly.  But this is obviously not how i want to code it. 

    I would appreciate any help.

    Thanks,
    Laura

     

    Filed under: ,
  • Re: Get Client ID from inside a formview

    01-12-2009, 1:16 PM

    Try this: document.getElementById('<%= TitleTextBox.ClientID %>');

  • Re: Get Client ID from inside a formview

    01-12-2009, 1:24 PM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

     Thank you, I tried it and it still just gives me null.

  • Re: Get Client ID from inside a formview

    01-12-2009, 1:25 PM
    • All-Star
      59,859 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,544
    • TrustedFriends-MVPs

    try this

    var FormView = document.getElementById('<%= "Formview1.ClientID"%>');

    var inputs = FormView.getElementsByTagName("input");

    for (i=0;i<inputs.length;i++)

    {

    if (inputs[i].id.indexOf("TitleTextBox") !=-1)

    {

    alert(inputs[i].value);

    break;

    }

    }

  • Re: Get Client ID from inside a formview

    01-12-2009, 1:32 PM
    • All-Star
      75,329 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,007
    • TrustedFriends-MVPs

    This: document.getElementById('<%= ("Formview1.Findcontrol("TitleTextBox.ClientID") )%>') won't work because you have a syntax error.

    Try document.getElementById('<%= FormView1.FindControl( "TitleTextBox").ClientID %>')

    NC...

  • Re: Get Client ID from inside a formview

    01-12-2009, 1:41 PM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

     I put this in my code and I got this Error: null is 'null' or not an object

     Since I really don't know anything about this I am going to post my code. I may have done something wrong. I set the value of my variable = the alert you have in th code. I also tried just adding the code and not setting the value of my title variable and got the same error. 

    Thanks Laura

     var title = document.createElement('INPUT');
        title.type = 'hidden';
        //change the name as needed -->
        title.name = 'htmlTitle';
        //set the data
        
        var FormView = document.getElementById('<%= "Formview1.ClientID"%>');

        var inputs = FormView.getElementsByTagName("input");

        for (i = 0; i < inputs.length; i++) {

            if (inputs[i].id.indexOf("TitleTextBox") != -1) {

                title.value = alert(inputs[i].value);

                break;

            }

        }
        
                     
        //append the new input to the form
        theForm.appendChild(title);

  • Re: Get Client ID from inside a formview

    01-12-2009, 1:54 PM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

    NC01:

    This: document.getElementById('<%= ("Formview1.Findcontrol("TitleTextBox.ClientID") )%>') won't work because you have a syntax error.

    Try document.getElementById('<%= FormView1.FindControl( "TitleTextBox").ClientID %>')

    NC...

     

     Thank you.  This still gave me null. 

  • Re: Get Client ID from inside a formview

    01-12-2009, 2:29 PM
    • All-Star
      75,329 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,007
    • TrustedFriends-MVPs

    Well then you either do not have a FormView1 element defined or you don't have a TitleTextBox element defined inside of it.

    Add this to the aspx file:
    <script type=text/javascript>
    <!--
    function testFunction()
    {
     var elementId = '<%= FormView1.FindControl( "TitleTextBox").ClientID %>';
    }
    // -->
    </script>

    Bring the page up in your browser.
    Right click on the page and choose View Source
    Post the FormView code -- it'll be a table tag like <table cellspacing="0" border="0" id="FormView1" style="width:70px;border-collapse:collapse;">
    and the above JavaScript

    NC...

  • Re: Get Client ID from inside a formview

    01-12-2009, 3:00 PM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

     Thank you for your reply again.  I am probably about to show my complete stupidity, but oh well. Ok. My page is a content page that has a masterpage file. when I try to add the script outside the content place holder, it says nothing can be outside the content place holder. If I put it inside the content place holder then I get the dreaded Object Not set to an instance of an object.  Also some other info that might be helpful.  The formview is located in an ajax modal popup that is triggered when I select an item in a gridview.  Formview1 contains TitleTextBox as well as the FCKeditor control where I click the preview icon that somehow runs the javascript where need to set the value = titletextbox.  Here is where the titletextbox is in the source without being able to add the script you suggested above.  I don't know if that helps.I pulled up the source file before I clicked the preview button in the fckeditor. I was not sure if the modal popup would cause a postback.

    <span id="ctl00_AdminContent_FormView1_lblTitle" class="admintext" style="display:inline-block;width:85px;">Title:</span>
    <input name="ctl00$AdminContent$FormView1$TitleTextBox" type="text" value="Test 1 edited 4:12" id="ctl00_AdminContent_FormView1_TitleTextBox" class="textbox" style="width:480px;" />
                              *&nbsp;
                               <br />

  • Re: Get Client ID from inside a formview

    01-12-2009, 11:38 PM
    • All-Star
      59,859 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,544
    • TrustedFriends-MVPs

    Hi

    put the script in the content place holder within the script tags and then try to run.

  • Re: Get Client ID from inside a formview

    01-13-2009, 10:33 AM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

    mudassarkhan:

    Hi

    put the script in the content place holder within the script tags and then try to run.

     

    Thank you for answering.  When I put the script in the content place holder I got the following. 

    Object reference not set to an instance of an object. body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 14: <!--
    Line 15:     function testFunction() {
    Line 16:         var elementId = '<%= FormView1.FindControl( "TitleTextBox").ClientID %>';
    Line 17:     }
    Line 18: // -->


    Source File: c:\inetpub\wwwroot\secure\test.aspx    Line: 16

     

    This happens when I first try to load the page.  I don't know if this is what is causing the error, but at this point in time the formview is not on the page yet.  Since it is inside the modal popup, which contains an updatepanel, it will not be on the page until I select a record from the gridview.  I am sorry if I was unclear about the way my page is set up.  I believe I forgot to put that I had an update panel inside the AJAX modal popup control.  Could that be the reason for my problems?  I did not think it would cause a problem because when I need the data, it is on the screen. 

     

    I appreciate your patience with me. 

    Laura

  • Re: Get Client ID from inside a formview

    01-13-2009, 10:41 AM
    Answer
    • All-Star
      59,859 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,544
    • TrustedFriends-MVPs

    I said this script

    var title = document.createElement('INPUT');
        title.type = 'hidden';
        //change the name as needed -->
        title.name = 'htmlTitle';
        //set the data
        
        var FormView = document.getElementById('<%= "Formview1.ClientID"%>');

        var inputs = FormView.getElementsByTagName("input");

        for (i = 0; i < inputs.length; i++) {

            if (inputs[i].id.indexOf("TitleTextBox") != -1) {

                title.value = alert(inputs[i].value);

                break;

            }

        }
        
                     
        //append the new input to the form
        theForm.appendChild(title);

     

    And By the way where is the FormView in master page or content place header

  • Re: Get Client ID from inside a formview

    01-13-2009, 11:09 AM
    • Member
      24 point Member
    • laurav_00
    • Member since 11-28-2007, 1:27 AM
    • Posts 91

     Sorry, ok i put the above script and it says that Formview is null or not an object. 

    The formview is in the content place holder in the content page.  I did expect the error above though, becuase the formview is not yet on the page. The page I am working on is the content page, it first displays a gridview.  Then when you select an item on the gridview it calls an AJAX modal popup.  The modal popup conains an update panel which contains the formview.  The formview contains the fckeditor which is a control that is a rich text editor.  When I click the preview button on the fckeditor control, the code runs that needs the TitleTextBox.  At this point in time the formview is on the screen so I don't know why it wont find the controls. I have looked through the FCKeditor documentations and have had no luck since this is a plugin for it that I am using. 

    Thanks, I know this is probably confusing without being able to see the whole picture.  

    Laura

  • Re: Get Client ID from inside a formview

    01-13-2009, 11:14 AM
    Answer
    • All-Star
      75,329 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,007
    • TrustedFriends-MVPs

    I see. The TextBox ID'd "TitleTextBox" is inside of a FormView ID'd "FormView1" which is inside of a ContentPlaceHolder ID'd "AdminContent" Try:
         var elementId = '<%= ((AdminContent.FindControl("FormView1")).FindControl("TitleTextBox")).ClientID %>';

    NC...

Page 1 of 1 (14 items)