Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to pass the ID of a text box in a gridview to a javascript

how to pass the ID of a text box in a gridview to a javascript

Scheduled Pinned Locked Moved ASP.NET
javajavascriptcsstoolstutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arindam Datta
    wrote on last edited by
    #1

    i am developing a web based application where i use i want to pass textbox ID which is in a grid view to the java script. how it can be possible Sourav ghosh

    I E J 3 Replies Last reply
    0
    • A Arindam Datta

      i am developing a web based application where i use i want to pass textbox ID which is in a grid view to the java script. how it can be possible Sourav ghosh

      I Offline
      I Offline
      Imran Khan Pathan
      wrote on last edited by
      #2

      Don't repost. Check Ignore HTML tags check box to post HTML code

      please don't forget to vote on the post that helped you.

      1 Reply Last reply
      0
      • A Arindam Datta

        i am developing a web based application where i use i want to pass textbox ID which is in a grid view to the java script. how it can be possible Sourav ghosh

        E Offline
        E Offline
        eyeseetee
        wrote on last edited by
        #3

        Would you not just use var myValue = document.getElementbyID('mytextBox').value;

        J A 2 Replies Last reply
        0
        • E eyeseetee

          Would you not just use var myValue = document.getElementbyID('mytextBox').value;

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          No, because a GridView is a NamingContainer so will give a UniqueID to your textbox.

          E 1 Reply Last reply
          0
          • A Arindam Datta

            i am developing a web based application where i use i want to pass textbox ID which is in a grid view to the java script. how it can be possible Sourav ghosh

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Please give more info about what you are trying to achieve, as often you dont even need the ID of the textbox. If you do, a textbox has a UniqueID property which you can use from Javascript

            A 1 Reply Last reply
            0
            • E eyeseetee

              Would you not just use var myValue = document.getElementbyID('mytextBox').value;

              A Offline
              A Offline
              Arindam Datta
              wrote on last edited by
              #6

              <asp:GridView id="GridView1" runat="server" Font-Size="X-Small" ForeColor="#333333" Width="802px" AllowPaging="True" GridLines="None" CellPadding="4" ShowFooter="false" AutoGenerateColumns="False" CssClass="grid-view" OnDataBound="GridView1_DataBound">
              <EmptyDataTemplate>
              <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="Red" Text="No Data Found"></asp:Label>
              </EmptyDataTemplate>
              <Columns>
              <asp:BoundField DataField="ORDER_NO" HeaderText="ORD_NO" ></asp:BoundField>
              <asp:BoundField DataField="ITEM_NO" HeaderText="ITEM_NO" ></asp:BoundField>
              <asp:BoundField DataField="BALTONS" HeaderText="BALTONS" ></asp:BoundField>
              <asp:BoundField DataField="ORDDATE" HeaderText="ORDDATE" ></asp:BoundField>
              <asp:BoundField DataField="DUEDATE" HeaderText="DUEDATE" ></asp:BoundField>
              <asp:BoundField DataField="MATNR" HeaderText="MAT_NO" ></asp:BoundField>
              <asp:BoundField DataField="DESCRIPTION" HeaderText="MAT_DESCP"><HeaderStyle Width="5000px" /><ItemStyle Width="5000px" /></asp:BoundField>
              <asp:TemplateField HeaderText="PLANQTY">
              <ItemTemplate>

                      <asp:TextBox ID="txtplanqty" runat="server" BackColor="#ffffcc" OnTextChanged="txtplanqty\_TextChanged" AutoPostBack="true" Font-Size="x-Small" Text='<%# Eval("PLAN\_QTY") %>' Width="70px"></asp:TextBox>
                      
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="TRGT\_PRD">
                    <ItemTemplate>
                    
                          <asp:TextBox ID="txttrgtprd" runat="server" Font-Size="X-Small" onfocus="getdata(this)" OnTextChanged="txttrgtprd\_TextChanged" BackColor="#ffffcc"  Width="70px" Text='<%# Eval("TRGT\_PRD") %>' AutoPostBack="true"></asp:TextBox>
                     
                    </ItemTemplate>
                    </asp:TemplateField>
                     </Columns>
                     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                     <RowStyle BackColor="#EFF3FB" />
                     <EditRowStyle BackColor="#2461BF" />
                     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
              
              J 1 Reply Last reply
              0
              • J J4amieC

                Please give more info about what you are trying to achieve, as often you dont even need the ID of the textbox. If you do, a textbox has a UniqueID property which you can use from Javascript

                A Offline
                A Offline
                Arindam Datta
                wrote on last edited by
                #7

                <pre>
                <asp:GridView id="GridView1" runat="server" Font-Size="X-Small" ForeColor="#333333" Width="802px" AllowPaging="True" GridLines="None" CellPadding="4" ShowFooter="false" AutoGenerateColumns="False" CssClass="grid-view" OnDataBound="GridView1_DataBound">
                <EmptyDataTemplate>
                <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="Red" Text="No Data Found"></asp:Label>
                </EmptyDataTemplate>
                <Columns>
                <asp:BoundField DataField="ORDER_NO" HeaderText="ORD_NO" ></asp:BoundField>
                <asp:BoundField DataField="ITEM_NO" HeaderText="ITEM_NO" ></asp:BoundField>
                <asp:BoundField DataField="BALTONS" HeaderText="BALTONS" ></asp:BoundField>
                <asp:BoundField DataField="ORDDATE" HeaderText="ORDDATE" ></asp:BoundField>
                <asp:BoundField DataField="DUEDATE" HeaderText="DUEDATE" ></asp:BoundField>
                <asp:BoundField DataField="MATNR" HeaderText="MAT_NO" ></asp:BoundField>
                <asp:BoundField DataField="DESCRIPTION" HeaderText="MAT_DESCP"><HeaderStyle Width="5000px" /><ItemStyle Width="5000px" /></asp:BoundField>
                <asp:TemplateField HeaderText="PLANQTY">
                <ItemTemplate>

                        &lt;asp:TextBox ID="txtplanqty" runat="server" BackColor="#ffffcc" OnTextChanged="txtplanqty\_TextChanged" AutoPostBack="true" Font-Size="x-Small" Text='&lt;%# Eval("PLAN\_QTY") %&gt;' Width="70px"&gt;&lt;/asp:TextBox&gt;
                        
                      &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="TRGT\_PRD"&gt;
                      &lt;ItemTemplate&gt;
                      
                            &lt;asp:TextBox ID="txttrgtprd" runat="server" Font-Size="X-Small" onfocus="getdata(this)" OnTextChanged="txttrgtprd\_TextChanged" BackColor="#ffffcc"  Width="70px" Text='&lt;%# Eval("TRGT\_PRD") %&gt;' AutoPostBack="true"&gt;&lt;/asp:TextBox&gt;
                       
                      &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                       &lt;/Columns&gt;
                       &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White
                
                1 Reply Last reply
                0
                • J J4amieC

                  No, because a GridView is a NamingContainer so will give a UniqueID to your textbox.

                  E Offline
                  E Offline
                  eyeseetee
                  wrote on last edited by
                  #8

                  Yeah forgot about that, he would have to find the control within the gridview.

                  1 Reply Last reply
                  0
                  • A Arindam Datta

                    <asp:GridView id="GridView1" runat="server" Font-Size="X-Small" ForeColor="#333333" Width="802px" AllowPaging="True" GridLines="None" CellPadding="4" ShowFooter="false" AutoGenerateColumns="False" CssClass="grid-view" OnDataBound="GridView1_DataBound">
                    <EmptyDataTemplate>
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="Red" Text="No Data Found"></asp:Label>
                    </EmptyDataTemplate>
                    <Columns>
                    <asp:BoundField DataField="ORDER_NO" HeaderText="ORD_NO" ></asp:BoundField>
                    <asp:BoundField DataField="ITEM_NO" HeaderText="ITEM_NO" ></asp:BoundField>
                    <asp:BoundField DataField="BALTONS" HeaderText="BALTONS" ></asp:BoundField>
                    <asp:BoundField DataField="ORDDATE" HeaderText="ORDDATE" ></asp:BoundField>
                    <asp:BoundField DataField="DUEDATE" HeaderText="DUEDATE" ></asp:BoundField>
                    <asp:BoundField DataField="MATNR" HeaderText="MAT_NO" ></asp:BoundField>
                    <asp:BoundField DataField="DESCRIPTION" HeaderText="MAT_DESCP"><HeaderStyle Width="5000px" /><ItemStyle Width="5000px" /></asp:BoundField>
                    <asp:TemplateField HeaderText="PLANQTY">
                    <ItemTemplate>

                            <asp:TextBox ID="txtplanqty" runat="server" BackColor="#ffffcc" OnTextChanged="txtplanqty\_TextChanged" AutoPostBack="true" Font-Size="x-Small" Text='<%# Eval("PLAN\_QTY") %>' Width="70px"></asp:TextBox>
                            
                          </ItemTemplate>
                          </asp:TemplateField>
                          <asp:TemplateField HeaderText="TRGT\_PRD">
                          <ItemTemplate>
                          
                                <asp:TextBox ID="txttrgtprd" runat="server" Font-Size="X-Small" onfocus="getdata(this)" OnTextChanged="txttrgtprd\_TextChanged" BackColor="#ffffcc"  Width="70px" Text='<%# Eval("TRGT\_PRD") %>' AutoPostBack="true"></asp:TextBox>
                           
                          </ItemTemplate>
                          </asp:TemplateField>
                           </Columns>
                           <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                           <RowStyle BackColor="#EFF3FB" />
                           <EditRowStyle BackColor="#2461BF" />
                           <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                           <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    
                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    function getdata(obj)
                    {
                    var txt = document.getElementById(obj);
                    txt.value='0';
                    }

                    note in the above snippet that you are passing a reference to the textbox (this) to the javascript method, hence you dont need the getElementById at all - you already have a reference to the element. therefore, your javascript method can simply be

                    function getdata(obj)
                    {
                    obj.value='0';
                    }

                    or your textbox's onfocus could simply be: onfocus="this.value='0';"

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups