簡體   English   中英

GridView中的OnRowDataBound事件根本不觸發

[英]OnRowDataBound event in GridView not firing at all

我通常不使用OnRowDataBound,但是在這種情況下我需要這樣做,因為我需要根據特定字段更改行的背景色。

這是我的ASPX:

<div id="divGrid" style='width:920px; height:430px; overflow:auto'>
    <asp:DataGrid ID="DataGrid_AuditSearch" runat="server"
        AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" 
        GridLines="None" AutoGenerateColumns="False" 
        OnRowDataBound="DataGrid_AuditSearch_RowDataBound" 
        OnCancelCommand="DataGrid_AuditSearch_CancelCommand" 
        OnUpdateCommand="DataGrid_AuditSearch_UpdateCommand" 
        OnEditCommand="DataGrid_AuditSearch_EditCommand">
        <AlternatingItemStyle Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <EditItemStyle BackColor="#999999" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <PagerStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Width="920px" Font-Underline="False" />
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <Columns>
            <asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" 
                EditText="Select" UpdateText="Update"></asp:EditCommandColumn>

            <asp:BoundColumn DataField="AUDIT_ID" HeaderText="Audit ID"/>  
            <asp:BoundColumn DataField="PLAN_ID" HeaderText="Plan ID"/>  
            <asp:BoundColumn DataField="PLAN_DESC" HeaderText="Plan Desc"/>  
            <asp:BoundColumn DataField="DOC_TYPE" HeaderText="Doc Type"/>
            <asp:BoundColumn DataField="PRODUCT" HeaderText="Product"/>
            <asp:BoundColumn DataField="PLAN_TYPE" HeaderText="Plan Type"/>  
            <asp:BoundColumn DataField="Auditor_ID" HeaderText="Auditor ID"/> 
        </Columns>

    </asp:DataGrid>
    <asp:Label ID="lblEmpty" runat="server" Visible="false" Style="font-weight:bold; font-size:large;"></asp:Label>
</div> 

這是我的C#代碼背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Sometimes you need the parameter, sometimes you don't.  This works for both cases.
        txtAuditSearch.Text = (Request["AuditID"] ?? String.Empty).ToString();

        Show_Data(0);
    }
    else
    {

    }
}

public void Show_Data(int AuditID)
{
    OracleConnection conn = GetConnection();
    try
    {

        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnCST"].ToString();

            OracleCommand cmd3 = new OracleCommand();
            cmd3.Connection = conn;


            string sqlquery2;

            //sqlquery2 = "SELECT * FROM F_Audit_Plan Where Audit_ID = " + AuditID + "";

            sqlquery2 = "SELECT FAP.AUDIT_ID, FAP.PLAN_ID, FAP.PLAN_DESC, DTY.DOC_TY AS DOC_TYPE, DP.PRODUCT, ";
            sqlquery2 = sqlquery2 + "DPT.PLAN_TYPE, FA.Auditor_Lan_ID AS Auditor_ID, FAP.Plan_Review_Ind ";
            sqlquery2 = sqlquery2 + "FROM F_Audit_Plan FAP ";
            sqlquery2 = sqlquery2 + "LEFT JOIN D_DOC_TY DTY ";
            sqlquery2 = sqlquery2 + "ON FAP.DOC_TY_ID = DTY.DOC_TY_ID ";
            sqlquery2 = sqlquery2 + "LEFT JOIN D_PRODUCT DP ";
            sqlquery2 = sqlquery2 + "ON FAP.PRODUCT_ID = DP.PRODUCT_ID ";
            sqlquery2 = sqlquery2 + "LEFT JOIN D_PLAN_TYPE DPT ";
            sqlquery2 = sqlquery2 + "ON FAP.PLAN_TY_ID = DPT.PLAN_TY_ID ";
            sqlquery2 = sqlquery2 + "LEFT JOIN F_Audit FA ";
            sqlquery2 = sqlquery2 + "ON FA.Audit_ID = FAP.Audit_ID ";
            sqlquery2 = sqlquery2 + "Where FAP.Audit_ID = " + AuditID + " ";
            sqlquery2 = sqlquery2 + "ORDER BY FAP.PLAN_DESC ASC ";

            cmd3.CommandText = sqlquery2;

            var SearchAdapter = new OracleDataAdapter(cmd3);
            var ds = new DataSet();
            SearchAdapter.Fill(ds);

            // Perform the binding.
            DataGrid_AuditSearch.DataSource = ds;
            DataGrid_AuditSearch.DataBind();

            if (DataGrid_AuditSearch.Items.Count < 1)
            {
                    lblEmpty.Visible = true;
                    lblEmpty.Text = "There is no data to display";
            }
            else
            {
                    lblEmpty.Visible = false;
            }

            conn.Close();
            //DataGrid_AuditSearch.Columns[3].Visible = false;
            //DataGrid_AuditSearch.Columns[1].Visible = false;
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
        conn.Close();
    }

}

protected void DataGrid_AuditSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string Status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Plan_Review_Ind"));

        if (Status == "Y")
        {
                e.Row.Attributes["style"] = "background-color: #28b779";
        }
        else
        {
                e.Row.Attributes["style"] = "background-color: #da5554";
        }
    }
}

我在DataGrid_AuditSearch_RowDataBound函數的第一行上放置了一個斷點,它甚至從未命中過。 知道我在做什么錯嗎?

AutoGenerateColumns="False" 出這個家伙 ,好像設置AutoGenerateColumns="False"打破了這一點。

您使用的是DataGrid ,而不是GridView DataGrid沒有OnRowDataBound事件。 請改用OnItemDataBound事件。

Microsoft上了解有關它的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM