2015-02-11 67 views
0

我可以得到标签值改变,当我使用下面的代码传ASP.NET标签ID为Javascript

document.getElementById('<%=lblDropdownValue.ClientID %>').innerHTML =  ddl.value; 

但我想它作为一个参数传递,如下图所示,但它不会似乎工作。

<table> 
    <tbody> 
     <tr> 
      <td> 
       <asp:DropDownList id="ddlProducts" runat="server" 
      onclick="myfunction(this,'<%=lblDropdownValue.ClientID %> "')"> 

       <asp:ListItem Selected="True" Value="-1"> Please Select </asp:ListItem> 
       <asp:ListItem Value="Car"> BMW </asp:ListItem> 
       <asp:ListItem Value="Music"> MP3 </asp:ListItem> 

      </asp:DropDownList> 

      </td> 
      <td> 
       <asp:Label Text="" id="lblDropdownValue" runat="server"/> 
      </td><td></td> 
     </tr> 

    </tbody> 

</table> 
</div> 
</form> 
<script> 
    function myfunction(ddl, lblText) 
    { 
     document.getElementById("'"+lblText+"'").innerHTML = ddl.value;   
    } 
</script> 

回答

1

在后面的代码将这个:

public void Page_Load(...) { 

    ddlProducts.Attributes["onclick"] = "myfunction(...)"; 

} 

,并从ASPX删除onclick

原因是您希望将onclick附加到客户端。你现在拥有的是一个属于服务器端的属性,因此它不能正确地给客户端。

+0

DziękujęWiktor的Zychla代码 – sw2020 2015-02-11 22:56:49

0

在我后面加

protected void Page_Load(object sender, EventArgs e) 
     { 
      ddlProducts.Attributes["onclick"] = "myfunction(this,"+lblDropdownValue.ClientID +");"; 

     } 

,改变了JavaScript来

<script> 
     function myfunction(ddl, lblText) 
     {    
      lblText.innerHTML 
      = ddl.value;   
     } 
    </script>