ASP.NET 无法通过内联编码访问在页面加载中创建的数据表

发布于 2024-07-07 06:30:19 字数 195 浏览 6 评论 0原文

在代码隐藏文件的 Page_Load 事件中,我将数据加载到数据表中。在我的 .aspx 页面中,我有一些内联编码,我想显示此数据表中的一些数据。但是当我运行程序时,它显示类似“错误 64 使用未分配的局部变量‘dtblChild’”的错误 dtblChild 是我的 DataTable 对象

代码隐藏中的 Page_Load 在加载表单元素后执行吗?

In my Page_Load event of codebehind file, I am loading data in to a datatable.In my .aspx page I am having some inline coding,I want to display some data from this datatable.But when i am running the program,It is showing an error like "Error 64 Use of unassigned local variable 'dtblChild' "
dtblChild is my DataTable Object

Is Page_Load in codebehind executes after loading the form elements ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光礼记 2024-07-14 06:30:19

内联代码在渲染阶段执行。

在页面的生命周期中,渲染发生的时间比加载晚得多

Inline Code executes during the Render stage.

in the lifecycle of a page, Render happens much later than Load

森林散布 2024-07-14 06:30:19

是的,页面加载在所有服务器控件加载到内存后执行,但是如果您要使用数据表,则必须确保每次刷新时都会重新填充数据表。

我认为您的问题表明您绑定到页面的方式存在一些更深层次的问题。

你能发布一些代码吗?

Yes, the page load executes after all of the server controls have been loaded into memory, but you have to ensure that the datatable is repopulated on each refresh if you're going to use it.

I think your question indicates some deeper problems with the way you're binding to the page.

Can you post some code?

温馨耳语 2024-07-14 06:30:19

尝试使用中继器控件或 datagridview 而不是 <% %> 蜜蜂蜇伤。 太多的内联代码与文件后面的代码不能很好地混合:您可能会得到意想不到的结果。 如果您真正知道自己在做什么,它就可以工作,但您必须小心,我不会向任何刚接触 ASP.Net 的人推荐它。

我还建议您花一些时间并熟悉 ASP.Net 页面生命周期

Try using a repeater control or datagridview rather than <% %> bee stings for this. Too much inline-code doesn't mix very well with code behind files: you can get unexpected results. It can work if you really know what you're doing, but you have to be careful and I wouldn't recommend it for anyone new to ASP.Net.

I also recommend that you take some time and become very familiar with the ASP.Net Page Lifecycle.

征﹌骨岁月お 2024-07-14 06:30:19

这是我的内联编码

<%
foreach(dtblChild.Rows 中的 DataRow dr)
{
%>
这个sss
<

  }

%%>

在 Page_Load 后面的代码中,我将数据填充到 DataTable 对象(dtblChild)

This is my inline coding

</tr>
<%
foreach (DataRow dr in dtblChild.Rows)
{
%>
<tr><td>Thissss</td></tr>
<%

  }

%>

in my code behind Page_Load ,i am populating Data to a DataTable object (dtblChild)

感性 2024-07-14 06:30:19

更“ASP.Net-ish”的方法是这样的:

<asp:Repeater ID="MyRepeater" runat="server" DataSource="dtblChild">
    <ItemTemplate>
        <tr><td>Thisss</td></tr>
    </ItemTemplate>
</asp:Repeater>

您可能还需要声明一个 ObjectDatasource 来包装您的数据表,以使其与中继器兼容。

The more "ASP.Net-ish" way to do this is like this:

<asp:Repeater ID="MyRepeater" runat="server" DataSource="dtblChild">
    <ItemTemplate>
        <tr><td>Thisss</td></tr>
    </ItemTemplate>
</asp:Repeater>

You probably also need to declare an ObjectDatasource to wrap your datatable to make it compatible with the repeater.

情何以堪。 2024-07-14 06:30:19

按照上面的建议将 dtblChild 绑定到中继器(或 DataGrid)。 维护起来会更容易。 然后在单独的函数中填充 DataTable 并将其绑定到转发器。 需要时在 Page_Load 中调用该函数。

<table>
<asp:Repeater ID="rptSearchResults" runat="server">
    <ItemTemplate>
        <tr>
            <td><%#DataBinder.Eval(Container.DataItem, "ColumnName") %></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
</table>

函数LoadData()
{

rptSearchResults.DataSource = dtblChild;

rptSearchResults.DataBind();

}

Bind your dtblChild to a repeater (or DataGrid) as suggested above. It will be easier to maintain. Then populate the DataTable in a separate function and bind it to the repeater. Call that function in Page_Load when you need to.

<table>
<asp:Repeater ID="rptSearchResults" runat="server">
    <ItemTemplate>
        <tr>
            <td><%#DataBinder.Eval(Container.DataItem, "ColumnName") %></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
</table>

function LoadData()
{

rptSearchResults.DataSource = dtblChild;

rptSearchResults.DataBind();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文