2011-04-29 44 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

相关问题