我在asp.net中有表,它使用转发器控件绑定到数据库。我使用table而不是gridview,但现在我无法弄清楚如何实现搜索代码。另外,我如何使用gridview而不是table,以便进行搜索,排序。
HTML:
<div class="container">
<asp:Repeater ID="RepeaterComp" runat="server">
<HeaderTemplate>
<table class="table table-bordered table-hover table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Category</th>
<th>Current Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("id")%></td>
<td>
<a href='<%#Eval("href")%>'>
<%#Eval("subtype")%>
</a>
</td>
<td><%#Eval("type")%></td>
<td><%#Eval("status")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
代码:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ComplaintWebsiteConnectionString"].ConnectionString);
String query = "select C.ID as complaintid, S.NAME as STATUS, 'just a string' as ID,'just a string' as href,T.Comp_Type as type,ST.Comp_SubType as subtype from Citizen_complaints C join Complaint_Type T on T.Type_ID=C.Type join Complaint_SubType ST on ST.SubType_ID = C.subtype join COMPLAINT_STATUS S on S.ID = C.STATUS_ID";
SqlCommand cmd = new SqlCommand(query, con);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
dr["href"] = "DeptComplaintStatus.aspx?id=" + dr["complaintid"].ToString();
dr["id"] = dr["complaintid"].ToString();
}
RepeaterComp.DataSource = ds;
RepeaterComp.DataBind();
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
}
}
答案 0 :(得分:2)
添加一些文本框和按钮控件。 TextBox用于输入要搜索的值,按钮用于进行此搜索。您的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindRepeater();
}
protected void BindRepeater(){
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ComplaintWebsiteConnectionString"].ConnectionString);
String query = "select C.ID as complaintid, S.NAME as STATUS, 'just a string' as ID,'just a string' as href,T.Comp_Type as type,ST.Comp_SubType as subtype from Citizen_complaints C join Complaint_Type T on T.Type_ID=C.Type join Complaint_SubType ST on ST.SubType_ID = C.subtype join COMPLAINT_STATUS S on S.ID = C.STATUS_ID";
if (Session["search"] != null)
query += " where your_field like '%" + Session["search"].ToString() + "'";
SqlCommand cmd = new SqlCommand(query, con);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
dr["href"] = "DeptComplaintStatus.aspx?id=" + dr["complaintid"].ToString();
dr["id"] = dr["complaintid"].ToString();
}
RepeaterComp.DataSource = ds;
RepeaterComp.DataBind();
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
}
}
protected void YourButton_Click(object sender, EventArgs e){
Session["search"] = YourTextbox.Text;
BindRepeater();
}
这是您问题的简单解决方案。