GridView没有反映AJAX的变化

时间:2011-07-13 16:09:10

标签: asp.net sql-server gridview parameters

我现在正在使用AJAX来更新我的GridView,当我在gridview中搜索一串文本时,或者当我从下拉列表中选择我想要订购Gridview的时候。这在以前工作,但我的代码非常混乱。所以我已经清理了一点,添加了一些参数等。不幸的是,现在,当下拉列表的selectedindex被更改或有人试图搜索字段时,没有任何反应 - 页面只是刷新。我也得到一个例外,说“由ORDER BY编号1标识的SELECT项包含一个变量作为标识列位置的表达式的一部分。只有在通过引用列名的表达式进行排序时才允许变量”。

如果您需要查看更多代码,请告诉我们!

public vieworders()
    {
        this.PreInit += new EventHandler(vieworders_PreInit);
    }

    void vieworders_PreInit(object sender, EventArgs e)
    {
            orderByString = orderByList.SelectedItem.Value;
            fieldString = searchTextBox.Text;
            updateDatabase(fieldString, orderByString);

    }

    protected void updateDatabase(string _searchString, string _orderByString)
    {
        string updateCommand = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE @searchString OR lName LIKE @searchString OR zip LIKE @searchString OR email LIKE @searchString OR cwaSource LIKE @searchString OR length LIKE @searchString OR price LIKE @searchString ORDER BY @orderByString";

        Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Cabot3");
        ConnectionStringSettings connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];

        // Create an SqlConnection to the database.
        using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
        using (SqlCommand _fillDatabase = new SqlCommand(updateCommand, connection))
        {

            connection.Open();
            _fillDatabase.Parameters.Add("@searchString", SqlDbType.VarChar, 50).Value = _searchString;
            _fillDatabase.Parameters.Add("@orderByString", SqlDbType.VarChar, 50).Value = _orderByString;
            _fillDatabase.ExecuteNonQuery();

            dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);

            // create the DataSet
            dataSet = new DataSet();
            // fill the DataSet using our DataAdapter               
            dataAdapter.Fill(dataSet, "SecureOrders");

            DataView source = new DataView(dataSet.Tables[0]);
            DefaultGrid.DataSource = source;
            DefaultGrid.DataBind();
            connection.Close();
        }
    }

表格

<form id="form1" runat="server">
<asp:ScriptManager ID = "ScriptManager" runat="server" />
    <div>
        <asp:Label runat="server" id = "orderByLabel" Text = "Order By: " />


        <asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
            <asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
            <asp:ListItem Value="lName">Last Name</asp:ListItem>
            <asp:ListItem Value="state">State</asp:ListItem>
            <asp:ListItem Value="zip">Zip Code</asp:ListItem>
            <asp:ListItem Value="cwaSource">Source</asp:ListItem>
            <asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
        </asp:DropDownList>
    </div>
    <div>
        <asp:Label runat="server" ID="searchLabel" Text="Search For: " />
        <asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
        <asp:Button ID="searchButton" runat="server" Text="Search" />
    </div>
<div>
<asp:UpdatePanel ID = "up" runat="server">



    <ContentTemplate>
    <div style= "overflow:auto; height:50%; width:100%">
    <asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "IdentityColumn"
    onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
    autogenerateselectbutton = "true">
    <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>
    </Columns>
    </asp:GridView>
    </div>
    </div>
    <div style= "overflow:auto; height:50%; width:100%" />
    <table border="1">
        <tr>    
        <td>Name: </td>
        <td><%=name %></td>
        </tr>
        <tr>
        <td>Zip: </td>
        <td><%=zip %></td>
        </tr>
        <tr>
        <td>Email: </td>
        <td><%=email %></td>
        </tr>
        <tr>
        <td>Length: </td>
        <td><%=length %></td>
        </tr>
        <tr>
        <td>Price: </td>
        <td><%=price %></td>
        </tr>
        <tr>
        <td>Source: </td>
        <td><%=source %></td>
        </tr>
    </table>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>



</div>
</form>

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

  1. 在HTML中,在scriptmanager行之后直接移动更新面板。
  2. 使用!IsPostPack子句将vieworders_PreInit中的代码行移动到vieworders_PageLoad。

答案 1 :(得分:0)

我从未使用过UpdatePanel,因为我的公司使用Telerik,但从我在研究中看到的例子中我记得看到了一个触发器组件。

根据我的理解,如果控件是UpdatePanel本身,那么你不必指定触发器,因为它是假定的。

对于您的方案,触发器(下拉列表)位于UpdatePanel之外。您可能需要在aspx中包含它:

<asp:UpdatePanel ID = "up" runat="server">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="orderByList" >
</Triggers>
<ContentTemplate>
    ...