accessing <asp:TemplateField <EditItemTemplate><asp:DropDownList via javascript

Posted on 2006-03-29
Last Modified: 2011-08-18
have a detailsview with a template field with a dropdown list.

I have attached a javascript function with:
foreach (DetailsViewRow dvr in dvEditPD.Rows)
            ((DropDownList)dvr.FindControl("ddlSource")).Attributes.Add("onchange", "OtherSource()");

and this fires...tested with a simple alert.

not sure if I should post this in the javascript forums...but....anyways.

in regular html aspx page I used the following to show/hide a text box based on selected value of ddlSource

var i=document.forms[0].elements['ddlSource'].selectedIndex
if (i==9) {
    document.getElementById('otherTextDiv').style.display = '';
    document.getElementById('otherTextDiv').style.display = 'none';

<td style="height: 25px">
   <div id="otherTextDiv" runat="server" style="DISPLAY: none" >
      <asp:TextBox id="txtOtherSource" runat="server">Plese Specify</asp:TextBox>

now that I'm using a details view I can't figure out how to get into the templatefield to hide/show the div

                        <div id="otherTextDiv" runat="server" style="DISPLAY: none">
                            <asp:TextBox id="txtOtherSource" runat="server">Plese Specify</asp:TextBox>

basically I need to show/hide a text box if selection is 'Other' but need to do it client side, no postback.

if I need to move this to javascript, let me know.

high points, cause kind of urgent

Question by:heincpa
    LVL 8

    Accepted Solution

    You should pass in the client-side ID of the control to your javascript function.

    foreach (DetailsViewRow dvr in dvEditPD.Rows)
              DropDownList ddl =  ((DropDownList)dvr.FindControl("ddlSource"))
              ddl.Attributes.Add("onchange", "OtherSource('" + ddl.ClientID + "')");

    Then use that to find the control in javascript:

    function OtherSource(clientId) {
    var i=document.forms[0].elements[clientId].selectedIndex

    Either that, or use the keyword 'this' to pass in an already instantiated instance of the control in javascript.
    LVL 1

    Author Comment

    I was so close...thanx for setting it straight!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now