using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Test_CallServar : System.Web.UI.Page, ICallbackEventHandler
{
private string Area="";
protected void Page_Load(object sender, EventArgs e)
{
} private void aa() {
this.DropDownList1.Items.Add(new ListItem("1", "1"));
}
#region ICallbackEventHandler 成员
public string GetCallbackResult()
{
return Area;
} public void RaiseCallbackEvent(string eventArgument)
{
aa();
}
#endregion
}
这样为什么不能给DropDownList1添加项呢
我是在一个按钮的onclick事件里面写<%= Page.ClientScript.GetCallbackEventReference(this, "areaid", "Show", "context") %>;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Test_CallServar : System.Web.UI.Page, ICallbackEventHandler
{
private string Area="";
protected void Page_Load(object sender, EventArgs e)
{
} private void aa() {
this.DropDownList1.Items.Add(new ListItem("1", "1"));
}
#region ICallbackEventHandler 成员
public string GetCallbackResult()
{
return Area;
} public void RaiseCallbackEvent(string eventArgument)
{
aa();
}
#endregion
}
这样为什么不能给DropDownList1添加项呢
我是在一个按钮的onclick事件里面写<%= Page.ClientScript.GetCallbackEventReference(this, "areaid", "Show", "context") %>;
public void RaiseCallbackEvent(string eventArgument)
{
aa();
}
加了又怎么样呢?问题是服务器端并没有重新生成DropDownList对应客户端的<select><option>...这样的html up.
你叫客户端怎么显示啊!
你只是返回了Area而已——一个空串。通常需要返回新的控件的Render到HtmlTextWriter中的输出(作为字符串),你的客户端的Show方法从参数接收到Area的值然后写入相应的dhtml控件的InnerHTML属性。或者,也可以将脚本写入Area,然后由Show方法在客户端Eval执行脚本。不过,客户端回调根本不会保留ViewState,这就造成你的程序如果稍微复杂,编程就低级了(因为你必须自己重新一套asp.net状态机制)。
你应该使用UpdatePanel,把客户端回调技术忘记吧。
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
string strItem = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "scriptKey"))
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptKey", "function addItem(item, context) { " + Page.ClientScript.GetCallbackEventReference(this, "item", "clientCallback", "context",true) + "}", true);
} #region ICallbackEventHandler 成员 public string GetCallbackResult()
{
return strItem;
} public void RaiseCallbackEvent(string eventArgument)
{
strItem = eventArgument;
this.DropDownList1.Items.Add(new ListItem(strItem));
} #endregion
}<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[function Button1_onclick() {
addItem(document.getElementById('<%= TextBox1.ClientID %>').value, null);
}
function clientCallback(result)
{
var ddl = document.getElementById('<%= DropDownList1.ClientID %>');
ddl.options.add(new Option(result));
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
</div>
</form>
</body>
</html>