简体   繁体   English

Gridview.Databind() 不刷新数据

[英]Gridview.Databind() not refresh the data

First of all, sorry if I make mistakes in english...首先,对不起,如果我在英语上犯了错误......

I'm making a web with c#, and I have some problems for refresh the data displayed in the GridView我正在用 c# 制作一个 web,我在刷新 GridView 中显示的数据时遇到了一些问题

I'm getting the data throw the SqlDataSource defined at the aspx view:我正在获取数据并抛出在 aspx 视图中定义的 SqlDataSource:

<asp:SqlDataSource ID="PRODUCTOS_CON_STOCK" runat="server" ConnectionString="<%$ ConnectionStrings:XXXX %>" ProviderName="<%$ ConnectionStrings:XXX.ProviderName %>" DataSourceMode="DataSet" SelectCommand=" select EAN, CODART.....  "> </asp:SqlDataSource>

When I click a button, I update some data in the database, and I want refresh de GridView with the new data, without reload the page.当我点击一个按钮时,我更新了数据库中的一些数据,我想用新数据刷新 de GridView,而不重新加载页面。 I'm making: gridView.DataBind();我正在制作: gridView.DataBind(); , but that doesn't refresh the data in the GridView. ,但这不会刷新 GridView 中的数据。

(At the database is updated) (在数据库更新时)

The GridView is inside of an UpdatePanel . GridViewUpdatePanel内部。

I was trying some things, like:我正在尝试一些事情,例如:

  • Reassing the DataSourceID and make the gridView.DataBind();重新评估DataSourceID并生成gridView.DataBind();

  • Assing the DataSourceID in null , make the gridView.DataBind();评估 null 中的DataSourceID ,使null gridView.DataBind(); , and alter reassing the DataSourceID and make the gridView.DataBind(); , 并改变重新评估DataSourceID并制作gridView.DataBind();

  • I tried too:我也试过:

    DataSourceSelectArguments argumentos = new DataSourceSelectArguments();
    
    PRODUCTOS_CON_STOCK.Select(argumentos);
    
    gridView.DataBind();
  • Set the UpdatePanel to updatemode="Always"UpdatePanel设置为updatemode="Always"

But any of all of that worked... Someone can help me?但是所有这些都有效......有人可以帮助我吗?

Thanks.谢谢。

Why not dump the datasource setting on the page of PRODUCTOS_CON_STOCK.为什么不转储 PRODUCTOS_CON_STOCK 页面上的数据源设置。

I find that when you need to filtere, load, and play?我发现什么时候需要过滤、加载和播放?

Just remove the data source from the markup.只需从标记中删除数据源。 So, in your GridView, remove the data source id setting of PRODUCTOS_CON_STOCK, and then delete the data source.所以,在你的GridView中,去掉PRODUCTOS_CON_STOCK的数据源id设置,然后删除数据源。

You have to write a BIT more code, but you now have control over the data, and MORE important, control over which parameters you need and want.你必须多写一点代码,但你现在可以控制数据,更重要的是,可以控制你需要和想要的参数。

So, say I have this markup:所以,假设我有这个标记:

A grid, and also a search/text box to filter the grid.一个网格,以及一个用于过滤网格的搜索/文本框。

<asp:Label ID="Label1" runat="server" Text="Search for Fighter jet" Font-Size="Large"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" Style="margin-left:15px" Font-Size="Large">
</asp:TextBox>

<asp:Button ID="cmdSearch" runat="server" Text="search"
    style="margin-left:15px" CssClass="btn" OnClick="cmdSearch_Click" />

<br />
<br />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" CssClass="table"  >
        <Columns>
            <asp:BoundField DataField="Fighter" HeaderText="Fighter"  />
            <asp:BoundField DataField="Engine" HeaderText="Engine"  />
            <asp:BoundField DataField="Thrust" HeaderText="Thrust"  />
            <asp:BoundField DataField="Description" HeaderText="Description" />

            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                <asp:ImageButton ID="btnImage" runat="server" Height="68px" Width="149px"
                    OnClientClick ="popimage(this);return false"
                    ImageUrl = '<%# Eval("ImagePath") %>' /> 
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>

Note close - I did build this grid using the wizards.注意关闭 - 我确实使用向导构建了这个网格。 But I THEN removed the Datasource ID for the GV, and removed the Datasource that was created in the markup.但是我然后删除了 GV 的数据源 ID,并删除了在标记中创建的数据源。

So, now my code to load is this:所以,现在我要加载的代码是这样的:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid("");
    }

    void LoadGrid(string MySearch)
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmdSQL = new SqlCommand("SELECT * from Fighters ", conn))
            {
                if (MySearch != "")
                {
                    cmdSQL.CommandText += @" WHERE Fighter LIKE @Fighter + '%'";
                    cmdSQL.Parameters.Add("Fighter", SqlDbType.NVarChar).Value = MySearch;
                }
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmdSQL.ExecuteReader());
                GridView1.DataSource = rstData;
                GridView1.DataBind();
            }
        }
    }

And I get this:我明白了:

在此处输入图像描述

And note the "optional" filter for the text box.并注意文本框的“可选”过滤器。 If you type in some text, (I used a "like" match in sql), then the code is only this:如果你输入一些文本,(我在 sql 中使用了“like”匹配),那么代码就是这样的:

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        LoadGrid(txtSearch.Text);
    }

but, the idea here is often it is much better to dump the SqlDataSoruce placed in the markup, and roll + write your own code.但是,这里的想法通常是转储放置在标记中的 SqlDataSoruce 并滚动 + 编写您自己的代码要好得多。 The problem is you can try and set the data source in code, but ALSO WITH a data source on the page - they fight over each other.问题是您可以尝试在代码中设置数据源,但也可以在页面上设置数据源——它们相互争夺。 So, try the above idea - and remove the data source in the markup.所以,试试上面的想法 - 并删除标记中的数据源。

Finally I resolved the issue.最后我解决了这个问题。

In my case, I was calling in the front, in AJAX , the C# method, that method is an WebMethod HttpPost就我而言,我在前面调用AJAX中的 C# 方法,该方法是WebMethod HttpPost

The issue was that I was saving in a Session the GridView and the SqlDataSource , and although I was executing the Select statement again, I was obtaining the old's values.问题是我在GridView Session SqlDataSource ,虽然我再次执行了 Select 语句,但我正在获取旧的值。

I was saving in a Session that information because the AJAX execute the method direct and the GridView and the SqlDataSource were null我在Session中保存了该信息,因为AJAX直接执行方法,而GridViewSqlDataSourcenull

Finally I tried to make a refresh from the GridVeiw by a button, and the info is updated correctly, so, I hided the button, and simulate the click button in AJAX :最后,我尝试通过一个按钮从GridVeiw进行刷新,并且信息已正确更新,因此,我隐藏了该按钮,并模拟了AJAX中的click按钮:

$.ajax({
        type: 'POST',
        url: pageUrl,
        data: JSON.stringify({ gridMod: gridMod }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function() {
            document.getElementById("<%= btnReload.ClientID %>").click();
        },
    });

So, the click event call to LoadGrid();所以,点击事件调用LoadGrid(); , and this one execute: ,而这个执行:

private void LoadGrid()
    {

        DataSourceSelectArguments argumentos = new DataSourceSelectArguments();

        PRODUCTOS_CON_STOCK.Select(argumentos);


        eanList.DataBind();


    }

In this way, I can refresh the data "automatically" without clicking in any button这样我就可以不用点击任何按钮就可以“自动”刷新数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM