GridView中的DataBind用户控件 [英] DataBind User Controls in Gridview

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

问题描述

 < asp:GridView ID = GVWunitcollection
CssClass =gridview
runat =server
ShowHeader =false
ShowFooter =false
AutoGenerateColumns =False>
< HeaderStyle CssClass =headerstyle/>
< RowStyle CssClass =rowstyle row/>
< FooterStyle CssClass =rowstyle row/>
<列>
< asp:TemplateField>
< ItemTemplate>
< uc:Unit ID =UNTcolrunat =server/>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>

我将GridView绑定到一个列表< T> 填写自定义单位类。每个单元由填充用户控件的几个属性组成。 (用户控件保存一个表,一些标签和文本框)。
我该如何实现?有没有一个简单的方法来将每个用户控件绑定到相应的单元?



顺便说一句,这是我模仿页面上多个用户控件的动态行为的方式。如果有一个更简单的方法,我想知道如何!



谢谢!

解决方案

您应该处理OnRowDataBound事件,然后使用FindControl和事件参数上的DataItem属性来提取要绑定的数据。您应该在用户控件上公开属性以分配值。这是一个例子:

 < asp:GridView ID =gvTestrunat =serverEnableViewState =false
OnRowDataBound =gvTest_RowDataBoundAutoGenerateColumns =false>
<列>
< asp:TemplateField>
< ItemTemplate>
< asp:Label ID =lblTestrunat =server>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>

  
protected void Page_Load(object sender,EventArgs e)
{
gvTest.DataSource = new [] {1,2,3,4};
gvTest.DataBind();
}

protected void gvTest_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
int item =(int)e.Row.DataItem;
标签lblTest =(Label)e.Row.FindControl(lblTest);
lblTest.Text = item.ToString();
}
}

而不是int,您应该转换为特定的数据类型,而不是Label,您应该转换为您的用户控件类型。而不是Label的Text属性,您应该从您的用户控件中暴露出来的属性。


I got a GridView in ASP.Net in which a user control is placed:

<asp:GridView ID="GVWunitcollection"
        CssClass="gridview"
        runat="server"
        ShowHeader="false"
        ShowFooter="false"
        AutoGenerateColumns="False">
        <HeaderStyle CssClass="headerstyle" />
        <RowStyle CssClass="rowstyle row" />
        <FooterStyle CssClass="rowstyle row" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <uc:Unit ID="UNTcol" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

I bind the GridView to a List<T> filled with custom 'Unit' Classes. Each Unit consists of several properties to fill the usercontrol. (The user control holds a table, and some labels and textboxes). How can I achieve this? Is there a simple way to bind each usercontrol to the appropriate Unit?

By the way, this is my way to mimic a "dynamic" behavior of multiple usercontrols on a page. If there's a more simple way, I would like to know how!

thank you!

解决方案

You should handle the OnRowDataBound event then use FindControl and the DataItem property on the event argument to extract the data you are binding to. You should expose properties on the user control to assign values to. Here is an example:

<asp:GridView ID="gvTest" runat="server" EnableViewState="false" 
OnRowDataBound="gvTest_RowDataBound" AutoGenerateColumns="false">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label ID="lblTest" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>


protected void Page_Load(object sender, EventArgs e)
{
    gvTest.DataSource = new[] { 1, 2, 3, 4 };
    gvTest.DataBind();
}

protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int item = (int)e.Row.DataItem;
        Label lblTest = (Label)e.Row.FindControl("lblTest");
        lblTest.Text = item.ToString();
    }
}

Instead of int you should cast to your specific datatype and instead of Label you should cast to your user control type. Instead of the Label's Text property you should assing to the property you've exposed from your user control.

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

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