如果我有Web用戶控制(ASP.NET),如何將多個對象從標記傳遞給javascript函數?見下圖控制調用ShowList下...將多個控件傳遞給WebUserControl中的Javascript函數
<script type="text/javascript">
function ShowList(txt, lst) {
var t = document.getElementById(t.id);
var l = document.getElementById(l.id);
l.style.display = 'block';
}
</script>
<asp:Panel ID="Wrapper" runat="server" Width="200px">
<asp:TextBox ID="TextBox1" runat="server" Width="150px"></asp:TextBox>
<asp:Image ID="Image1" runat="server" CssClass="image" ImageUrl="~/Images/cb-button-b3.png"
onclick='<%="ShowList("TextBox1,ListBox1)"%>'>
</asp:Image>
<asp:ListBox ID="ListBox1" runat="server" CssClass="listbox">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:ListBox>
</asp:Panel>
感謝您的回覆。我編輯了標記,但現在出現此錯誤「Microsoft JScript運行時錯誤:'TextBox1'未定義」。我認爲它試圖在參數前添加用戶控件的實例名稱。像UserControl1_TextBox1等... – TroyS 2011-04-18 17:21:36
至於爲什麼我用<%...%>是我最初試圖做到這一點'%=「ShowList(」+ Me.ClientID +「_TextBox1,」+ Me.ClientID +「_ListBox1)」%> 」。我忘了發佈它。 – TroyS 2011-04-18 17:26:56
我認爲這會從VB.NET代碼中更好地完成。在您的Page_Load事件中:'Image1.Attributes.Add(「onclick」,「ShowList(」+ TextBox1.ClientID +「,」+ ListBox1.ClientID +「)」)' – 2011-04-18 18:01:33