SoFunction
Updated on 2025-03-09

(C#) Method to add client js events to control

On the server side, you can also use ajax to implement it without swiping the page. But I think there is a more direct and simpler way, which can be achieved with a js event.
However, DropDownList Button and other controls provide some foreground events like "OnClientClick", only server events.
I thought that all C# page codes eventually generate HTML, and the js events are ultimately transported to the browser and are based on HTML. What are the js events of the HTML control that the server control finally generates? We should be able to add the corresponding events to it in aspx.
The Htm generated by DropDownList is that the element <Select> has an onchange event, so we can also add an onchange() event to DropDownList. The method we usually use is to add an onchange() event to DropDownList with id ddlExamType in the Page_Load event in the background file:
("onchange","SelecteChanged('"+"')");
Foreground defined js function: SelecteChanged()
Copy the codeThe code is as follows:

<script language="javascript">
function selectChange(objID) {
var ddlExamType = (objID);
if (bool) {
……
}
else {
……
}
}
</script>

Summary: For C# server-side control, we can add any js events to the corresponding control when generating html.