Access HTML control in Javascript

Discussion in 'ASP .Net' started by Ben, Jun 19, 2006.

  1. Ben

    Ben Guest

    Hi,

    I have a ASP page which applied default master page style.

    I added a html text control in the ContentPlaceHolder and try to access
    it in javascript.
    I always got a null value of document.form2.DataType.value.
    ('document.form2.DataType' is null or not an object)
    Could anyone point out error in the following code?

    Thanks,

    Ben

    ----------------Begin of ASPX file-----------------
    <%@ Page Language="VB" Transaction="Required"
    MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
    CodeFile="dataPage.aspx.vb" Inherits="DataPage" title="xxxx" %>

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">

    <form id="form2">
    <div>
    <INPUT id="DataType" type=text />
    </div>
    </form>

    <script language="javascript">

    function ShowPopup()
    {

    document.form2.DataType.value = "Raw Data"; //

    return true;
    }
    </script>

    </asp:Content>
    ----------------End of ASPX file-----------------
     
    Ben, Jun 19, 2006
    #1
    1. Advertising

  2. Ben

    Ben Guest

    I found that it works with 'document.forms[0].DataType.value'.

    Ben
     
    Ben, Jun 19, 2006
    #2
    1. Advertising

  3. Hi Ben,

    I'm not sure why you're adding a form since ASP.NET already has one. Try
    something like this?

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled
    Page" %>
    <asp:content id="Content2" runat="Server"
    contentplaceholderid="ContentPlaceHolder1">
    <div>
    <input id="DataType" type="text" />
    </div>
    <script language="javascript">
    document.forms[0].DataType.value = "Raw Data"; //
    </script>
    </asp:content>


    Ken
    Microsoft MVP [ASP.NET]


    "Ben" <> wrote in message
    news:...
    > Hi,
    >
    > I have a ASP page which applied default master page style.
    >
    > I added a html text control in the ContentPlaceHolder and try to access
    > it in javascript.
    > I always got a null value of document.form2.DataType.value.
    > ('document.form2.DataType' is null or not an object)
    > Could anyone point out error in the following code?
    >
    > Thanks,
    >
    > Ben
    >
    > ----------------Begin of ASPX file-----------------
    > <%@ Page Language="VB" Transaction="Required"
    > MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
    > CodeFile="dataPage.aspx.vb" Inherits="DataPage" title="xxxx" %>
    >
    > <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    >
    > <form id="form2">
    > <div>
    > <INPUT id="DataType" type=text />
    > </div>
    > </form>
    >
    > <script language="javascript">
    >
    > function ShowPopup()
    > {
    >
    > document.form2.DataType.value = "Raw Data"; //
    >
    > return true;
    > }
    > </script>
    >
    > </asp:Content>
    > ----------------End of ASPX file-----------------
    >
     
    Ken Cox [Microsoft MVP], Jun 19, 2006
    #3
  4. Ben

    Ben Guest

    Thanks, Ken
    Your code works fine. The reason is that I want to access control by
    form name instead of form index.

    Bin


    Ken Cox [Microsoft MVP] wrote:
    > Hi Ben,
    >
    > I'm not sure why you're adding a form since ASP.NET already has one. Try
    > something like this?
    >
    > <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled
    > Page" %>
    > <asp:content id="Content2" runat="Server"
    > contentplaceholderid="ContentPlaceHolder1">
    > <div>
    > <input id="DataType" type="text" />
    > </div>
    > <script language="javascript">
    > document.forms[0].DataType.value = "Raw Data"; //
    > </script>
    > </asp:content>
    >
    >
    > Ken
    > Microsoft MVP [ASP.NET]
     
    Ben, Jun 19, 2006
    #4
  5. Hi Ben,

    You'd want to use the form name as it was in the master, not a form that you
    add yourself. That would just confuse ASP.NET.

    Ken

    "Ben" <> wrote in message
    news:...
    > Thanks, Ken
    > Your code works fine. The reason is that I want to access control by
    > form name instead of form index.
    >
    > Bin
    >
    >
    > Ken Cox [Microsoft MVP] wrote:
    >> Hi Ben,
    >>
    >> I'm not sure why you're adding a form since ASP.NET already has one. Try
    >> something like this?
    >>
    >> <%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
    >> Title="Untitled
    >> Page" %>
    >> <asp:content id="Content2" runat="Server"
    >> contentplaceholderid="ContentPlaceHolder1">
    >> <div>
    >> <input id="DataType" type="text" />
    >> </div>
    >> <script language="javascript">
    >> document.forms[0].DataType.value = "Raw Data"; //
    >> </script>
    >> </asp:content>
    >>
    >>
    >> Ken
    >> Microsoft MVP [ASP.NET]

    >
     
    Ken Cox [Microsoft MVP], Jun 20, 2006
    #5
  6. Ben

    JohnPantoo

    Joined:
    Sep 19, 2011
    Messages:
    3
    Re: Creating html control dynamically in javascript

    :listen:Hi Everyone,
    Here some snippet code for creating html control dynamically in javascript....
    <script type="text/javascript"> function createUserInterface() {
    document.write("<form id='form1'>");
    document.write("Employee Id :&nbsp;&nbsp; <input type='text' id='tt1' /> <br />");
    document.write("Employee Name :&nbsp;&nbsp; <input type='text' id='tt2' /><br />");
    document.write("Employee Age :&nbsp;&nbsp; <input type='text' id='tt3' /><br />");
    document.write("Employee City :&nbsp; <select id='selectCity'><option value='Allahabd'>Allahabd</option>");
    document.write("<option value='Lucknow'>Lucknow</option><option value='Kanpur'>Kanpur</option></select><br />");
    document.write("<input type='button' id='btnSubmit' value='Submit' />");
    document.write("</form>");
    }

    </script>


    for more details please check out the following link......
    http://mindstick.com/Articles/ddeb7...97374e9b0/?Creating html controls dynamically


    :yell:Thanks !!!
     
    JohnPantoo, Sep 21, 2011
    #6
    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. Matthew Louden
    Replies:
    1
    Views:
    7,006
    Scott M.
    Oct 11, 2003
  2. =?Utf-8?B?c2VyZ2UgY2FsZGVyYXJh?=

    Help on HTML server control vs HTML control

    =?Utf-8?B?c2VyZ2UgY2FsZGVyYXJh?=, Sep 15, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    2,159
    =?Utf-8?B?c2VyZ2UgY2FsZGVyYXJh?=
    Sep 15, 2005
  3. xz
    Replies:
    9
    Views:
    544
    James Kanze
    Jan 8, 2008
  4. Salim Afþar

    access html control with javascript

    Salim Afþar, Aug 28, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    147
    Salim Afþar
    Aug 28, 2003
  5. Joe Stampf
    Replies:
    0
    Views:
    125
    Joe Stampf
    Dec 15, 2003
Loading...

Share This Page