• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

How do I Read values from previous aspx page?

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
TonyReba
Asked:
TonyReba
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
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
 
TonyRebaAuthor Commented:
would that the best way though>?

How I read the values from my code posted?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TonyRebaAuthor Commented:
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
 
revaluserCommented:
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
 
TonyRebaAuthor Commented:
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
 
revaluserCommented:
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
 
TonyRebaAuthor Commented:
what do you mean? in page1.aspx?

can you help me  on how to do this?
0
 
revaluserCommented:
how you are getting redirected to page2?
0
 
TonyRebaAuthor Commented:
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
 
revaluserCommented:
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
 
TonyRebaAuthor Commented:
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
 
revaluserCommented:
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
 
TonyRebaAuthor Commented:
yes this is what i do , in page1 right?
0
 
TonyRebaAuthor Commented:
but now based on that , i want to display listview in page 2
0
 
revaluserCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now