• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1242
  • Last Modified:

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

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,
0
heincpa
Asked:
heincpa
1 Solution
 
mppetersCommented:
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.
0
 
heincpaAuthor Commented:
I was so close...thanx for setting it straight!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now