我是新來的.NET,我已創建文本框,下拉列表和變奏按鈕。如何從表和顯示搜索記錄在GridView控件在C#?
當我選擇「開頭」,在下拉,並在文本框中鍵入一些字符,然後單擊搜索按鈕,它應該顯示的記錄(根據搜索文本)在GridView控件從表中。
對於我創建下拉,並創建存儲procedure.And我添加以下代碼:
下面是ASPX:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<td><asp:HiddenField ID="hiddenfield" runat="server" />
<td><asp:HiddenField ID="curricular" runat="server" />
<asp:TextBox ID="searchid" runat="server" Visible="false"></asp:TextBox>
</td>
<td>
<asp:Label ID="condition" runat="server" Visible="false" >
</asp:Label>
<asp:Label ID="searchtext" runat="server" Visible="false"></asp:Label>
</td>
<asp:DropDownList ID="searchrecord" runat="server" Width="150px"></asp:DropDownList>
<asp:TextBox ID="textsearch" runat="server"></asp:TextBox>
<asp:Button ID="searchclick" runat="server" text = "search" OnClick="searchrecords_Click" OnClientClick="return searchrecords();" />
<center><div><h4>Searched Records</h4></div></center><br /> <br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_RowCommand"
EnablePersistedSelection="True" BackColor="White"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="240px"
Width="755px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="Section" HeaderText="Section"
SortExpression="Section" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="Email" HeaderText="Email"
SortExpression="Email" />
<asp:BoundField DataField="Mobilenum" HeaderText="Mobile Number"
SortExpression="Mobile Number" />
<asp:ImageField DataImageUrlField="Image" HeaderText="Image" ControlStyle-Width="50" ControlStyle-Height = "50">
<ControlStyle Height="50px" Width="50px"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="Extracurricular" HeaderText="Extracurricular"
SortExpression="Name" />
</Columns>
<HeaderStyle BackColor="#FF0066" BorderColor="#CCFFFF" ForeColor="White"
Height="50px" Width="50px" />
<SelectedRowStyle BackColor="#FF66FF" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="sp_searchedstudentrecords"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<script type="text/javascript">
function searchrecords() {
if ($.trim(document.getElementById("<%=textsearch.ClientID%>").value).length == 0) {
alert("Enter Your Characters to search !");
document.getElementById("<%=textsearch.ClientID%>").focus();
return false;
}
}
</script>
<script type="text/javascript" src="Scripts/jquery-2.1.4.js" />
</asp:Content>
這裏是我的代碼隱藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = Connection.DBconnection();
{
SqlCommand com = new SqlCommand("sp_studentsearchrecords", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", searchid.Text.Trim());
com.Parameters.AddWithValue("@searchrecords", searchrecord.Text.Trim());
SqlDataAdapter adp = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
searchrecord.DataSource = ds;
searchrecord.DataTextField = "searchrecords";
searchrecord.DataValueField = "id";
searchrecord.DataBind();
}
}
}
}
protected void searchrecords_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
{
SqlCommand com = new SqlCommand("sp_insertsearchtext", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@searchname", searchtext.Text.Trim());
com.ExecuteNonQuery();
}
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
hiddenfield.Value = index.ToString();
}
最後的存儲過程:
ALTER PROCEDURE sp_searchedstudentrecords
(
@condition varchar(20),
@searchtext varchar(50)
)
AS
begin
If (@condition = 'startswith')
select * from student where name like @searchtext+ '% '
else if (@condition = 'endswith')
select * from student where name like '%' [email protected]
else
select * from student where name like '%' [email protected]+ '%'
End
當我運行這段代碼,它顯示了以下錯誤:
Compiler Error Message: CS1061: 'ASP.searchrecords_aspx' does not contain a definition for 'GridView1_RowCommand' and no extension method 'GridView1_RowCommand' accepting a first argument of type 'ASP.searchrecords_aspx' could be found (are you missing a using directive or an assembly reference?)
我竭力要解決這個問題,我可以知道,如何在我的代碼通過這兩個@condition
和@searchtext
參數?
任何幫助將不勝感激。
感謝,
更新:
protected void searchrecords_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
{
SqlCommand com = new SqlCommand("sp_insertsearchtext", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", idsearch.Text.Trim());
com.Parameters.AddWithValue("@searchtext", searchtext.Text.Trim());
com.ExecuteNonQuery();
}
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
hiddenfield.Value = index.ToString();
}
和
ALTER PROCEDURE sp_insertsearchtext
(
@id int,
@searchtext Varchar (100)
)
AS
begin
Insert into searchtext (searchtext) values (@searchtext)
End
從ASPX中刪除OnRowCommand =「GridView1_RowCommand」。或者將相關事件添加到代碼後面。 –