我有一个gridview的用户控件,但由于某种原因我不能在后端代码中使用asp控件。 在其他用户控件中我可以正常使用它们,但在这个特定的用户控件中,当我尝试使用gridview中使用的控件时,它会给出错误。 这是我的gridview的代码,如果您发现异常,请告诉我:
<asp:GridView ID="gvGDG" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1" Width="100%" CssClass="mGrid" GridLines="None" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
AllowPaging="True" >
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ItemStyle-HorizontalAlign="Center" CausesValidation="False">
</asp:CommandField>
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True"
ItemStyle-HorizontalAlign="Center" >
</asp:BoundField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" DataTextField="name" DataValueField="ID" onselectedindexchanged="ddlcountry_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" />
</Triggers>
</asp:UpdatePanel>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="State Province" SortExpression="State_Province">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("State_Province") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSource3" SelectedValue='<%# Bind("State_Province") %>'
DataTextField="StateName" DataValueField="StateName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [StateName] FROM [States]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="GDG Type" SortExpression="State_Province">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("GDG_Type") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlGdg" runat="server" DataSourceID="SqlDataSource4" SelectedValue='<%# Bind("GDG_Type") %>'
DataTextField="GDG" DataValueField="GDG">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [GDG] FROM [GDG]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Energy Type UOM" SortExpression="Energy_Type_UOM">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Energy_Type_UOM") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUomEnergy" runat="server" DataSourceID="SqlDataSource5" SelectedValue='<%# Bind("Energy_Type_UOM") %>'
DataTextField="UOM" DataValueField="UOM">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT UOM FROM UOM WHERE (Type = 'E')"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="GDG UOM" SortExpression="GDG_UOM">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("GDG_UOM") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUom" runat="server" DataSourceID="SqlDataSource6" SelectedValue='<%# Bind("GDG_UOM") %>'
DataTextField="UOM" DataValueField="UOM">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT UOM FROM UOM WHERE (Type = 'O')"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="GDG_Coeficient_Value"
HeaderText="GDG Coeficient Value" SortExpression="GDG Coeficient Value"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
提前致谢,Laziale
答案 0 :(得分:0)
如果我那么。你有一个用户控件。说wucMyControl.ascx,你有一个gridview控件gvGDG。你无法从wucMyControl.ascx.cs访问它。如果出现问题,请使用以下解决方案:
在用户控件设计器文件中,检查Gridview控件的访问级别。可能会被改为私人,意外,而不是保护。如果是私有的,请将其保护或公开,并检查您是否能够立即访问它。
<强>更新强> 仅供参考,您无法直接访问子控件。为此,您需要访问控件DataBound事件。
在你的情况下:
protected void GridView_RowDataBound(sender, e)
{
if(e.Row.RowType == DataControlRowtype.DataRow)
{
var ddl = (DropdownList)e.Row.FindControl("ddlCountry");
// do whatever you want with ddl. Similarly you can find all html and server control inside a gridview.
}
}
这会对你有帮助......
将事件添加到gridview子控件
在GridView1_RowDataBound
方法中,通过调用ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
然后,声明void ddl_SelectedIndexChanged(object sender, EventArgs e)
方法来处理您的逻辑。 sender参数将是对所选下拉列表的引用。同时将drodownlist的AutoPostback属性设置为true。
答案 1 :(得分:0)
我想也许你在ASP.net设计师页面顶部的代码有些错误:
请查看CodeFile =“??????”和继承=“????????”