2017-01-24 38 views
0

通常、OnRowDataBoundでは動作しませんが、特定のフィールドに基づいて行のバックカラーを変更する必要があるため、この場合は必要です。ここでGridViewの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機能の最初の行にブレークポイントを入れてもそれを打つことはありません。私が間違っていることは何か考えていますか?

+0

また、http://security.stackexchange.com/questions/68701/how-does-stored-procedure-prevents-sql-injectionをハードコードする代わりに、ストアドプロシージャを使用することもできます –

答えて

0

Check this guy outのように、設定AutoGenerateColumns="False"のように見えます。

0

DataGridを使用していますが、GridViewではありません。 DataGridにはOnRowDataBoundイベントがありません。代わりにOnItemDataBoundイベントを使用してください。

Microsoftで詳しく読むことができます。

関連する問題