仅在按钮单击时填充gridview

时间:2015-02-08 15:46:23

标签: asp.net vb.net gridview

我的页面包含4个文本框(查询过滤器),提交按钮和gridview。 gridview数据源配置为从4个文本框中获取文本并过滤查询结果。这部分效果很好。

这个问题的出现是为了使查询正常运行,我不能有空文本框或查询不会起作用。必须输入一些内容,或输入通配符%。

为了对我的用户友好,我尝试为gridview数据源配置中的4个控制源(文本框)中的每一个设置默认值%。但是,由于gridview会自动填充页面加载,因此所有4个控件都设置为通配符,因此无法正常工作。当你有一个拥有数十万行的数据库......

TL / DR:我希望我的gridview只在按钮点击时填充,我希望空白文本框被接受为通配符(%)。我尝试了几个关于IsPostBack的不同代码信息,但他们似乎没有做任何事情。语言= VB。任何你好人提供的提示都将非常感激。 asp下面:

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TaxRollConnectionString %>" SelectCommand="SELECT [OWNER], [ADDRESS1], [ADDRESS2], [ADDRESS3], [CITY], [STATE], [ZIP], [WELL], [YEARBEGAN], [OPERATOR], [SURVEY], [ACRES], [TYPE], [INTEREST], [VALUE], [YEAR], [COUNTY] FROM [Owner] WHERE (([OWNER] LIKE '%' + @OWNER + '%') AND ([WELL] LIKE '%' + @WELL + '%') AND ([OPERATOR] LIKE '%' + @OPERATOR + '%') AND ([COUNTY] LIKE '%' + @COUNTY + '%'))">
        <SelectParameters>
            <asp:ControlParameter ControlID="textOwner" Name="OWNER" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="textWell" Name="WELL" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="textOp" Name="OPERATOR" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="textCo" Name="COUNTY" PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="OWNER" HeaderText="OWNER" SortExpression="OWNER" />
            <asp:BoundField DataField="ADDRESS1" HeaderText="ADDRESS1" SortExpression="ADDRESS1" />
            <asp:BoundField DataField="ADDRESS2" HeaderText="ADDRESS2" SortExpression="ADDRESS2" />
            <asp:BoundField DataField="ADDRESS3" HeaderText="ADDRESS3" SortExpression="ADDRESS3" />
            <asp:BoundField DataField="CITY" HeaderText="CITY" SortExpression="CITY" />
            <asp:BoundField DataField="STATE" HeaderText="STATE" SortExpression="STATE" />
            <asp:BoundField DataField="ZIP" HeaderText="ZIP" SortExpression="ZIP" />
            <asp:BoundField DataField="WELL" HeaderText="WELL" SortExpression="WELL" />
            <asp:BoundField DataField="YEARBEGAN" HeaderText="YEARBEGAN" SortExpression="YEARBEGAN" />
            <asp:BoundField DataField="OPERATOR" HeaderText="OPERATOR" SortExpression="OPERATOR" />
            <asp:BoundField DataField="SURVEY" HeaderText="SURVEY" SortExpression="SURVEY" />
            <asp:BoundField DataField="ACRES" HeaderText="ACRES" SortExpression="ACRES" />
            <asp:BoundField DataField="TYPE" HeaderText="TYPE" SortExpression="TYPE" />
            <asp:BoundField DataField="INTEREST" HeaderText="INTEREST" SortExpression="INTEREST" />
            <asp:BoundField DataField="VALUE" HeaderText="VALUE" SortExpression="VALUE" />
            <asp:BoundField DataField="YEAR" HeaderText="YEAR" SortExpression="YEAR" />
            <asp:BoundField DataField="COUNTY" HeaderText="COUNTY" SortExpression="COUNTY" />
        </Columns>

代码隐藏:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    textOwner.Text = ""
    textWell.Text = ""
    textCo.Text = ""
    textOp.Text = ""
End Sub

结束班


修改

我设法通过从gridview中删除datasourceID来解决我的两个主要问题,并在点击时执行以下操作:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Call BindData()
End Sub

Private Sub BindData()
    With GridView1
        .DataSource = SqlDataSource1
        .DataBind()
    End With
End Sub

然而,在这样做的过程中,我设法打破了gridview的表排序功能。我想我会找到一种方法来进行排序客户端。

0 个答案:

没有答案