2012-01-20 49 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