skip to Main Content

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

I’m getting the data throw the SqlDataSource defined at the aspx view:

<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.
I’m making: gridView.DataBind();, but that doesn’t refresh the data in the GridView.

(At the database is updated)

The GridView is inside of an UpdatePanel.

I was trying some things, like:

  • Reassing the DataSourceID and make the gridView.DataBind();

  • Assing the DataSourceID in null, make the gridView.DataBind();, and alter reassing the DataSourceID and make the gridView.DataBind();

  • I tried too:

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

But any of all of that worked…
Someone can help me?

Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    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

    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.

    I was saving in a Session that information because the AJAX execute the method direct and the GridView and the SqlDataSource were null

    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:

    $.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();, 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


  2. Why not dump the datasource setting on the page of 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.

    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.

    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:

    enter image description here

    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:

        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. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search