开发者

How to find and modify an asp.net control with JavaScript?

This has been bothering me for quite some time. I'm simply trying to change the value of a textbox defined in asp.net, which normally works when it is not within a LoggedInTemplate.

I've searched everywhere on the net and even here, but the only thing I can find close to doing what I need is here Finding an asp:button and a开发者_开发技巧sp:textbox in Javascript. Unfortunately that doesn't work. Here's what I have so far:

<head id="Head1" runat="server">
  <script src="../../scripts/webeffects.js" type="text/javascript" language="javascript"></script>
</head>
<asp:LoginView ID="LoginView1" runat="server">
  <LoggedInTemplate>
  <div class="lotto_pick_container">
    <table runat="server" id="tblLottoPick">
      <tr><th colspan="3">Pick a Lotto Number</th></tr>
  <tr>
    <td><asp:TextBox ID="txt1stNum" runat="server"></asp:TextBox></td>
    <td><asp:TextBox ID="txt2ndNum" runat="server"></asp:TextBox></td>
    <td><asp:TextBox ID="txt3rdNum" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td><asp:Button ID="cmdSubmitPick" runat="server" Text="Submit Lotto Number" 
        onclientclick="return validateLottoPicks()" /></td>
  </tr>
</table>
  </div>
  </LoggedInTemplate>
</asp:LoginView>

Right now I'm trying to use an external js script to find a textbox and modify it with an arbitrary value of 12, just so that I know it can work:

function validateLottoPicks() {
  document.getElementById('<%= LoginView1.FindControl("txt2ndNum").ClientID %>').value = 12
}

When I debug this with Firebug it seems all my other code works, except for this one function. I have verified the function gets called, it's just this line that doesn't work. Can someone please provide some guidance? I'll send milk and cookies.

Update:

Thanks for all the help from everyone. This was my first time using Stack Overflow and I was definitely impressed by the quick responses.

It turns out my actual problem was that using <%= LoginView1.FindControl("txt2ndNum").ClientID %> does not work in external js scripts. I didn't know this. Once, I put the function in the header of my page everything worked. Now I'm going to avoid using ASP.NET controls because I don't want to deal with that again, and it makes more sense if I ever decide to use something other than ASP.NET.


The problem is that you have set visible=false on this line

<table runat="server" id="tblLottoPick" visible="false">

From that moment the controls are not rendered the javascript can not find this control. If you do not wish to show this table use css, for example style="display:none;" so its rendered but not visible. All the rest seems to me that working.

Update

From the comments also seems that this code is not inside the aspx page, so the ClientID can not work and not found. Add this somewhere in the header in the page that have this LoginView1 control.

function validateLottoPicks() {
  document.getElementById('<%= LoginView1.FindControl("txt2ndNum").ClientID %>').value = 12
}


Try

tblLottoPick.FindControl("txt2ndNum").ClientID instead.


Try putting this into a javascript string variable. What are you getting?

'<%= LoginView1.FindControl("txt2ndNum").ClientID %>'

Because you shouldn't have to do that. Can you just do this?

document.getElementById('txt2ndNum')

Also, check in firebug to see what the ID value is set to for that control (i.e. the rendered textbox). I mean see what it's rendered as. You just need to get the right id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜