asp.net用户控件绑定内的GridView [英] asp.net user controls binds inside gridview

查看:115
本文介绍了asp.net用户控件绑定内的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好老乡程序员,有几个问题就在这里。我加入一个GridView内的用户控件。
现在的问题是如何能够绑定它会导致用户控件中是gridviewthat需要在 CourseCatID 这样就可以绑定DATAS。顺便说一句,因为我需要渲染用于其他目的嵌套UserControl的,我不能使用嵌套griview。任何教程/帮助将非常乐意AP preciated。

Hello fellow programmers, got a few problem here. I am adding a user control inside a gridview. Now my question is how can bind it cause inside the user control is a gridviewthat needs the CourseCatID so that it could bind the datas. And by the way I cannot use nested griview cause I need the render of the nested usercontrol for another purpose. Any tutorial/help will be gladly appreciated.

<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>

感谢您的时间

推荐答案

您有几个选项。

最简单的一种是暴露在用户控件的公共属性,将让你做到这一点:

The easiest one is to expose a public property on the user control that will let you do this:

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

然后在数据绑定用户控件,只要该财产被指定给。请注意,我假设类是一个Int32。例如(请注意,CourseCategoryID在私有字段存储其值,而不是在ViewState中):

Then databind in the user control as soon as that property is assigned to. Note that I'm assuming the category is an Int32. For example (note that the CourseCategoryID stores its value in a private field, not in 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事件,并做到这一点:

Your other option is to expose the same property and handle the RowDataBound event and do this:

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();
}

请注意,我当前行用户控件分配一个新的类别后手动数据绑定的,而不是立即

Note that I'm databinding manually instead of immediately after assigning a new category to the user control in the current row.

有关详细信息,请参阅:
<一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.90%29.aspx\"相对=nofollow> GridView.RowDataBound事件(ASP.NET 3.5)
 要么
<一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.100%29.aspx\"相对=nofollow> GridView.RowDataBound事件(ASP.NET 4.0)

For more information, see: GridView.RowDataBound Event (ASP.NET 3.5) or GridView.RowDataBound Event (ASP.NET 4.0)

这篇关于asp.net用户控件绑定内的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆