2012-01-20 46 views
1

你好同事,在這裏遇到了一些問題。我在GridView中添加一個用戶控件。 現在我的問題是如何綁定它導致在用戶控件內部是一個網格視圖需要CourseCatID,以便它可以綁定數據。順便說一句,我不能使用嵌套的griview因爲我需要爲另一個目的提供嵌套的usercontrol。任何教程/幫助將非常感激。asp.net用戶控件綁定gridview

<asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False" Width="1100px" 
       DataKeyNames="CourseCatID" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" 
       CellPadding="4" ForeColor="#333333" GridLines="None"> 
       <Columns> 
        <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" /> 
        <asp:BoundField HeaderText="CourseCatID" Visible = "false" DataField="CourseCatID" /> 
        <asp:TemplateField HeaderText="Course Category"> 
         <ItemTemplate> 
          <asp:Label ID="lblCourseCatID" runat="server" Visible="false" Text='<%# Eval("CourseCatID")%>'></asp:Label> 
           <a href="javascript:toggleDiv('mydiv<%# Eval("CourseCatID")%>')"> 
           <asp:TextBox ID="txtCourseCatName" runat="server" Text='<%# Eval("CourseCatName") %>' Font-Size="XX-Small" 
          Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox> 
           <asp:Image ID="img" onclick="javascript:Toggle(this);" runat="server" ImageUrl="~/Images/minus.gif" 
            ToolTip="Collapse" Width="7px" Height="7px" ImageAlign="AbsMiddle" /></a> 
          <asp:Label ID="lbllastname" Height="15px" runat="server" Text='<%# Eval("CourseCatName")%>'> </asp:Label> 
          <div id="mydiv<%# Eval("CourseCatID")%>"> 


           <br /> 
           &#160&#160&#160&#160&#160&#160<%--OnClick="ImageAdd_click" --%> 
           <asp:ImageButton ID="ImageAdd" Height="17px" ImageUrl="Images/addCourse.png" runat="server" 
            CommandName="cmdAdd" CommandArgument='<%# Eval("CourseCatID") %>' /> 
            <br /> 
            <br /> 

            &#160&#160&#160&#160&#160&#160 
           <asp:Panel ID="pnlCourse" runat="server"></asp:Panel> 
           <b><cuc1:CourseUserControl ID="CourseUserControl1" runat="server" /></b> 
           <br /> 
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
           <br /> 
           <br /> 
            <br /> 

           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
          </div> 
         </ItemTemplate> 
        </asp:TemplateField> 

謝謝您的時間

回答

3

你有幾個選項。

最簡單的一個是揭露你這樣做對用戶的控制,可以讓公共財產:只要該財產被指定給

<cuc1:CourseUserControl ID="CourseUserControl1" runat="server" CourseCategoryID='<%# (int)Eval("CourseCatID") %>' /> 

然後在數據綁定用戶控件。請注意,我假設該類別是一個Int32。例如(請注意,CourseCategoryID在私有字段存儲的值,而不是在ViewState中):

private int _courseCategoryID; 
public int CourseCategoryID 
{ 
    get { return _courseCategoryID; } 
    set 
    { 
     _courseCategoryID = value; 

     // TODO: code that initializes the GridView in user control. 

     this.DataBind(); 
    } 
} 

你的另一個選擇是公開相同的特性和處理RowDataBound事件,並做到這一點:

if (e.Row.RowType == DataControlType.DataRow) 
{ 
    CourseUserControl courseDetails; 
    courseDetails = (CourseUserControl)e.Row.FindItem("CourseUserControl1"); 

    // Assuming category ID is Int32 
    courseDetails.CourseCategoryID = (int)grdCategory.DataKeys[e.Row.RowIndex].Value; 
    courseDetails.DataBind(); 
} 

請注意,在爲當前行中的用戶控件分配新類別之後,我手動進行數據綁定而不是立即進行數據綁定。

欲瞭解更多信息,請參見: GridView.RowDataBound Event (ASP.NET 3.5)GridView.RowDataBound Event (ASP.NET 4.0)

+0

非常感謝主席先生您的快速答覆,但我通過手動創建解決它自己。使用Dim courseUC作爲CourseUserControl = LoadControl(「〜/ CourseUserControl.ascx」)但無論如何 – Androyds