网络应用程序,我需要根据某些选项>
过滤gridview数据库包含以下表格:
***********************************
user * warning
***********************************
id * id
firstname * user_id
lastname * type
fathername * content
* title
* date
* writer1
* writer2
* committeecoordinator
* youthcoordinator
user.id = warning.user_id 我需要根据4个选项进行过滤:
类型的下拉列表 来自用户的名字+父名+姓氏的下拉列表 日期的文本框 标题的文本框
我需要网格视图来显示所有信息,在更改选项之后,它会使用验证选项的新记录重新绑定
我写这些查询:
SELECT
warning.id, warning.user_id, warning.type, warning.title, warning.xcontent,
warning.xdate, warning.writer1, warning.writer2, warning.committeecoordinator,
warning.youthcoordinator,
user.firstname + ' ' + user.fathername + ' ' + user.lastname AS name
FROM warning INNER JOIN user ON warning.user_id = user.id
但不知道如何将过滤器添加到此查询
我试图添加warning.type=@warning但不起作用的地方
选项代码:
<table align="right" cellpadding="7px">
<tr>
<td align="left">
<span class="brownfont">name</span>
</td>
<td>
<asp:DropDownList ID="userlist" CssClass="select" runat="server" DataSourceID="userdatasource" DataTextField="name" DataValueField="id">
<asp:ListItem Text="" />
</asp:DropDownList>
<asp:SqlDataSource ID="userdatasource" runat="server" ConnectionString="<%$ ConnectionStrings:samy_sarc %>" SelectCommand="SELECT id, (firstname +' '+fathername+' ' +lastname) AS name FROM sarcuser"></asp:SqlDataSource>
</td>
<td width="70px">
</td>
<td align="left">
<span class="brownfont">type<span>
</td>
<td>
<asp:DropDownList ID="type" CssClass="select" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left">
<span class="brownfont">title</span>
</td>
<td>
<asp:TextBox ID="title" CssClass="textfield" runat="server"></asp:TextBox>
</td>
<td width="70px">
</td>
<td align="left">
<span class="brownfont">date</span>
</td>
<td>
<asp:TextBox ID="xdate" CssClass="NjmeDine_Date textfield" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="5" align="left">
<asp:Button class="button" ID="filter" runat="server" Text="seach" OnClick="filter_Click" />
</td>
</tr>
</table>
<asp:SqlDataSource ID="warningdatasource" runat="server" ConnectionString="<%$ ConnectionStrings:samy_sarc %>" DeleteCommand="DELETE FROM sarcwarning WHERE (id = @id)" SelectCommand="SELECT sarcwarning.id, sarcwarning.user_id, sarcwarning.type, sarcwarning.title, sarcwarning.xcontent, sarcwarning.xdate, sarcwarning.writer1, sarcwarning.writer2, sarcwarning.committeecoordinator, sarcwarning.youthcoordinator, sarcuser.firstname + ' ' + sarcuser.fathername + ' ' + sarcuser.lastname AS name FROM sarcwarning INNER JOIN sarcuser ON sarcwarning.user_id = sarcuser.id ">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="userlist" DefaultValue="%" Name="user_id" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="title" DefaultValue="%" Name="title" PropertyName="Text" />
<asp:ControlParameter ControlID="type" DefaultValue="%" Name="type" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="xdate" DefaultValue="%" Name="xdate" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
请你帮帮我
答案 0 :(得分:1)
编辑:您希望在页面加载中显示GridView中的所有数据。所以你应该删除所有
来自SqlDataSource的SelectParameters
并更改SelectCommand
。您的SqlDataSource
应如下所示:
<asp:SqlDataSource ID="warningdatasource" runat="server"
ConnectionString="<%$ ConnectionStrings:samy_sarc %>"
ProviderName="<%$ ConnectionStrings:samy_sarc.ProviderName %>"
DeleteCommand="DELETE FROM warning WHERE (id = @id)"
SelectCommand= "SELECT w.id, w.user_id, w.type, w.title, w.content, w.date, w.writer1,
w.writer2, w.committeecoordinator, w.youthcoordinator,
u.firstname + ' ' + u.fathername + ' ' + u.lastname AS name
FROM warning w
INNER JOIN [user] u ON w.user_id = u.id"
>
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
</asp:SqlDataSource>
请记住,在页面加载中,这将被执行,而不需要在任何地方调用它。
现在,在过滤器按钮上单击,您要过滤记录。我有以下代码:
protected void filter_Click(object sender, EventArgs e)
{
Filter();
}
protected void Filter()
{
warningdatasource.SelectCommand= @"SELECT w.id, w.user_id, w.type, w.title, w.content, w.date, w.writer1,
w.writer2, w.committeecoordinator, w.youthcoordinator,
u.firstname + ' ' + u.fathername + ' ' + u.lastname AS name
FROM warning w
INNER JOIN [user] u ON w.user_id = u.id
Where u.id = @user_id
AND w.type = @type
AND w.title = @title
AND w.date = @xdate";
if (warningdatasource.SelectParameters.Count > 0)
{
warningdatasource.SelectParameters["user_id"].DefaultValue = userlist.SelectedValue;
warningdatasource.SelectParameters["type"].DefaultValue = type.SelectedValue;
warningdatasource.SelectParameters["title"].DefaultValue = title.Text;
warningdatasource.SelectParameters["xdate"].DefaultValue = xdate.Text;
}
else
{
warningdatasource.SelectParameters.Add("user_id", TypeCode.Int32, userlist.SelectedValue);
warningdatasource.SelectParameters.Add("type", TypeCode.Int32, type.SelectedValue);
warningdatasource.SelectParameters.Add("title", TypeCode.String, title.Text);
warningdatasource.SelectParameters.Add("xdate", TypeCode.DateTime, xdate.Text);
}
}
现在我得到了预期的结果:
您可以下载我的测试项目here。
答案 1 :(得分:0)
我在代码中发现了错误: 问题是我比较user_id从数据库(int)和控制选择值(字符串)
所以正确的解决方案是:
其中(user_id +''喜欢@id)
<asp:SqlDataSource ID="warningdatasource" runat="server"
ConnectionString="<%$ ConnectionStrings:samy_sarc %>"
ProviderName="<%$ ConnectionStrings:samy_sarc.ProviderName %>"
DeleteCommand="DELETE FROM sarcwarning WHERE (id = @id)"
SelectCommand= "SELECT w.id, w.user_id, w.type, w.title, w.xcontent, w.xdate, w.writer1, w.writer2, w.committeecoordinator, w.youthcoordinator, u.firstname + ' ' + u.fathername + ' ' + u.lastname AS name FROM sarcwarning AS w INNER JOIN sarcuser AS u ON w.user_id = u.id WHERE (w.user_id +'' like @user_id) AND (w.type like @type) AND (w.title like @title) AND (w.xdate like @xdate)"
>
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="userlist" DefaultValue="%" Name="user_id" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="type" DefaultValue="%" Name="type" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="title" DefaultValue="%" Name="title" PropertyName="Text" />
<asp:ControlParameter ControlID="xdate" DefaultValue="%" Name="xdate" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
惊人的ASP