由用户触发
function oncallbacknobtn()
{
callserver(document.getelementbyid('txtusername').value,"");
}
public partial class webpage_callbackbtn : system.web.ui.page, icallbackeventhandler
{
protected string struserinfo; //callback最终得到的信息
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
}
protected void page_load(object sender, eventargs e)
{
string cbreference = page.clientscript.getcallbackeventreference(this,"arg", "receiveserverdata", null);
//page.clientscript.getcallbackeventreference(this,"arg", "receiveserverdata", null);
string callbackscript;
callbackscript = "function callserver(arg,context){" + cbreference + "};";
//string callbackscript = "function callserver(arg,context){webform_docallback('__page',arg,receiveserverdata,null,null,false)};";
page.clientscript.registerclientscriptblock(this.gettype(), "callserver123", callbackscript, true);
}