如何为 gridview 提供多个控件?

发布于 2024-11-03 00:51:10 字数 158 浏览 1 评论 0原文

例如,我有一个网格视图和两个文本框。

一个文本框用于输入要搜索的文本。 第二个文本框是要搜索的订单号。

我希望我的 gridview 根据其中之一进行填充。我不知道如何告诉我的表单,如果用户正在使用数字搜索,如果是名称,则按名称搜索。

感谢您的任何帮助。

For example, I have a gridview and two textboxes.

One textbox is for the text to search for.
The second textbox is an order number to search for.

I want my gridview to populate based on one or the other. I don't know how to tell my form if the user is using a number search by that and if a name, instead search by that.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿光 2024-11-10 00:51:10

好吧,希望你还没有解决这个问题,因为我花了几分钟想出了一个例子,我认为它几乎可以满足你的要求。

DB 访问使用存储过程,但您可以将 ObjectDataSource 与 DAL 一起使用,或者仅在 SqlDataSource 上内联 SQL 语句等。

标记:

Product ID:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="You must enter a number"
    ValidationGroup="vg1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<br />
Description:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="cmdSearch" runat="server" Text="Search" ValidationGroup="vg1" /><br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="spGetProducts"
    CancelSelectOnNullParameter="False" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" PropertyName="Text" DbType="String" DefaultValue="" />
        <asp:ControlParameter ControlID="TextBox2" PropertyName="Text" DbType="Int32" DefaultValue="" />
    </SelectParameters>
</asp:SqlDataSource>

以及用于查询的 T-SQL:

CREATE PROCEDURE spGetProducts
    @ProductId int = NULL
    ,@ProductDescription nvarchar(100) = NULL
AS
BEGIN
    SELECT [ProductId]
        ,[ProductDescription]
    FROM [Products]
    WHERE (
        (
            (@ProductId IS NULL)
            OR
            ([ProductId] LIKE % + @ProductId + %)
        )
        AND
        (
            (@ProductDescription IS NULL)
            OR
            ([ProductDescription] LIKE % + @ProductDescription + %;)
        )
    );
END

如果用户未在任一字段中输入任何内容,由于 SqlDataSource.CancelSelectOnNullParameter = False,SqlDataSource 仍将绑定,但由于设置了 ControlParameter.DefaultValue,空参数不会随查询一起发送。然后,存储过程会将 NULL 值插入到参数中,并基本上跳过 WHERE 子句中的该部分过滤。

希望这有帮助。

OK hope you haven't solved this yet because I took a few minutes to come up with an example that I think will do pretty much what you want.

DB access uses a stored procedure but you can use a ObjectDataSource with DAL, or just inline the SQL statement on the SqlDataSource, etc.

Markup:

Product ID:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="You must enter a number"
    ValidationGroup="vg1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<br />
Description:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="cmdSearch" runat="server" Text="Search" ValidationGroup="vg1" /><br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="spGetProducts"
    CancelSelectOnNullParameter="False" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" PropertyName="Text" DbType="String" DefaultValue="" />
        <asp:ControlParameter ControlID="TextBox2" PropertyName="Text" DbType="Int32" DefaultValue="" />
    </SelectParameters>
</asp:SqlDataSource>

And T-SQL for your query:

CREATE PROCEDURE spGetProducts
    @ProductId int = NULL
    ,@ProductDescription nvarchar(100) = NULL
AS
BEGIN
    SELECT [ProductId]
        ,[ProductDescription]
    FROM [Products]
    WHERE (
        (
            (@ProductId IS NULL)
            OR
            ([ProductId] LIKE % + @ProductId + %)
        )
        AND
        (
            (@ProductDescription IS NULL)
            OR
            ([ProductDescription] LIKE % + @ProductDescription + %;)
        )
    );
END

If the user doesn't enter anything in either of the fields, the SqlDataSource will still bind due to SqlDataSource.CancelSelectOnNullParameter = False but the empty parameter will not be sent with the query due to ControlParameter.DefaultValue being set. The stored procedure will then insert the NULL value into the parameter and basically skip that part of the filtering in the WHERE clause.

Hope this helps.

‘画卷フ 2024-11-10 00:51:10

您可以使用 (TextBox1.Text.Trim.Length > 0) 或 (TextBox1.Text = "") 检查文本框

You can check the textboxes by using (TextBox1.Text.Trim.Length > 0) or (TextBox1.Text = "")

摇划花蜜的午后 2024-11-10 00:51:10

听起来您真正要问的是如何根据多个可能的过滤参数来过滤数据源。解释这一点需要知道您的数据源是什么。不管怎样,gridview 只会显示过滤后的结果,对吗?

如果您使用 SQL 作为数据源,该技术将与过滤内存中的集合完全不同。因此,更多有关这方面的信息将会有所帮助。

It sounds like what you are really asking is how do you filter your data source based on more than one possible filter parameter. Explaining that would require knowing what your data source is. Either way, the gridview is just going to display the filtered results, right?

If you are using SQL for your data source the technique is going to be totally different than filtering a collection in memory. So more information on that would be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文