Solved

Accessing a code-behind variable in the .aspx page

Posted on 2004-04-06
7
405 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

9 Experts available now in Live!

Get 1:1 Help Now