Solved

Accessing a code-behind variable in the .aspx page

Posted on 2004-04-06
7
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

731 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