简体   繁体   中英

How to Call Client-Side Code Before Server-Side Event?

<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>

How to call the javascript function before call rbEmployeeGroup_SelectedIndexChanged this code behind function?

You need to add an onclick handler to every radiobutton in your list.

This can be done in your server code before the control is rendered.

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

I figured out. Just add the onClientClick = "..."

When we want to execute client code first than server code then we need to use Onclientclick . show below is one example:

<asp:Button 
    ID="btnAlelrt"
    runat="server"
    Text="SubmitDetails"
    OnClick="btnAlelrt_Click"
    OnClientClick="return confirm('Data Already Exists ,Do you Want Update?');"
/>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM