Solved

Urgent: How to populate selected records in a popup window

Posted on 2007-04-11
46
267 Views
Last Modified: 2008-01-09
Greeting,
I use Repeater to display data on the asp.net form. It contains a checkbox field which is used to determine which row need to be updated. Once the form being submitted, I want to have a popup window which use repeater to show all the selected records from the previous(main) form. The main form will be redirected to another page and popup window need to be on top of the screen.

Thanks in advance.
0
Comment
Question by:mrong
  • 26
  • 19
46 Comments
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

I would do the following:

first store the original dataset/datarows in the session
then on submit go through the repeater items and check whether the checkbox is checked or not and store these rows in another session-variable.

protected void submit_click(object sender, EventArgs e) {
DataSet ds = (DataSet)Session["rowdata"];
ArrayList rows = new ArrayList();

for(int i = 0; i < repeater.Items.Length; i++) {

  CheckBox chb = (CheckBox)repeater.Items[i].findControl("chb");
  if ((chb != null) && (chb.Checked)) {
    rows.Add(ds.Tables[0].Rows[i]);
  }
}
Session["updateRows"] = rows;
}

to popup a window use the onload event in the body-tag of your page

<body onload="window.open('updateRows.aspx')">

where updateRows.aspx is the popup-page. in Page_load of this page get the rows from the session and bind them to the repeater

protected void Page_Load() {
  if (!IsPostBack) {

     uptRepeater.DataSource = Session["updateRows"];
     uptRepeater.DataBind();
  }
}

I hope that helps,

Thalox
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
Is your code in VB.net? I tried your code in my onClick function ..it returned me lots of error. Below is what I have for onClick function.

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
'code goes here
End Sub

Thanks.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

no sorry in c#
I do not use vb very often, so this will probably not be correct (I think casts are handled different in vb), but it should be easy to correct the rest

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
DataSet ds = (DataSet)Session["rowdata"]
rows as ArrayList = new ArrayList()
dim i as integer

for  i = 0 to repeater.Items.Length - 1

  dim chb as CheckBox = (CheckBox)repeater.Items[i].findControl("chb")
  if (chb != null) && (chb.Checked) then
    rows.Add(ds.Tables(0).Rows(i))
  endif
endfor
Session["updateRows"] = rows;
end sub
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
I changed your code a little bit and no syntax error any more. I will test it soon.
For popup a window,  does it popup a new window everytime that page being reloaded?

Thanks.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

i found out how to cast ;)


Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
dim ds as DataSet
ds = CType(Session["rowdata"], DataSet)
dim rows as ArrayList = new ArrayList()
dim i as integer

for  i = 0 to repeater.Items.Length - 1

  dim chb as CheckBox = CType(repeater.Items(i).findControl("chb"), CheckBox)
  if (chb != null) && (chb.Checked) then
    rows.Add(ds.Tables(0).Rows(i))
  endif
endfor
Session["updateRows"] = rows
end sub
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
it will popup a a window everytime yes.

if you need to prevent this you will have to set this in codebehind

<body <%= onLoadFunction %> >

codebehind

public onLoadFunction as String

sub page_load()

' check if there are data to display
if Session["rowdata"] = Nothing then
' do nothing
onLoadFunction = ""
else
onLoadFunction = "onload=\"window.open('updateRows.aspx')\""
endif
end sub

you will have to remove the rows from the session to disable the popup, but I dont know enough about your program to say anything about where to do this...
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
I got error "End of statement expected".

Sub page_load()

        ' check if there are data to display
        If Session("rowdata") Is Nothing Then
            ' do nothing
            onLoadFunction = ""
        Else
        onLoadFunction = "onload=\"window.open('Email.aspx')\""    <--------error here
        End If
    End Sub

BTW, how to remove the orws from the session?
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
the error is because c# <> vb

try this

onLoadFunction = "onload=""window.open('Email.aspx')"""

to remove it from session
Session.Remove("rowdata")
0
 

Author Comment

by:mrong
Comment Utility
Thalox,

I tested the code, but it didn't popup the window which it suppose to...
Please let me know if you need me to post any function.
Thanks.
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
My bad. It did popup the window but got error on the poup window. Saying "DataBinding 'System.Data.DataRow' does not contain a property with the name 'proposal'".
'proposal' is the a database field which I retrieved from SQL stmt.

in my code I have
/load data from the database
MyOracleAdapt.Fill(MyOracleDS)
Session("MyDS") = MyOracleDS

 ds = CType(Session("MyDS"), DataSet)
        Dim rows As New ArrayList()

//in the update stmt, I have the following. Session("updateRows") is being pass to the popup window.
 rows.Add(ds.Tables(0).Rows(i))
 Session("updateRows") = rows
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

sorry cant try this, but I think the databinder doesn't "recognize" a list of datarows the same as datarows in a datatable and so databinding works different there.

you could try to store a whole dataset with the rows in the session, not only the rows.

something like this (syntax errors not excluded ;)):

Dim updDs as DataSet = new DataSet()
Dim tab  as DataTable = new DataTable()
updDs.Tables.Add(tab)
' assign column from original dataset-table to new one
tab.Columns = ds.Tables(0).Columns
' add rows
tab.Rows.Add(ds.Tables(0).Rows(i).ItemArray)
' store in session
Session("updateRows") = updDs


hope this helps

Thalox

0
 

Author Comment

by:mrong
Comment Utility
Thalox,
Thank you for your post.
How to assign column from original dataset-table to new one in VB.net?

Thanks.

0
 

Author Comment

by:mrong
Comment Utility
I treid your code below but didn't work.
tab.Columns = ds.Tables(0).Columns
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
ah, yepp cannot assign table.Columns

try this

' go through columns and create a new one with its settings
for(int i = 0; i < ds.Tables(0).Columns.Count; i++)
{
      DataColumn col = (DataColumn)ds.Tables(0).Columns(i)
      tab.Columns.Add(new DataColumn(col.ColumnName, col.DataType))
}
0
 

Author Comment

by:mrong
Comment Utility
Thalox,

Below is what I have. It runs for the first time then gave me message "A column named 'proposal' already belongs to this DataTable". I guess I have to clean the table everytime....How to do it? Thanks.

 Dim col As New System.Data.DataColumn
                    Dim j
                    For j = 0 To ds.Tables(0).Columns.Count - 1
                        col = ds.Tables(0).Columns(j)
                        tab.Columns.Add(New DataColumn(col.ColumnName, col.DataType))
                    Next
                    tab.Rows.Add(ds.Tables(0).Rows(i).ItemArray)
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
tab.Columns.Clear()
0
 

Author Comment

by:mrong
Comment Utility
I added the following code but still got the same error msg.
tab.Columns.Clear()
tab.Rows.Clear()
updDs.Clear()
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

try to debug your app and see what is in tab.Columns before you add a new one, and what is in col (in the for loop)

what do you mean with "It runs for the first time " the whole sub (displays the rows) or only the first round in your for loop?
0
 

Author Comment

by:mrong
Comment Utility
When I tested the app first time, it didn't give me any error. After that, it always say "A column named 'proposal' already belongs to this DataTable".
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

then either  tab.Columns is not empty before the for loop or ds.Tables(0).Columns(j)
points to the same col every time.
does i increment each run in the for loop automatically?
0
 

Author Comment

by:mrong
Comment Utility
I think the problem is caused by the index
Below is what I have for now. Do I need another loop for row(i)?
 tab.Columns.Clear()

                    tab.Rows.Clear()
                    Dim col As New System.Data.DataColumn
                    Dim j
                    For j = 0 To ds.Tables(0).Columns.Count - 1
                        tab.Columns.Add(New DataColumn(col.ColumnName, col.DataType))
                        col = ds.Tables(0).Columns(j)              
                    Next
                    tab.Rows.Add(ds.Tables(0).Rows(i).ItemArray)
0
 
LVL 9

Accepted Solution

by:
Thalox earned 500 total points
Comment Utility
do you do this within the loop to add the rows?

this is wrong.

first create the table with columns

tab.Rows.Clear()
                    Dim col As New System.Data.DataColumn
                    Dim j
                    For j = 0 To ds.Tables(0).Columns.Count - 1
                        tab.Columns.Add(New DataColumn(col.ColumnName, col.DataType))
                        col = ds.Tables(0).Columns(j)              
                    Next

then insert the rows in another loop

for  i = 0 to repeater.Items.Length - 1

  dim chb as CheckBox = CType(repeater.Items(i).findControl("chb"), CheckBox)
  if (chb != null) && (chb.Checked) then
    tab.Rows.Add(ds.Tables(0).Rows(i))
  endif
endfor
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mrong
Comment Utility
Thomax,
I did exactly as you suggested above got error "This row already belongs to another table" at the following line.
 tab.Rows.Add(ds.Tables(0).Rows(i))

If I use  tab.Rows.Add(ds.Tables(0).Rows(i).ItemArray), I gto error "The string was not recognized as a valid DateTime. There is a unknown word starting at index 5"
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
sorry, copy + paste this loop from my comment before, ... itemarray is right.

the other error is because one of your datetime values is somehow no date.

try to change the row

tab.Columns.Add(New DataColumn(col.ColumnName, col.DataType))

to

tab.Columns.Add(New DataColumn(col.ColumnName, typeof(string)))          ' not sure if this is right for vb, means all columns have datatype string

this should at least display the data
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
In vb, the syntax is the following.
tab.Columns.Add(New DataColumn(col.ColumnName, GetType(String)))

Two more things.
1. The window still popup when I refresh the window(not re-submit).
I followed your instruction.
In Page_load I have the following:
If Session("updateRows") Is Nothing Then
            ' do nothing
            onLoadFunction = ""
        Else
            onLoadFunction = "onload=""window.open('Email.aspx')"""
        End If

In Update function I have:
Session("updateRows") = updDs
Response.Redirect("Report.aspx?shop=" & Session("shop"))
Session.Remove("updateRows")

I thought this will remove the session once submitted the form, but it didn't work.

2.The window popup....but it didn't show me the field("proposal") as I like..instead it shows me a day field which I have no diea where it from.
in the code-behind of popup window I have the following.
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Repeater5.DataSource = Session("updateRows")
            Repeater5.DataBind()
        End If
    End Sub
on aspx I have the following:
 <td><div align=left><%#DataBinder.Eval(Container.DataItem, "proposal")%></td>

Btw, it always show me one row on the popup windw  even I selected multiple rows from the previous page.

Thank you for your help.




       

0
 

Author Comment

by:mrong
Comment Utility
Tholax,
I have fixed problem 1 above and working on Problem 2 now.
Thanks.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
I think you should make more use of the debugger.

check what rows are added to the update dataset and with which values
also check what you get from from the session in the popup window. how many rows are there (as many as written into this dataset?) and what values are there, especially in the proposal column
0
 

Author Comment

by:mrong
Comment Utility
Thalox,

I tried to debug it but got forbidden message..I am running remotely from the server.
I guess it should be much easier if I can watch what is happening thru the code.
Any Suggestions?

How to check what I am getting from the session?

Thanks.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

In ancient times one wrote everything need to know directly in the page ;)
thats the only way I can think of.
declare a label in your page and wrote to it what you need.

but I think it would be much easier to develop local (with debugging) and port it to the server when it is done.

0
 

Author Comment

by:mrong
Comment Utility
Thalox,
The column problem has been fixed..now the problem is the row.
Instead giving me the row I selected from the previous window...it gives me the top row of the refreshed page.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

how do you fill the repeater to display the rows you select from?
make sure you dont refill the repeater on postback

if not IsPostBack then
' fill data
...
endif

when you fill the update ds:
is Repeater.Items.Length the same as the number of rows displayed?
does the loop go through each row?
do you find the checkbox-control for every row? if so, is it checked (and are the same rows checked as you did in the browser?)

what do you mean with "refreshed page"?

0
 

Author Comment

by:mrong
Comment Utility
Say I have 5 rows in form1, and I selected top 2 rows on form1. After I submit form1, I have bottom 3 rows(left over) in form1. In form2(popup form), I have top 2 rows of those 3 left over on form1.
No sure what's going on.

0
 

Author Comment

by:mrong
Comment Utility
Below is my page_load in form1

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

        If Session("updateRows") Is Nothing Then
            onLoadFunction = ""
        Else
            onLoadFunction = "onload=""window.open('Email.aspx')"""

        End If

        Session.Remove("updateRows")
        Session("shop") = Trim(Request.QueryString("shop"))
        Session("MyName") = Request.Cookies("MyName")
        If Request.Cookies("MyName") Is Nothing Then
            Response.Redirect("login.asp")
        End If

        If Not Me.IsPostBack Then

            Dim sqlText As String = "MySQL here...."
            Dim MyOracleConn As New System.Data.OracleClient.OracleConnection("Data Source=DB;Persist Security Info=True;User ID=ROOT;Password=PWD;Unicode=True")

            Dim MyOracleCMD As New System.Data.OracleClient.OracleCommand(sqlText)

            MyOracleCMD.Connection = MyOracleConn

            Dim MyOracleAdapt As New System.Data.OracleClient.OracleDataAdapter(MyOracleCMD)
            Dim MyOracleDS As New System.Data.DataSet()

            MyOracleAdapt.Fill(MyOracleDS)

            If MyOracleDS.Tables(0).Rows.Count = 0 Then
                Server.Transfer("none.aspx")
            End If
            Repeater1.DataSource = MyOracleDS
            Repeater1.DataBind()
            Session("MyDS") = MyOracleDS
        End If

    End Sub
0
 

Author Comment

by:mrong
Comment Utility
Below is what I have for Update-Click on form1

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click

        Dim ds As New System.Data.DataSet()
        ds = CType(Session("MyDS"), DataSet)

        Dim rows As New ArrayList()

        Dim updDs As DataSet = New DataSet()
        Dim tab As DataTable = New DataTable()
        updDs.Tables.Add(tab)
        tab.Rows.Clear()
        tab.Columns.Clear()
        Dim col As New System.Data.DataColumn
        Dim j
        For j = 0 To ds.Tables(0).Columns.Count - 1
            col = ds.Tables(0).Columns(j)
            tab.Columns.Add(New DataColumn(col.ColumnName, GetType(String)))
        Next
        Dim i

        For i = 0 To Repeater1.Items.Count - 1
            Dim attControl As CheckBox

            attControl = Repeater1.Items(i).FindControl("closeCase")
            If (attControl.Checked) Then
                Dim sqlText As String = "select key_value from ae_s_lck_e where key_value=Trim('" & attControl.ToolTip.ToString & "')"
                Dim MyOracleConn As New System.Data.OracleClient.OracleConnection("Data Source=FME;Persist Security Info=True;User ID=ROOT;Password=XXXXXXXXX;Unicode=True")
                Dim MyOracleCMD As New System.Data.OracleClient.OracleCommand(sqlText)
                MyOracleCMD.Connection = MyOracleConn
                Dim MyOracleAdapt As New System.Data.OracleClient.OracleDataAdapter(MyOracleCMD)
                Dim MyOracleDS As New System.Data.DataSet()
                MyOracleAdapt.Fill(MyOracleDS)
                If MyOracleDS.Tables(0).Rows.Count = 0 Then
                    Dim sqlText6 = "Update .."
                    Dim MyOracleConn6 As New System.Data.OracleClient.OracleConnection("Data Source=DB;Persist Security Info=True;User ID=ROOT;Password=PWD;Unicode=True")

                    Dim MyOracleCMD6 As New System.Data.OracleClient.OracleCommand(sqlText6, MyOracleConn6)
                    MyOracleConn6.Open()
                    MyOracleCMD6.ExecuteNonQuery()
                    MyOracleConn6.Close()

                    Dim sqlText7 = "SQL.."
                    Dim MyOracleConn7 As New System.Data.OracleClient.OracleConnection("Data Source=DB;Persist Security Info=True;User ID=ROOT;Password=PWD;Unicode=True")

                    Dim MyOracleCMD7 As New System.Data.OracleClient.OracleCommand(sqlText7, MyOracleConn7)
                    MyOracleConn7.Open()
                    MyOracleCMD7.ExecuteNonQuery()
                    MyOracleConn7.Close()

                    tab.Rows.Add(ds.Tables(0).Rows(i).ItemArray)

                Else
                End If
            End If
        Next

        GridView1.DataSource = updDs
        GridView1.DataBind()

        Session("updateRows") = updDs
        Session("updateRows1") = Session("updateRows")
        Response.Redirect("Report.aspx?shop=" & Session("shop"))

    End Sub
0
 

Author Comment

by:mrong
Comment Utility
Thanks MASQUERAID !
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
I think I found out what caused the problem. On my repeater I always sort the data before click on "update", therefore order of the row is different once it is sorted.
Is it possible to add repeater items to the dataset? Something similiar like below?
 tab.Rows.Add(Repeater1.Items(i).DataItem)

Thanks.

0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

I would never have found that out with your posts ;)

you should be able to do this, something like this

Dim data as DataRowView = CType(DataRowView, Repeater1.Items(i).DataItem)

tab.Rows.Add(data.ItemArray)


but it could be, that daatitem is null at this point.
if so you will have to store the index of each row in the dataset in the items of your repeater (with a hidden field or something..)
and then use the stored index to get the right data from the dataset
0
 

Author Comment

by:mrong
Comment Utility
Thalox,
I am checking on the syntax, it game me errors.
0
 

Author Comment

by:mrong
Comment Utility
I tried the following but doesn't work....
Dim Sdata As DataRowView = CType( Repeater1.Items(i).DataItem,DataRowView)
                    tab.Rows.Add(Sdata.Row.ItemArray)
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
what errors do you get?
if it is a nullreference-exception then probably the dataitem is null/nothing
0
 

Author Comment

by:mrong
Comment Utility
The error message is "System.NullReferenceException: Object reference not set to an instance of an object" I thought I always select rows from the previous window...how can the datatiem be null/nothing?

Thanks.
0
 

Author Comment

by:mrong
Comment Utility
How to store the index in the repeater and use it to get the right data from the dataset? Thanks.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility
the dataitem is only set in the databound event.

to find the right row in the dataset there are several methods.

I prefer to use the primarykey of the table your rows are from and store the value for each row in a hidden field in your repeateritem.
then you could do whatever you want with the rows (sort, ..) and when clicked on update get the value of the hidden field (the same way as the checked of the checkbox) and select the row from your dataset

dim rows as datarow()
set rows = ds.Tables(0).Select("id = '" & <ID_FROM_FIELD> & "'")

the right row should be rows(0)

0
 

Author Comment

by:mrong
Comment Utility
what if I use more than one filed as ID? Thanks.
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

it doesnt matter, what you use as id. you have just to make sure, that the select statement will only return one (the right) row.
you could use more than one hidden field or combine all id field in one string
e.g    hiddenfield.value = id1 & "#" & id2 & "#" id3 ...
and then split it for the select statement
ids as string()
ids = hiddenfield.value.split('#')
ds.Tables(0).Select("id1 = '" & ids(0) & "' and id2 = '" & ids(1) & "'" ...)


0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Crystal Report for VS2013 17 36
Hovering effect 9 28
c# regex: extract & replace text between braces 6 28
getting id from database 5 24
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

14 Experts available now in Live!

Get 1:1 Help Now