Skip to main content
 首页 » 编程设计

c#之如何在服务器端事件之前调用客户端代码

2024年02月24日31飞鱼

<asp:RadioButtonList ID="rbEmployeeGroup" runat="server" RepeatDirection="Horizontal" 
    AutoPostBack="true" OnSelectedIndexChanged=" [Call java script to check first] then rbEmployeeGroup_SelectedIndexChanged"> 
    <asp:ListItem Selected="True" Text="Employees" Value="employees"></asp:ListItem> 
    <asp:ListItem Text="Groups" Value="groups"></asp:ListItem> 
</asp:RadioButtonList> 

如何在调用 rbEmployeeGroup_SelectedIndexChanged 这个代码隐藏函数之前调用 javascript 函数?

请您参考如下方法:

您需要为列表中的每个单选按钮添加一个 onclick 处理程序。

这可以在呈现控件之前在您的服务器代码中完成。

foreach (var item in rbEmployeeGroup.Items) 
{ 
   item.Attributes.Add("onclick", "nameOfJavascriptFunction"); 
}