【问题标题】:Gridview color not changing when a button is clicked单击按钮时Gridview颜色不会改变
【发布时间】:2018-08-10 10:27:01
【问题描述】:

我目前正在开发一个网页,该网页通过单击按钮从 SQLdatasource 检索数据。我还包括一个突出特定行的功能。 它适用于页面加载,但不适用于单击按钮。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Timers;
using System.Drawing;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        filterButton.Click += new EventHandler(this.filterButton_Click);
        prodTab.Rows[1].Cells[7].BackColor = Color.Red;

    }


    protected void filterButton_Click(object sender, EventArgs e)
    {

        string newQuery = "SELECT top 10 A.[LogOn],A.[Tcode],B.[Name],A.[Item],A.[ToLocation] AS [BIN],A.[Quantity],A.[ToStorageLocation] AS [Production Line],A." +
            "[Flag],A.[Remarks] FROM [IBusiness].[dbo].[SY_TransferOrderLog] A INNER JOIN" + " [IBusiness].[dbo].[ST_SY_User] B ON A.[UserId] = B.[UserId] WHERE " +
            "(Tcode ='T32' OR Tcode ='B32') And [LogOn] BETWEEN GETDATE()-1 AND GETDATE() and B.[User] = '" + TextBox1.Text + "' Order By LogOn desc";

        string command = SqlDataSource.SelectCommand; // added just for debug purpose
        SqlDataSource.SelectCommand = newQuery;

        prodTab.Rows[1].Cells[7].BackColor = Color.Red;
    }

}
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    确保在将数据(重新)绑定到 GridView 之后更改单元格颜色。

    这适用于为单元格着色

    prodTab.DataSource = source;
    prodTab.DataBind();
    
    prodTab.Rows[1].Cells[1].BackColor = Color.Red;
    

    这不是因为它被默认值覆盖了。

    prodTab.Rows[1].Cells[1].BackColor = Color.Red;
    
    prodTab.DataSource = source;
    prodTab.DataBind();
    

    【讨论】:

    • 您好!非常感谢你。如果可以的话,我会投票
    • 您可以接受答案为正确答案。然后其他用户也将知道该解决方案有效。
    【解决方案2】:

    将 runat="server" 属性添加到您的 aspx 页面中的按钮

    【讨论】:

    • runat="server" 默认设置在 ASPX 页面的 HTML CODE 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多