Solved

Accessing a code-behind variable in the .aspx page

Posted on 2004-04-06
7
408 Views
Last Modified: 2012-05-04
I'm having difficulty getting a variable declared in the code-behind to display
in the .aspx page.  

Here is my variable and how I set it:

    Protected _numYouthsByCaseType As String

        Try

            rd.SetRequiredParamsForGet(resultList, ConfigurationSettings.AppSettings("connString"))
            ds = rd.GetRptOpenReferralsByJPOCaseTypeDataset(resultList, employeeid, casetypeid)

            Dim dt1 As DataTable = SelectDistinct("DistinctNameCount", ds.Tables(0), "YouthName")

 =>       _numYouthsByCaseType = dt1.Rows.Count

        Catch ex As Exception
            err.AddError(resultList, ResultItemType.RTPageError, ex)
        End Try

In my webform, I attempt to access the variable this way:

<asp:Label id="lblNumYouths" runat="server">Number of Youths per Case Type: <%= _numYouthsByCaseType %> </asp:Label>

When I run the report, the entire label disappears and I haven't been able to track it through
debugging.  I appreciate your input.
0
Comment
Question by:JeffDrummond
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10767040
since the Label's ID is lblNumYouths

then in your code-behind, you an simply assign the value like this:

Try

            rd.SetRequiredParamsForGet(resultList, ConfigurationSettings.AppSettings("connString"))
            ds = rd.GetRptOpenReferralsByJPOCaseTypeDataset(resultList, employeeid, casetypeid)

            Dim dt1 As DataTable = SelectDistinct("DistinctNameCount", ds.Tables(0), "YouthName")

             _numYouthsByCaseType = dt1.Rows.Count
             lblNumYouths.Text = _numYouthsByCaseType.ToString
        Catch ex As Exception
            err.AddError(resultList, ResultItemType.RTPageError, ex)
        End Try

see if that gets you wnat you need.

AW
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 10767487
Another alternative solution is set your variable into public type. Otherwise, Arthur_Wood's comment should did the trick.
eg:
Public numYouthsByCaseType as String
0
 

Expert Comment

by:skwash
ID: 10767589
yes, that should work although I dont think you need to do the .tostring

lblNumYouths.Text = "Number of Youths per Case Type: " + _numYouthsByCaseType
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:JeffDrummond
ID: 10767679
Thanks for the suggestions.  Help me out a little further.

The label is in the footer of a datagrid which is in the
item template section of a datalist in a template column.
Intellisense doesn't see the label so I haven't been able
to refer to it as described above.  Is there a method
of getting to the label properties programatically given
what I have described?

Thanks.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10767954
try making this change:

Protected _numYouthsByCaseType As String


to:

Public _numYouthsByCaseType As String
  and then use the code that you originally posted:

<asp:Label id="lblNumYouths" runat="server">Number of Youths per Case Type: <%= _numYouthsByCaseType %> </asp:Label>

That is just a guess, as I don't use Template sections.  AW
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 10767989
JeffDrummond ,
Alternative, you can get the label properties inside OnItemDataBound event.

Private Sub dlstFAQ_ItemDataBound(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstFAQ.ItemDataBound
        Try
            If e.Item.ItemType = ListItemType.Footer Then
                Dim lblNumYouths As Label= CType(e.Item.FindControl("lblNumYouths"), Label)
                lblNumYouths.Text = "Number of Youths per Case Type: " & _numYouthsByCaseType 'set your value here
            End If
        Catch expError As Exception
                Response.write(expError.ToString)
        End Try
End Sub
0
 

Author Comment

by:JeffDrummond
ID: 10768546
x_com:

I thought through your suggestion.  Since the datagrid is in the itemtemplate of the datalist,
by checking for the item in the ItemDataBound event I was able to get to the label and
its properties:

 Private Sub dlCaseTypes_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlCaseTypes.ItemDataBound

        Try

            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

                Dim lblNumYouths As Label = CType(e.Item.FindControl("lblNumYouths"), Label)
                lblNumYouths.Text = "Total Number of Youths in " & _caseType & ":  " & _numYouthsByCaseType

            End If

        Catch ex As Exception
            err.AddError(resultList, ResultItemType.RTPageError, ex)
        End Try

    End Sub

Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question