Solved

Accessing a code-behind variable in the .aspx page

Posted on 2004-04-06
7
406 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now