Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing a code-behind variable in the .aspx page

Posted on 2004-04-06
7
Medium Priority
?
417 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

885 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