如何訪問網格視圖中的控件,以便我可以更改它的前景色?在下面的代碼中,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>
你可以發佈GridView的標記嗎? – 2011-04-29 18:34:40
標記編輯! – Gagege 2011-04-29 18:45:24