2011-12-08 95 views
0

我有用gridview的用戶控件,但由於某種原因,我不能使用後端代碼內的asp控件。 在其他用戶控件中,我可以正常使用它們,但是在這個特定的用戶控件中,當我嘗試使用gridview中使用的控件時,它給了我錯誤。 這裏是我的gridview的代碼,請讓我知道,如果你發現一些不尋常的:無法從gridview訪問asp控件

<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

通過使用,您是指在瀏覽器中?你會得到什麼錯誤? – Remy

回答

0

如果我M右即可。你有一個用戶控制。說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)方法來處理您的邏輯。發件人參數將是對所選下拉列表的引用。同時將drodownlist的AutoPostback屬性設置爲true。

+0

thx爲您的答覆,但它仍然是一樣的。它受到保護,我嘗試公開但同樣的事情正在發生。只是fyi,我可以訪問後端代碼中的gridview對象,但我無法訪問諸如dropdownlist ddlCountry之類的控件。 – Laziale

+0

查看我的更新回答。 –

+0

這就是它在設計器文件中的樣子 protected global :: System.Web.UI.WebControls.GridView gvGDG; – Laziale

0

我想也許你有一些毛病在頁面ASP.net設計師的最上面一行代碼:

上的CodeFile =請檢查「??????」和Inherits =「????????」