开发者

How do you have multiple controls for a gridview?

开发者 https://www.devze.com 2023-03-01 02:55 出处:网络
For example, I have a gridview and two textboxes.开发者_如何学运维 One textbox is for the text to search for.

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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号