Solved

How do I Read values from previous aspx page?

Posted on 2010-09-02
16
619 Views
Last Modified: 2012-05-10
What is the easiest and best way to read the values from an aspx page I have. I want to pass wheter each checkbox is checked or not to a second page. Thanks in advance


'#####################  Checkboxes ###################################



    Private ReadOnly Property IDs() As List(Of Integer)
        ' Create a list of ID's that are selected.  ID's is the primary
        ' Key for this table
        Get
            If Me.ViewState("ID") Is Nothing Then
                Me.ViewState("ID") = New List(Of Integer)()
            End If
            Return CType(Me.ViewState("ID"), List(Of Integer))
        End Get
    End Property

    'Create a Sub that will store the selected row ID's into your list variable
    Protected Sub AddRowstoIDList()
        ' Loop through all the current items in the Listview
        For Each lvi As ListViewDataItem In ListView1.Items
            ' Find the checkbox in each row
            Dim CheckBoxPrintProvider As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider"), CheckBox)
            ' If the checkbox is ticked then add the corresponding ID to our private list
            If (Not (CheckBoxPrintProvider) Is Nothing) Then
                ' Get the ID from the datakeynames property
                Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
                If (CheckBoxPrintProvider.Checked AndAlso Not Me.IDs.Contains(ID)) Then
                    ' Add the ID to our list
                    Me.IDs.Add(ID)
                    '
                    '  AddFunction(CType(lvi.FindControl("hdnID"), HiddenField).Value)

                ElseIf (Not CheckBoxPrintProvider.Checked AndAlso Me.IDs.Contains(ID)) Then
                    ' Not checked - remove the ID from our list
                    Me.IDs.Remove(ID)
                End If
            End If
        Next
    End Sub

    Protected Sub ListView1_PagePropertiesChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles ListView1.PagePropertiesChanging
        AddRowstoIDList()
    End Sub

    Protected Sub ListView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles ListView1.Sorting
        AddRowstoIDList()
    End Sub


    Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound

        ' '' Get each Listview Item on DataBound
        ''Dim lvi As ListViewDataItem = e.Item
        ''If (lvi.ItemType = ListViewItemType.DataItem) Then
        ''    ' Find the checkbox in the current row
        ''    Dim CheckBoxPrintProvider As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider"), CheckBox)
        ''    ' Make sure we're referencing the correct control
        ''    If (Not (CheckBoxPrintProvider) Is Nothing) Then
        ''        ' If the ID exists in our list then check the checkbox
        ''        Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
        ''        CheckBoxPrintProvider.Checked = Me.IDs.Contains(ID)
        ''    End If
        ''End If

        '' Creating a loop through entire list
        '' Dim Id As Integer
        ''For Each lvi As ListViewDataItem In ListView1.Items
        ''If (ListView1.It = DataControlRowType.DataRow) Then
        ''If (CType(ListView1.FindControl("CheckBoxPrintProvider"), CheckBox).Checked = True) Then
        ''AddFunction(CType(lvi.FindControl("hdnID"), HiddenField).Value)
        ''End If
        ''End If
        ''Next

        'For Each lvi As ListViewDataItem In ListView1.Items

        '    Dim tb As TextBox = Page.Master.FindControl("txtMsg")
        '    ' Find the checkbox in each row
        '    Dim CheckBoxPrintProvider As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider"), CheckBox)
        '    ' If the checkbox is ticked then add the corresponding ID to our private list
        '    If (Not (CheckBoxPrintProvider) Is Nothing) Then
        '        ' Get the ID from the datakeynames property
        '        Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
        '        If (CheckBoxPrintProvider.Checked AndAlso Not Me.IDs.Contains(ID)) Then
        '            ' Add the ID to our list
        '            Me.IDs.Add(ID)
        '            '
        '            ' Gets the item that contains the CheckBox object.
        '            Dim item As ListViewDataItem = CType(CheckBoxPrintProvider.Parent.Parent, ListViewDataItem)
        '            '  AddFunction(CType(lvi.FindControl("hdnID"), HiddenField).Value)
        Dim myButtonPrint As Button = CType(ListView1.FindControl("PrintButton"), Button)
        '            tb.Text = "TransID sent = " & ListView1.DataKeys(item.DataItemIndex).Value

        ' myButtonPrint.PostBackUrl = "PrintableProviderList.aspx"
        '        ElseIf (Not CheckBoxPrintProvider.Checked AndAlso Me.IDs.Contains(ID)) Then"
        '            ' Not checked - remove the ID from our list
        '            Me.IDs.Remove(ID)
        '        End If
        '    End If
        'Next

        'Dim cb As CheckBox = TryCast(ListView1.FindControl("CheckBoxPrintProvider"), CheckBox)
        'Dim IdVal As String
        ''  IdVal = ListView1.DataKeys
        'Dim myButtonPrint As Button = CType(ListView1.FindControl("PrintButton"), Button)
        '' myButtonPrint.PostBackUrl = "PrintableProviderList.aspx?Id=" & CType(e.Item.DataItem, System.Data.DataRowView)("ID").ToString()

    End Sub

    Protected Sub Check_Clicked()

        AddRowstoIDList()
        Dim myButtonPrint As Button = CType(ListView1.FindControl("PrintButton"), Button)
        myButtonPrint.Attributes.Add("onclick", "popChosen()")
        ' Gets the item that contains the CheckBox object.
        ' Dim item As ListViewDataItem = CType(chkBox.Parent.Parent, ListViewDataItem)

        ' Replace ListView1 with ID of your ListView Control
        'tb.Text = "TransID sent = " & ListView1.DataKeys(item.DataItemIndex).Value
        'Response.Redirect("PrintableProviderList.aspx")
    End Sub

Open in new window

0
Comment
Question by:TonyReba
16 Comments
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
Comment Utility
Use the session is the best way than view state. In view state you need to check that page is postback or not in every time. But in session you stored the values once, it will expired when you close the application.
Session variable are unique per each user. If you open same url in two browser then server will create two independent sessions for you. Keep in mind that each session has a expire time. You can configure session time from IIS. Also one another thing is, you can receive session value form any pages after assigning.

For example Write the below code under page1 button:
protected void cmdTransfer_Click(object sender, EventArgs e)
{
Session["myname"] = txtName.Text;
Response.Redirect("page2.aspx");
}

Now read session variable value from page2:
string myname ="";
if(Session["myname"]!=null)
myname=Session["myname"].ToString();

May it will help you what you are looking.

Thanks
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
would that the best way though>?

How I read the values from my code posted?
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
I want to read the ID from a database record from each record displayed in my list view  + get if the field checkbox waqs checked and pass that info to a second page.

Can someon please show me how , a good sample based on my code above??
0
 
LVL 3

Expert Comment

by:revaluser
Comment Utility
in the first page have link button and for the postbackurl set it to second page url and in the second page Page_load use previouspage.findcontrol functions to find the checkbox, this gets you the instance of the previous page checkbox in the 2 ns page and check whether they are checked or not in the 2nd page

0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
OK, getting there,,

this is the chekbox inside my itemtemplate of listview1
<asp:CheckBox ID="CheckBoxPrintProvider" runat="server" Text='<%#Eval("ID")%>'/>

In my page1.aspx code behind I have

 Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound

        Dim commaStr As New CommaDelimitedStringCollection()

        For Each lvi As ListViewItem In ListView1.Items
            Dim chb As CheckBox = DirectCast(lvi.FindControl("CheckBoxPrintProvider"), CheckBox)
            If chb.Checked Then
                commaStr.Add(chb.Text)
            End If
            'str should contain the comma separated string...
            Dim str As [String] = commaStr.ToString()
        Next

        Dim myButtonPrint As Button = CType(ListView1.FindControl("PrintButton"), Button)
        myButtonPrint.PostBackUrl = "PrintableProviderList.aspx"

now what I need to add in my page2 in order to get the values of each id which that checkebox was checked

this is just an idea



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim txt As CheckBox



        ' Find the server name on the previous page

        txt = CType(Page.PreviousPage.FindControl _

            ("CheckBoxPrintProvider"), CheckBox)

        If Not IsNothing(txt) Then

            prevServerName.Text = Server.HtmlEncode(txt.Text)

        Else

            prevServerName.Text = "[Name Not available]"

        End If

    End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:revaluser
Comment Utility
Idea is correct but you need to find list view first and in that list view use for loop to get each control and find the checkbox and its checked status

0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
what do you mean? in page1.aspx?

can you help me  on how to do this?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Expert Comment

by:revaluser
Comment Utility
how you are getting redirected to page2?
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
I am using postback url ,

I am using findcontrol in page1 though sicne button is insisde a listview template

 Dim myButtonPrint As Button = CType(ListView1.FindControl("PrintButton"), Button)
        myButtonPrint.PostBackUrl = "PrintableProviderList.aspx"
0
 
LVL 3

Expert Comment

by:revaluser
Comment Utility
so you are finding the previous page controls in "PrintableProviderList.aspx" and everything need to work fine but in the "PrintableProviderList.aspx" you need to find the Listview not checkbox directly
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
But I need to check wheter chebox is checked, and then only display those in listview in PrintableProviderList,,


Could you suggest a code that can do that?:
0
 
LVL 3

Expert Comment

by:revaluser
Comment Utility
Yeah thats what you need to find the listview of the firstpage first and loop through the listview to find the checkbox and check whether they are checked or not
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
yes this is what i do , in page1 right?
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
but now based on that , i want to display listview in page 2
0
 
LVL 3

Accepted Solution

by:
revaluser earned 500 total points
Comment Utility
is this being done in 2nd page

' Find the server name on the previous page
 Dim txt  as New listview
        txt = CType(Page.PreviousPage.FindControl _
            ("listview"), listview)

for(listview)
'    Find the checkbox
' check if it is checked or not
end for
       
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 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

10 Experts available now in Live!

Get 1:1 Help Now