How should I make \"Html Select Control\" with OnChange event to trigger C# code behind function
like ASP.NET SelectedIndexChanged of the DropDownList C
I've found a great way to handle my problems, showing my ideas as following code
Front End
<select id="StartDate" onserverchange="StartDate_ServerChange" runat="server">
</select>
Back End
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
this.StartDate.Attributes.Add("onchange", cs.GetPostBackEventReference(this.StartDate, this.StartDate.ID));
}
protected void StartDate_ServerChange(object sender, EventArgs e)
{
}
PS: two references
https://msdn.microsoft.com/en-us/library/ms153112(v=vs.110).aspx
https://blog.csdn.net/lovegonghui/article/details/51942241
Use Ajax call in xx_ServerChange function from JavaScript
$.ajax({
type: 'POST',
url: 'PageName.aspx/functionName()',
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
// Do something interesting here.
}
});
If I'm getting it right, you need to use another control to trigger the code behind. Just add the button and access the state of the select
<select runat="server" id="xx">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
int index = xx.SelectedIndex;
}
I think you need to submit the form for the change to be registered:
<form id="myForm">
<select runat="server" id="xx" onchange="myForm.submit()" onserverchange="xx_ServerChange">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</form>