【问题标题】:Search/Sort to display specific fields in GridView搜索/排序以在 GridView 中显示特定字段
【发布时间】:2011-07-19 18:32:32
【问题描述】:

在我将数据绑定到 .cs 文件中的 GridView 之前。我进行了搜索/排序工作(通过在文本框中输入搜索数据库,通过从下拉列表中选择一个选项进行排序)。但是,现在我将我的数据绑定到 .aspx 文件中,当然我的排序/搜索不再起作用了。如何更改排序/搜索算法以便绑定正确的数据???

(searchFill 是调用搜索/排序的函数)

.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Cabot3");
        connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["SecureODBConnectionString"];
        searchFill();
        GridViewRow row = DefaultGrid.SelectedRow;
        int rowIndex = DefaultGrid.SelectedIndex;
        HiddenGrid.SelectedIndex = rowIndex;
        GridViewRow row2 = HiddenGrid.SelectedRow;
        //int id = Convert.ToInt32(row.Cells[25].Text);
        fName = row2.Cells[0].Text;
        lName = row2.Cells[1].Text;
        addr = row2.Cells[2].Text;
        addr2 = row2.Cells[3].Text;
        city = row2.Cells[4].Text;
        state = row2.Cells[5].Text;
        zip = row2.Cells[6].Text;
        country = row2.Cells[7].Text;
        email = row2.Cells[8].Text;
        phone = row2.Cells[9].Text;
        ccType = row2.Cells[10].Text;
        ccNum = row2.Cells[11].Text;
        ccExp = row2.Cells[12].Text;
        length = row2.Cells[13].Text;
        delivery = row2.Cells[14].Text;
        price = row2.Cells[15].Text;
        source = row2.Cells[16].Text;
        joined = row2.Cells[17].Text;
        url = row2.Cells[18].Text;
        orderResults = row2.Cells[19].Text;
        pubName = row2.Cells[20].Text;
        sourceCode = row2.Cells[21].Text;
    }

    protected void searchFill()
    {
        orderByString = orderByList.SelectedItem.Value;
        fieldString = searchTextBox.Text;
        string sqlStatement = "SELECT * FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR addr LIKE'%" + fieldString + "%' OR addr2 LIKE'%" + fieldString + "%' OR city LIKE'%" + fieldString + "%' OR state LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR country LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR phone LIKE'%" + fieldString + "%' OR ccType LIKE'%" + fieldString + "%' OR ccNum LIKE'%" + fieldString + "%' OR ccExp LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR cwaJoined LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR delivery LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' OR url LIKE'%" + fieldString + "%' OR orderResults LIKE'%" + fieldString + "%' OR pubName LIKE'%" + fieldString + "%' OR sourceCode LIKE'%" + fieldString+ "%' ORDER BY " + orderByString;
        using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
        using(SqlCommand searchSort = new SqlCommand(sqlStatement, connection))
        {
            searchSort.Parameters.AddWithValue("@fieldString", fieldString);
            searchSort.Parameters.AddWithValue("@orderByString", orderByString);

            connection.Open();
                searchSort.ExecuteNonQuery();             
            connection.Close();
        }
    }

.aspx

 <asp:GridView ID="DefaultGrid" 
        runat = "server"
        DataKeyNames = "IdentityColumn"
        onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
        autogenerateselectbutton = "True"
        enableviewstate = "False"
        selectedindex="0" DataSourceID="OrderSource" EnableModelValidation="True" 
        AutoGenerateColumns="False">
    <SelectedRowStyle BackColor="Azure"
        forecolor="Black"
        font-bold="true" />
    <Columns>
        <asp:TemplateField HeaderText = "Processed">
            <ItemTemplate>
                <asp:CheckBox
                ID="CheckBoxProcess"
                AutoPostBack = "true"
                Checked = '<%#Eval("processed") %>'
                OnCheckedChanged = "CheckBoxProcess_CheckedChanged"
                runat="server"
                Enabled = "true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="fName" HeaderText="First Name" SortExpression="fName" />
        <asp:BoundField DataField="lName" HeaderText="Last Name" SortExpression="lName" />
        <asp:BoundField DataField="addr" HeaderText="Address" SortExpression="addr" />
        <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
        <asp:BoundField DataField="phone" HeaderText="Phone" SortExpression="phone" />
        <asp:BoundField DataField="ccType" HeaderText="Credit Card Type" 
            SortExpression="ccType" />
        <asp:BoundField DataField="length" HeaderText="Length" 
            SortExpression="length" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="OrderSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SecureODBConnectionString %>" 
        SelectCommand="SELECT * FROM [SecureOrders]"></asp:SqlDataSource>
    </div>

【问题讨论】:

    标签: c# asp.net sql-server search gridview


    【解决方案1】:

    也许这...

    1. 编写您的存储过程(正如@coder 所说...您这样做的方式并不安全)

      创建过程 dbo.myPROC (@parm1 int = null,@parm2 int = null,...) 作为 开始

      SELECT
         field1,
         field2,
         field3,
         ...
      FROM
         Table
      WHERE
         (field1 = @parm1 or @parm1 is null)
         OR
         (field2 = @parm2 or @parm2 is null)
      
      END
      
    2. 配置您的数据源(使用向导)。

    一旦您将存储过程标识到数据源,它将要求您提供存储过程中标识的每个参数的源值。只需填写默认值,剩下的就不用管了。基本上你想要结束的是这样的(我没有得到这个语法正确 - 但一旦你开始,你的智能感知就会为你得到它):

    ...
    <SELECT PARAMETERS>
       <PARAMETER name="parm1" type="integer">
       ...
    </SELECT PARAMETERS>
    
    1. 修改您的 searchFill() 过程以使用您在 onload 中创建的变量

    me.datasource1.parameters.clear() me.datassource.parameters("parm1").defaultvalue = fname; me.datassource.parameters("parm2").defaultvalue = lname; ...

    1. 重新绑定您的网格视图

      me.gridview.databind

    这并不优雅,但它似乎符合您目前采用的方法。希望对您有所帮助。

    【讨论】:

    • 哦,顺便说一下,OrderBy 子句由于某种原因不会带参数 - 这就是它使用变量编写的原因:/
    • 是的——你不能对参数进行排序。您可以在存储过程中编写动态 sql(类似于您最初构造 sql-string 的方式),但这很痛苦。您还可以在数据绑定后对网格进行排序,而不必担心 sql 中的 orderby。
    • 我没有意识到 GridView 有一个排序功能 - 这让我的生活变得如此轻松!
    【解决方案2】:

    您必须将参数从后面的代码传递给 sql 数据源。看看this。

    另外,您要小心 sql 注入攻击。永远不要将文本框值直接传递给 sql 语句。我希望你的实际代码中有一个存储过程。

    【讨论】:

    • 我的 OrderBy 子句不带参数 - 这就是我使用文本框值的原因。我的代码中的其他所有内容都使用参数...
    • 您可能希望使用这些技术之一来实现排序.. stackoverflow.com/questions/213148/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2016-08-28
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2021-08-23
    • 2014-11-07
    相关资源
    最近更新 更多