ASP.Net / C# / Visual Studio.Net Accessing Code Behind Methods and Properties from the Designer

L

Luminousc

I'm getting my feet wet learning C# and .net from a VBScript
background. I have built a small application that uses custom 'info'
objects, each of which contains a single record from a SQL database. I
have bound an array of these objects to a datagrid and have been
trying to work within the limited confines of the built-in ASP.net
Datagrid control. When speaking with a colleague who has a C++
background he suggested that I create my own grid control. I've been
trying to do this by putting all the code that dimensions the array
and populates it into my codebehind and simply creating a 'for' loop
in my HTML code with a reference to each object in the array in html
table cells within the loop. My problem is I can't seem to access the
array object defined in the codebehind in the C# embedded in my HTML
code, I'm guessing that the line at the top of the page:

'<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="RequestsGrid.ascx.cs"
Inherits="DTS_UI.Components.requestgrid"%>'

is supposed to take care of this but that doesn't seem to be the case.
Can anyone point out what I'm missing?

Luminousc
 
L

luminousc

OK, I worked out that my array object needed to be set to public or
protected in order to be able to see it in the designer. Got that,
great. However I now have this strange issue:

<%for (int xx=0; xx<=myInfoRequestArr.Length; xx++){%>
<tr style="FONT-SIZE:smaller; ForeColor:Black; BackColor:#FFA0A0">
<td Style="FONT-SIZE:7pt; WIDTH:23px; HorizontalAlign:Left">
<%# myInfoRequestArr[xx].RequestID.ToString() %>
</td>
</tr>
<%}%>

When I try and run this I get:
" CS0103: The name 'xx' does not exist in the class or namespace
'ASP.RequestsGrid_ascx' "

Why can't the second code-block see the xx integer variable from the
first?!?!

Luminousc
 
S

Scott Allen

The data binding expressions (inside of <%# %>) are put into a
different scope when code is generated for this aspx page, and this
code will execute when you call DataBind on the page.

If you want a finer grain of control over the output without having to
write all the looping code, etc., I'd suggest trying the asp.net
Reapeater control.

Here is some more info on what happens with data binding expressions:

http://odetocode.com/Articles/278.aspx
 
K

Karl Seguin

<%# myInfoRequestArr[xx].RequestID.ToString() %>

needs to change to

<%= myInfoRequestArr[xx].RequestID.ToString() %>

also you'll need to change your for statement to be:

for (int xx=0; xx<myInfoRequestArr.Length; xx++){

(notice how it' sjust less than (<) vs less than or equal (<=)

<%# is used for databinding and isn't evaluated at the same time as <%
which is why xx isn't declared


You do know that ASP.Net offers databound controls which make this type of
code bad, right?

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
L

luminousc

Thanks guys, I have been playing with the built-in DataGrids, DataList
and the Repeater, My actual page is very much more complicated than my
example, I just simplified it down to the basics so as to make my query
easier to read. Switching to '=' instead of the '#' I've seen in all
the other examples I've so-far seen has fixed the problem.

Thanks very much

Luminousc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top