同时更新文字和gridView

时间:2013-05-31 15:21:59

标签: c# .net sql gridview literals

当网格视图中的“更新”按钮被点击时,有没有办法进行文字更新? 文字中填充了gridview的一列中所有值的总和,并且当gridview更新时,我还希望文字立即更新。 gridView正在从sql server填充。 我的文字代码背后是:

   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();
        }
    }

它的.net是:

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

我尝试过使用标签代替文字并给出gridview的编辑按钮和标签相同的ValidationGroup但是没有用。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您是否尝试将文字编辑代码放在GridView的RowCommand事件处理程序中?

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

答案 1 :(得分:0)

我最终将代码放在gv_RowUpdated方法中:

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();
    }

}