.NET网格视图的动态列和数据源

时间:2022-07-13 15:44:59

I am working on a multi-purpose page and rather than adding multiple grids to the same page we wanted to use a single GridView to the page, and on Page_Init add the needed columns, and set the respective DataSourceID.

我正在开发一个多用途页面,而不是将多个网格添加到同一页面,我们希望在页面上使用单个GridView,并在Page_Init上添加所需的列,并设置相应的DataSourceID。

So to do this, we have something like the following in the aspx, the codebehind in the Page_Init is very simple adding a few columns then setting the DataSourceID property of the GridView.

所以为了做到这一点,我们在aspx中有类似下面的东西,Page_Init中的代码隐藏非常简单,添加几列然后设置GridView的DataSourceID属性。

ASPX:

<asp:GridView ID="gvDisplay" runat="server" AutoGenerateColumns="false" CellPadding="5"
    width="100%" AllowPaging="true" PageSize="200" DataSourceID="wuProcessLogDataSource">
    <RowStyle CssClass="RowStyle" />
    <AlternatingRowStyle CssClass="AlternatingRowStyle" />
    <HeaderStyle CssClass="HeaderStyle" />  
</asp:GridView>
<asp:ObjectDataSource id="wuProcessLogDataSource" runat="server" EnablePaging="True" 
    SelectMethod="GetWUProcessLog" TypeName="Project.Objects.WUProcessLogDal"
    SelectCountMethod="GetWUProcessLogTotalRecords">
   <SelectParameters>
    <asp:QueryStringParameter QueryStringField="w" DefaultValue="0" Name="workunitId" />
   </SelectParameters>    
</asp:ObjectDataSource>

The object data source is there and working as the first page load triggers without any issues at all. However, as soon as you click on a page button, the grid disappears from the page? Any ideas?

对象数据源在那里并作为第一页加载触发器而没有任何问题。但是,只要单击页面按钮,网格就会从页面中消失?有任何想法吗?

I would just use a DataGrid but it doesn't have the desired dynamic display abilities for the HyperLinkColumn.

我只是使用DataGrid但它没有HyperLinkColumn所需的动态显示能力。

2 个解决方案

#1


1  

It sounds like you're doing something like

听起来你正在做类似的事情

If (!Page.IsPostBack)
{
   //create + add columns - set datasource etc
}

If that's the case - then you need to remove the check and always generate the columns (I'd also suggest disabling viewstate for the datagrid)

如果是这种情况 - 那么你需要删除检查并始终生成列(我还建议禁用数据网格的viewstate)

#2


1  

try the page load event instead of page init

尝试页面加载事件而不是页面init

#1


1  

It sounds like you're doing something like

听起来你正在做类似的事情

If (!Page.IsPostBack)
{
   //create + add columns - set datasource etc
}

If that's the case - then you need to remove the check and always generate the columns (I'd also suggest disabling viewstate for the datagrid)

如果是这种情况 - 那么你需要删除检查并始终生成列(我还建议禁用数据网格的viewstate)

#2


1  

try the page load event instead of page init

尝试页面加载事件而不是页面init