我实际上是通过使用客户端回调方法来执行搜索功能。当用户开始输入时,GridView将立即给出结果。但是现在我遇到了问题。如何显示选择用户的结果?
这是我的编码:
List<Student> _student = new List<Student>();
private string _Callback;
partial class Student
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime? CertificationDate { get; set; }
public string ImageID { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
_student.Add(new Student() { ID = 1, Name = "Ravi", CertificationDate = DateTime.Now, ImageID = "https://lh4.googleusercontent.com/_EBZOpH5YsH0/TV7DBZr44lI/AAAAAAAAApE/G0fRAUZmKR8/s800/ChromeIssue.png" });
_student.Add(new Student() { ID = 2, Name = "TestUser", CertificationDate = DateTime.Now });
String cbReference = Page.ClientScript.GetCallbackEventReference(this,"arg", "ReceiveServerData", "context");
String callbackScript= "function CallServer(arg, context)" +"{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
}
public string GetCallbackResult()
{
return _Callback;
}
public void RaiseCallbackEvent(string eventArgument)
{
if (!string.IsNullOrEmpty(eventArgument))
{
var Query = from p in _student
where p.Name.StartsWith(eventArgument, true, null)
orderby p.ID
select p;
GridView1.DataSource = Query;
GridView1.DataBind();
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
GridView1.RenderControl(new HtmlTextWriter(sw));
_Callback = sw.ToString();
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{//?? Error occur here
this.Label1.Text = this.GridView1.SelectedIndex.ToString();
}
我的前端:
<head runat="server">
<title></title>
<script src="../js/jquery-1.4.1.js" type="text/javascript"></script>
<script language ="javascript" type="text/javascript">
$(document).ready(function () {
$("#txtUserName").keyup(function (event) {
CallServer($(this).val(), "");
});
});
function ReceiveServerData(rValue) {
document.getElementById("ResultsSpan").innerHTML = rValue;
}
</script>
</head>
<input type="text" id="txtUserName" />
<div id="ResultsSpan">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="ID ">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("ID") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%#Eval("Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
当我点击“选择”按钮时,它会显示此错误此页面的状态信息无效,可能已损坏。
如何选择名称或ID,标签也会显示结果?