我有一个ASP.Net应用程序,当我从组合中更改值时必须显示标签:
:
public partial class WebFormAJAXControls : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
string _callbackArg;
public void RaiseCallbackEvent(string eventArgument)
{
_callbackArg = eventArgument;
}
public string GetCallbackResult()
{
return _callbackArg;
}
protected void Page_Load(object sender, EventArgs e)
{
//register the name of the client side function that will be called by the server
string callbackRef = Page.ClientScript.GetCallbackEventReference(this, "args", "ClientCallbackFunction","");
//define a function used by client to call server
string callbackScript = " function MyServerCall(args){alert ('1'); " + callbackRef + " ; }";
//register the client function to the page
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyServerCall", callbackScript, true);
}
}
:
<head runat="server">
<title></title>
<script type="text/javascript">
function ClientCallbackFunction(args) {
alert(args);
LabelMessage.innerText = args;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownListChoice" runat="server" OnChanged="MyServerCall(DropDownListChoice.value)">
<asp:ListItem>Choise 1</asp:ListItem>
<asp:ListItem>Choise 2</asp:ListItem>
<asp:ListItem>Choise 3</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Label ID="labelMessage" runat="server"></asp:Label>
</div>
</form>
</body>
当我在组合框中更改选择时,程序不会执行anithing。有人可以告诉我这是什么问题吗?
答案 0 :(得分:0)
不应该这样:
function ClientCallbackFunction(args) {
alert(args);
LabelMessage.innerText = args;
}
是这样的:
function MyServerCall(args) {
alert(args);
LabelMessage.innerText = args;
}
目前,您没有MyServerCall
方法可以调用。
您想要的方法是OnChange
,而不是OnChanged
。如果以这种方式访问LabelMessage
,您也会收到错误消息。使用jQuery之类的东西可以解决这个问题:$('#labelMessage').text(args);
答案 1 :(得分:0)
尝试使用RegisterStartupScript注册客户端脚本功能。
例如:Page.ClientScript.RegisterStartupScript(typeof(Page),“MyServerCall”,callbackScript,true);