asp.net中使用callback
时间:2011-03-11 来源:象牛
①由用户触发
- 在button的 onclick 中写入:
onclick="<%= ClientScript.GetCallbackEventReference(this,"document.getElementById('txtUserName').value","onCallBack",null) %>" - 在 xxx.aspx.cs 中继承 ICallbackEventHandler 并实现其方法。
public partial class webPage_callBackBtn : System.Web.UI.Page, ICallbackEventHandler
{
protected string strUserInfo;
protected void Page_Load(object sender, EventArgs e)
{
}
#region ICallbackEventHandler 成员
public string GetCallbackResult()
{
return strUserInfo;
}
public void RaiseCallbackEvent(string eventArgument)
{
if (eventArgument == "") return;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["nowthWindConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10)
.Value = eventArgument;
cmd.CommandText = "SELECT EmployeeID, LastName FROM Employees WHERE FirstName=@FirstName";
cmd.Connection = conn;
SqlDataReader reader;
ConnectionState previousConnectionState = conn.State;
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
reader = cmd.ExecuteReader();
using (reader)
{
while (reader.Read())
{
// Process SprocResults datareader here.
strUserInfo += reader[0];
}
}
strUserInfo += "###";
}
finally
{
if (previousConnectionState == ConnectionState.Closed)
{
conn.Close();
}
}
}
#endregion
}
相关阅读 更多 +