We help IT Professionals succeed at work.

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

heincpa
heincpa asked
on
Medium Priority
1,266 Views
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 = '';
    }
    else
    {
    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>
   </div>
</td>

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

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

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

thanx,
Comment
Watch Question

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I was so close...thanx for setting it straight!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.