如何使用OnChange事件生成“”以触发函数后面的C#代码
类似于SelectedIndexChanged控件的ASP.NET ASP.NET
例如
前端
<select runat="server" id="xx" onserverchange="xx_ServerChange">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
后端
protected void xx_ServerChange(object sender, EventArgs e)
{
}
PS:
1.不像这个Select server changed event,因为它必须创建另一个事件按钮。
2.不要使用asp:DropDownList
3.请不要使用Ajax或JQuery等任何重定向方法.
发布于 2018-07-18 10:33:37
我找到了一种很好的方法来处理我的问题,我用下面的代码展示了我的想法
前端
<select id="StartDate" onserverchange="StartDate_ServerChange" runat="server">
</select>
后端
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:两处参考资料 https://msdn.microsoft.com/en-us/library/ms153112(v=vs.110).aspx https://blog.csdn.net/lovegonghui/article/details/51942241
发布于 2018-07-18 07:08:55
如果我做对了,您需要使用另一个控件来触发后面的代码。只需添加按钮并访问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" />
代码背后:
protected void Button1_Click(object sender, EventArgs e)
{
int index = xx.SelectedIndex;
}
发布于 2018-07-18 07:16:55
在xx_ServerChange函数中使用Ajax调用JavaScript
$.ajax({
type: 'POST',
url: 'PageName.aspx/functionName()',
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
// Do something interesting here.
}
});
https://stackoverflow.com/questions/51395458
复制相似问题