English 中文(简体)
• 如何获取javascript中重复标签价值
原标题:how to access repeater label value in javascript

This is my javascript

  function btnEditClick() {
    alert(document.getElementById( <%=LblRefPhyID.ClientID %> ).value);          
  }

<asp:Repeater ID="repeaterRefPhysicianList" runat="server">
  <ItemTemplate>
    <tr onclick="selectRow(this);">
      <td class="csstablelisttd" style="display: none;">
        <asp:Label ID="LblRefPhyID" runat="server" Text= <%#Eval("Ref_Phy_ID")%> ></asp:Label>  
      </td>

在Edit htoni的客户点上,不得不将RefphyId转到能够这样做的另一页。

问题回答

It s a repeater. That means that the ItemTemplate will be repeated for each item in your databound collection.

This comes with a caveat: IDs are supposed to be unique. So when you say that your asp:Label has an ID of LblRefPhyID, ASP.NET automagically does you the favor of generating unique IDs for each instance of the repeater that eventually makes its way to your generated HTML. These generated IDs will be based on your original value of LblRefPhyID, but it won t be exactly that, so a plain document.getElementById() outside of the repeater won t work.

围绕这一点有许多工作方式,而你需要做的第一步是实际起草一些考虑到自动生成的身份证的法典。 也许会用<代码>书写一些 Java字。 LblRefPhyID.ClientID, 无论怎样,都可以动态地读到onclick

EDIT

And, oh yeah, @Pointy is completely correct in stating that label elements don t have values, just their inner HTMLs. I don t get why he got downvoted for giving a correct response.

Try to set css class instead of id and bind elements click event by class name. I m using jquery for this:

$(document).ready(function(){
  //bind click event on our label class
  $( .lblRef ).live( click , function(){
    alert($(this).text());
   });
});

And this in asp.net page code:

<asp:Label CssClass="lblRef" runat="server" Text= <%#Eval("Ref_Phy_ID")%> ></asp:Label>

<超文本编码><label>s no t有“价值”。 他们确实有内容:

alert(document.getElementById( <%=LblRefPhyID.ClientID %> ).innerHTML);

Best way would be to check what is the pattern of id generated by the repeater on the client side and then you can use that id to get the value of the label using innerHTML. For instance in your case id generated may be : repeaterRefPhysicianList_LblRefPhyID_01 to till the number of rows in source.

So you can use this information with innerHTML to get the value of the label.. All in all just check your html page you will know what to do next :)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签