通过锚标记将另一个控件的clientid发送到javascript

时间:2015-01-02 10:43:25

标签: javascript jquery asp.net .net

我在asp.net中有gridview:

 <asp:TemplateField HeaderText="Shipment Received" ItemStyle-VerticalAlign="top" HeaderStyle-VerticalAlign="top">
  <ItemTemplate>
    <a href="#"  id="aShipment"  onclick="javascript:setIdRMA('<%# Eval("idRMA") %>','<%# Eval("idreturn")%>','<%# Eval("idorder")%>','<%# Eval("returnReason")%>','<%# Eval("returnFor")%>','<%# Eval("returnStatus")%>','<%# Eval("dateClosed")%>','<%# Eval("shipmentReceiveDate")%>','<%# Eval("resolution")%>','<%# Eval("sku") %>',lblShipmentReceived.ClientID);">
      <asp:Label ID="lblShipmentReceived" runat="server" Text='<%# Bind("shipmentReceived")%>'></asp:Label>
    </a>

  </ItemTemplate>
</asp:TemplateField>

点击锚标签我正在调用javascript的setIdRMA方法

我想将锚标记内的标签的客户端ID发送到该函数。

对于我写的如上所示:

lblShipmentReceived.ClientID

但这不会过去。

请帮帮我。

2 个答案:

答案 0 :(得分:1)

由于您使用的是GridView,因此您可能无法直接获得ClientID,因此您可以通过其相对推荐找到锚点标记内的label。首先按this关键字传递achor标记元素,然后在函数中访问它以查找label

onclick="setIdRMA(..,..,..,this);"

并在函数中

function setIdRMA(..,..,..,obj)
{
    var label = $(obj).find('span'); //asp.net label is rendered as span and there is only 1 label so you can use .find()
}

答案 1 :(得分:1)

.children selector用于此

并且asp:Lable在HTML网页上呈现为span,因此您可以使用span

找到它
 <asp:TemplateField HeaderText="Shipment Received" ItemStyle-VerticalAlign="top" HeaderStyle-VerticalAlign="top">
  <ItemTemplate>
    <a href="#"  id="aShipment"  onclick="javascript:setIdRMA(this,'<%# Eval("idRMA") %>','<%# Eval("idreturn")%>','<%# Eval("idorder")%>','<%# Eval("returnReason")%>','<%# Eval("returnFor")%>','<%# Eval("returnStatus")%>','<%# Eval("dateClosed")%>','<%# Eval("shipmentReceiveDate")%>','<%# Eval("resolution")%>','<%# Eval("sku") %>',lblShipmentReceived.ClientID);">
      <asp:Label ID="lblShipmentReceived" runat="server" Text='<%# Bind("shipmentReceived")%>'></asp:Label>
    </a>

  </ItemTemplate>
</asp:TemplateField>

更改您的onclick函数参数并在JavaScript函数中添加this作为第一个参数,然后更改您的javaScript函数,如下所示。

function setIdRMA(,,,){
   alert($(this).children('span').attr('id'));
}