views:

1231

answers:

8

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 ?

A: 

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?

Ben Scheirman
A: 

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.

Joel Coehoorn
A: 

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)

A: 

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.

Joel Coehoorn
A: 

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

}

Tom
A: 

i need to retain the table rows , i dont want to use a repeater

A repeater WILL keep your table rows.
Joel Coehoorn
+1  A: 

Inline Code executes during the Render stage.

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

Jimmy
A: 

Yes Load happens before render.Still i am not able to use a datatable created in Page Load. Y ????????

This kind of response should be added as a comment to the specific answer. But to answer your new question, one possibility is that the datatable is scoped to the load method rather than the whole page class.
Joel Coehoorn
DataTable is declared as public
Yes, but did you declare it inside the load method? If so, it goes out of scope and will be collected when the method ends.
Joel Coehoorn
But I declared it as public just before the pageLoad method , public DataTable dtblChild; protected void Page_Load(object sender, EventArgs e) {