2011-04-29 48 views
0

如何訪問網格視圖中的控件,以便我可以更改它的前景色?在下面的代碼中,FindControl()返回null。如何在.NET Grid View中更改控件的前景色?

protected void mileageRowBound(object sender, GridViewRowEventArgs e) 
{ 
    (e.Row.Cells[1].FindControl("ddlStateCode") as DropDownList).ForeColor = System.Drawing.Color.LightBlue; 
} 

我也試過e.Row.FindControl(「ddlStateCode」)和其他一些變化。我很難過。

有人問了標記:

<asp:GridView runat="server" OnPreRender="grvStateWiseMileage_OnPreRender" OnRowCommand="grvStateWiseMileage_OnRowCommand" 
              CssClass="GridViewStyle" BorderWidth="1" Width="100%" ID="grvStateWiseMileage" 
               AutoGenerateColumns="false" RowStyle-Height="25px" ShowHeader="false" OnRowDataBound="mileageRowBound"> 
              <Columns> 
                <asp:TemplateField> 
                 <ItemStyle CssClass="GridViewRowStyle" Width="5%" /> 
                <ItemTemplate> 
                 <asp:Label ID="lblLine" runat="server" onKeyUp="javascript:ValidateDecimal(this)" 
                  Text='<%# Eval("Line#") %>'></asp:Label> 
                 <asp:ImageButton runat="server" ID="imgbtnMileageDelete" ImageUrl="Images/delete.png" /> 
                 <asp:HiddenField runat="server" ID="hdnFuelMileageCode" Value='<% #Eval("FuelMileageCode") %>' /> 
                 <asp:HiddenField runat="server" ID="hdnMileageCode" Value='<% # Eval("MileageCode") %>' /> 
                 <asp:HiddenField runat="server" ID="hdnMileagePosted" Value='<% # Eval("MileagePosted") %>' /> 
                </ItemTemplate> 
               </asp:TemplateField> 
                <asp:TemplateField> 
                <ItemStyle CssClass="GridViewRowStyle" Width="10%" /> 
                <ItemTemplate> 
                 <asp:DropDownList runat="server" OnLoad="ddlStateCode_OnLoad" ID="ddlStateCode" Style="border: none; 
                  border-width: 0px; width: 100px"> 
                 </asp:DropDownList> 
                 <asp:HiddenField runat="server" ID="hdnStateCode" Value='<% # Eval("State")%>' /> 
                </ItemTemplate> 
               </asp:TemplateField> 
                <asp:TemplateField> 
                <ItemStyle CssClass="GridViewRowStyle" Width="10%" /> 
                <ItemTemplate> 
                 <BDP:BasicDatePicker ID="bdpMileageDate" runat="server"> 
                 </BDP:BasicDatePicker> 
                 <asp:HiddenField runat="server" ID="hdnMileageDate" Value='<% # Eval("Date")%>' /> 
                </ItemTemplate> 
               </asp:TemplateField> 
               <asp:TemplateField> 
                <ItemStyle CssClass="GridViewRowStyle" Width="10%" /> 
                <ItemTemplate> 
                 <asp:TextBox ID="txtMiles" Style="border: none; border-width: 0px; text-align: right" 
                  Width="90%" runat="server" MaxLength="12" onKeyUp="javascript:ValidateDecimal(this)" 
                  Text='<%# Eval("Miles") %>' onblur="postBackHiddenField('hdnStateWiseMileage')" 
                  onkeydown="return postBackHiddenFieldForEnterMiles(event)"></asp:TextBox> 
                 <cc1:FilteredTextBoxExtender runat="server" FilterMode="ValidChars" FilterType="Custom, Numbers" 
                  ValidChars="." TargetControlID="txtMiles"> 
                 </cc1:FilteredTextBoxExtender> 
                </ItemTemplate> 
               </asp:TemplateField> 
               <asp:TemplateField> 
                <ItemStyle CssClass="GridViewRowStyle" Width="10%" /> 
                <ItemTemplate> 
                 <asp:DropDownList runat="server" ID="ddlLoadStatus" Style="border: none; border-width: 0px; 
                  width: 100px"> 
                  <asp:ListItem Selected="True" Text="Loaded" Value="1"></asp:ListItem> 
                  <asp:ListItem Text="Empty" Value="2"></asp:ListItem> 
                  <asp:ListItem Text="Toll" Value="3"></asp:ListItem> 
                 </asp:DropDownList> 
                 <asp:HiddenField runat="server" ID="hdnMileageType" Value='<% # Eval("LoadStatus")%>' /> 
                </ItemTemplate> 
               </asp:TemplateField> 
              </Columns> 
              <FooterStyle BorderStyle="None" BackColor="White" /> 
             </asp:GridView> 
+0

你可以發佈GridView的標記嗎? – 2011-04-29 18:34:40

+0

標記編輯! – Gagege 2011-04-29 18:45:24

回答

1

嘗試來包裝你的代碼行中

if(e.Row.RowType == DataControlRowType.DataRow) 
{ 
     (e.Row.FindControl("ddlStateCode") as DropDownList).ForeColor = System.Drawing.Color.LightBlue; 
} 

因此,這將跳過標題(和頁腳和其他一些)。

0

我想更好的回答這個問題是使用CSS來改變控制文本的顏色。

嘗試將以下行添加到ASPX頁面。

<style type="text/css"> 
#ddlStateCode 
{ 
    color: #FF0000; /* Change to the hexacode you want */ 
} 
</style> 

希望它有幫助。

+0

對不起,我不是很清楚。我試圖做這個programmaticaly,因爲我需要根據行中的某些值更改顏色。 – Gagege 2011-04-29 18:42:31

+0

嗯......好吧,你可以使用javascript來做到這一點......如果該值在頁面中。您可以查詢(jQuery)並通過查詢的值更改顏色。 – marcoaoteixeira 2011-04-29 18:50:40

相關問題