【问题标题】:GridView isn't reflecting AJAX changesGridView 不反映 AJAX 更改
【发布时间】:2011-07-13 16:09:09
【问题描述】:

当我在 gridview 中搜索文本字符串时,或者当我从下拉列表中选择我想要订购 Gridview 的内容时,我现在正在使用 AJAX 来更新我的 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>

【问题讨论】:

  • 请出示您的表单设计?

标签: asp.net sql-server gridview parameters


【解决方案1】:

自从我的公司使用 Telerik 以来,我从未使用过 UpdatePanel,但从我在研究期间看到的示例中,我记得看到了一个触发器组件。

据我了解,如果控件本身带有 UpdatePanel,那么您不必指定触发器,因为它是假定的。

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

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

【讨论】:

  • 谢谢,我确实有一个触发器,但后来我摆脱了它。不过我会再试一次,看看会发生什么:D
【解决方案2】:

尝试以下方法:

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

【讨论】:

  • 抱歉,这不会改变任何事情!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-26
相关资源
最近更新 更多