The method in asp.net of C to add a client js event to a control

  • 2020-05-09 18:24:40
  • OfStack

On the server side, you can also use ajax to achieve, do not brush the page. But I think there's a more direct, simpler way to do it, and you can do it with one js event.
However, controls such as DropDownList don't ket Button provide some foreground events like "OnClientClick" and only server-side events.
Think of all the C# page code that eventually generates HTML, and the js event that eventually runs in the browser, based on Html. We should be able to add an js event to the HTML control that the server control eventually generates.
Htm generated by DropDownList is an element < Select > There is an onchange event, so we can also add an onchange() event to DropDownList. In the Page_Load event of background file, we usually add an onchange() event to DropDownList where id is ddlExamType:
ddlExamType.Attributes.Add("onchange","SelecteChanged('"+this.ddlExamType.ClientID"')");
js function defined by foreground: SelecteChanged ()
 
<script language="javascript"> 
function selectChange(objID) { 
var ddlExamType = document.getElementById(objID); 
if (bool) { 
 ...  
} 
else { 
 ...  
} 
} 
</script> 

Summary: for C# server-side control, we can add any js event to the control when html is generated. Attributes.Add ().

Related articles: