c#
  • gridview
  • code-behind
  • 2017-01-24 3 views 0 likes 
    0

    Обычно я не работаю с OnRowDataBound, но мне нужно в этом случае, потому что мне нужно изменить цвет фона строк на основе определенного поля.Событие OnRowDataBound в GridView вообще не срабатывает

    Вот мой 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

    Кроме того, вы можете захотеть использовать хранимые процедуры вместо hardcoding. 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.

    Смежные вопросы