如何在.NET网格视图中更改控件的前景色?

时间:2011-04-29 18:29:59

标签: c# .net

如何在网格视图中访问控件,以便我可以更改它的前景色?在下面的代码中,FindControl()返回null。

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>

2 个答案:

答案 0 :(得分:1)

尝试将您的代码行包装在

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

因此它将跳过标题(以及页脚和其他一些内容)。

答案 1 :(得分:0)

我想这个问题的更好答案是使用CSS来改变控制文本的颜色。

尝试将以下行添加到ASPX页面。

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

希望它有所帮助。