Question

Is there any way to have a literal update when the "update" button in a gridview has been hit? The literal is populated with the sum of all the values in one column of the gridview and when the gridview is updated I also want the literal to immediately update. The gridView is being populated from a sql server. My literal's code behind is:

   protected void litBalance_Init(object sender, EventArgs e)
    {

        string queryString =
            "SELECT SUM(AuthorizationAmount) AS Balance FROM dbo.CPSTransaction WHERE (ApplicationIDPrimary = '" + Request.QueryString["WSUID"] + "')";
        using (SqlConnection connection =
                   new SqlConnection(ConfigurationManager.ConnectionStrings["OrientationConnectionString"].ConnectionString))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                reader.Read();
                litBalance.Text = reader["Balance"].ToString();
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

and it's .net is:

<asp:Literal ID="litBalance" runat="server" oninit="litBalance_Init"></asp:Literal>

I have tried using a label instead of a literal and giving the gridview's edit button and the label the same ValidationGroup but that didn't work.

Thanks in advance for the help.

Was it helpful?

Solution

Have you tried putting your literal edit code inside the RowCommand event handler for the GridView?

http://msdn.microsoft.com/en-US/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.80).aspx

OTHER TIPS

I ended up putting the code in the gv_RowUpdated method:

protected void gvPaymentDetails_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{

    string balanceQueryString =
        "SELECT SUM(AuthorizationAmount) AS Balance FROM dbo.CPSTransaction WHERE (ApplicationIDPrimary = '" + Request.QueryString["WSUID"] + "')";
    using (SqlConnection balanceConnection =
               new SqlConnection(ConfigurationManager.ConnectionStrings["OrientationConnectionString"].ConnectionString))
    {
        SqlCommand balanceCommand =
            new SqlCommand(balanceQueryString, balanceConnection);
        balanceConnection.Open();

        SqlDataReader balanceReader = balanceCommand.ExecuteReader();

        if (balanceReader.HasRows)
        {
            balanceReader.Read();
            litBalance.Text = balanceReader["Balance"].ToString();
        }

        // Call Close when done reading.
        balanceReader.Close();
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top