 |
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 23rd, 2008, 06:17 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dynamic control onclick event
I wrote the code below to populate a table with all open service requests from a database. If there are 2 open requests only two table rows are generated; if there are 20 open requests 20 rows are generated. That part is working perfectly fine. What is supposed to happen is when the user clicks on one of the images (imgStatus(i)) another window opens with the details of that request. Currently, the new window opens, but only the last item in the table is ever displayed, regardless of which image is clicked.
The onclientclick event will not capture the ID of the image that was clicked. Does anyone know how I can make the code recognize the clicked image and pick up its ID so the correct request can be displayed in the new window?
Thanks,
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\TSR.mdf;Trusted_Connection=True;User Instance=True")
Dim SearchTSR As String = "SELECT TSRNumber, Request, Comments, ActionDTG, LastUser, Status FROM TSR WHERE Status BETWEEN 0 AND 4"
Dim daSearchTSR As New SqlDataAdapter(SearchTSR, conn)
Dim dsSearchTSR As New DataSet
'Create a dataset for the matching entry
daSearchTSR.Fill(dsSearchTSR, "dtSearchTSR")
Dim totalrows As Integer = dsSearchTSR.Tables(0).Rows.Count
'MsgBox(totalrows)
'Exit Sub
Dim imgStatus(0 To totalrows) As ImageButton
Dim txtTsrNumber(0 To totalrows) As TextBox
Dim tr(0 To totalrows) As TableRow
'Display open TSRs
If dsSearchTSR.Tables(0).Rows.Count = 0 Then
MsgBox("There are no open TSRs at this time.", , "Technical Services")
Response.Write("<script language='javascript'>")
Response.Write("self.close()")
Response.Write("</script>")
Else
Dim row1 As DataRow
Dim i As Integer = 0
For Each row1 In dsSearchTSR.Tables("dtSearchTSR").Rows
tr(i) = New TableRow()
Dim td1 As TableCell = New TableCell() 'Creates column 1
td1.BorderStyle = BorderStyle.Solid
td1.BorderWidth = 2
td1.HorizontalAlign = HorizontalAlign.Center
td1.VerticalAlign = VerticalAlign.Middle
td1.Width = 150
imgStatus(i) = New ImageButton()
imgStatus(i).ID = "ImgStatus" & i
imgStatus(i).CssClass = "panel control"
imgStatus(i).ImageAlign = ImageAlign.Middle
imgStatus(i).BorderStyle = BorderStyle.Solid
imgStatus(i).BorderWidth = 1
imgStatus(i).BorderColor = System.Drawing.ColorTranslator.FromHtml("#b3aaa1")
imgStatus(i).Width = 100
imgStatus(i).OnClientClick = "javascript:window.open('Review.aspx');"
Dim lblStatus As Label = New Label()
lblStatus.ID = "lblStatus" & i
lblStatus.BorderStyle = BorderStyle.Solid
lblStatus.BorderWidth = 1
lblStatus.BorderColor = System.Drawing.ColorTranslator.FromHtml("#b3aaa1")
lblStatus.CssClass = "table status label"
lblStatus.Font.Bold = True
Select Case row1("Status")
Case 0
imgStatus(i).ImageUrl = "~\Images\zero.bmp"
lblStatus.Text = "Not Yet Actioned"
Case 1
imgStatus(i).ImageUrl = "~\Images\25done.bmp"
lblStatus.Text = "Sent to SE"
Case 2
imgStatus(i).ImageUrl = "~\Images\50done.bmp"
lblStatus.Text = "Working"
Case 3
imgStatus(i).ImageUrl = "~\Images\50done.bmp"
lblStatus.Text = "Awaiting Info"
Case 4
imgStatus(i).ImageUrl = "~\Images\75done.bmp"
lblStatus.Text = "Dispositioned"
End Select
Dim td2 As TableCell = New TableCell 'Creates column 2
td2.BorderStyle = BorderStyle.Solid
td2.BorderWidth = 2
td2.HorizontalAlign = HorizontalAlign.Left
td2.VerticalAlign = VerticalAlign.Top
td2.Width = 650
Dim lblTsrNumber As Label = New Label
lblTsrNumber.ID = "lblTsrNumber" & i
lblTsrNumber.Width = 70
lblTsrNumber.CssClass = "table top label"
lblTsrNumber.Text = "TSR No:"
txtTsrNumber(i) = New TextBox
txtTsrNumber(i).ID = "txtTsrNumber" & i
txtTsrNumber(i).Width = 100
txtTsrNumber(i).CssClass = "table top control"
txtTsrNumber(i).ReadOnly = True
txtTsrNumber(i).Text = row1("TSRNumber")
Session.Add("TSRNumber", txtTsrNumber(i).Text)
imgStatus(i).ToolTip = txtTsrNumber(i).Text
Dim lblLastAction As Label = New Label
lblLastAction.ID = "lblLastAction" & i
lblLastAction.Width = 100
lblLastAction.CssClass = "table top label"
lblLastAction.Text = "Last Actioned:"
Dim txtLastAction As TextBox = New TextBox
txtLastAction.ID = "txtLastAction" & i
txtLastAction.Width = 125
txtLastAction.CssClass = "table top control"
txtLastAction.ReadOnly = True
txtLastAction.Text = row1("ActionDTG")
Dim lblBy As Label = New Label
lblBy.ID = "lblBy" & i
lblBy.Width = 25
lblBy.CssClass = "table top label"
lblBy.Text = "by:"
Dim txtLastPerson As TextBox = New TextBox
txtLastPerson.ID = "txtLastPerson" & i
txtLastPerson.Width = 150
txtLastPerson.CssClass = "table top control"
txtLastPerson.ReadOnly = True
'txtLastPerson.Text = row1("LastUser")
Dim lblRequest As Label = New Label
lblRequest.ID = "lblRequest" & i
lblRequest.Width = 70
lblRequest.CssClass = "label request"
lblRequest.Text = "Request:"
Dim txtRequest As TextBox = New TextBox
txtRequest.ID = "txtRequest" & i
txtRequest.Width = 530
txtRequest.TextMode = TextBoxMode.MultiLine
txtRequest.CssClass = "panel control"
txtRequest.ReadOnly = True
txtRequest.Text = row1("Request")
Dim lblComments As Label = New Label
lblComments.ID = "lblComments" & i
lblComments.Width = 70
lblComments.CssClass = "label comments"
lblComments.Text = "Comments:"
Dim txtComments As TextBox = New TextBox
txtComments.ID = "txtComments" & i
txtComments.Width = 530
txtComments.Height = 80
txtComments.TextMode = TextBoxMode.MultiLine
txtComments.CssClass = "panel control"
txtComments.ReadOnly = True
If IsDBNull(row1("Comments")) Then
txtComments.Text = ""
Else
txtComments.Text = row1("Comments")
End If
td1.Controls.Add(imgStatus(i)) 'These two lines define the fields in column 1
td1.Controls.Add(lblStatus)
td2.Controls.Add(lblTsrNumber) 'These lines define the fields in column 2
td2.Controls.Add(txtTsrNumber(i))
td2.Controls.Add(lblLastAction)
td2.Controls.Add(txtLastAction)
td2.Controls.Add(lblBy)
td2.Controls.Add(txtLastPerson)
td2.Controls.Add(lblRequest)
td2.Controls.Add(txtRequest)
td2.Controls.Add(lblComments)
td2.Controls.Add(txtComments)
tr(i).Cells.Add(td1) 'These lines add the columns to the row, then the row to the table
tr(i).Cells.Add(td2)
Table1.Rows.Add(tr(i))
i = i + 1
Next
End If
|

August 24th, 2008, 03:53 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Why do you need imgStatus as an array of ImageButton controls? Why not just declare a single instance of it inside your loop, just as you do with your other controls? Inside the loop you only seem to use imgStatus(i) and I don't see imgStatus being used elsewhere.
Also, based on what do you think a ImageButton will behave differently for each row? All I see is this:
imgStatus(i).OnClientClick = "javascript :window.open('Review.aspx');"
Which means that each button simply opens Review.aspx with no additional state. Maybe you need to pass a query string parameter to Review.aspx?
HtH,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

August 24th, 2008, 10:06 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Thanks for the response. I initially tried setting up the control the same as the others (without the '(i)'), but I got the same results. The version I posted was the second attempt at resolving this problem.
I have figured out when the img button is clicked the loop is performed the same number of times as there are open requests in the database. I guess that is what would be expected.
I looked at the view source to see if the buttons are being given unique identities and they are, but I just can't seem to capture the identity of the particular img button being pressed. I need that identity to retrieve the correct open request from the database for the fields in the window that is opened on the 'onclientclick' event.
If I move the onclientclick event outside of the loop I lose access to the control altogether. I'm guessing that is because it is not statically declared and is being created as the page loads? I've also tried to use session values to no avail.
I'm sure the solution is simple; it just escapes me.
Chris
|

August 25th, 2008, 04:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
But what is it that you want to do? Postback to the same page or open a new window client side?
In the former case, you need to recreate the dynamic controls on Postback and wire up a Click handler. In the latter case, you need to send info to the requested URL.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

August 25th, 2008, 01:55 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I want to be able to open a new window (page2) with information related to the specific img button in the table on page1 that was clicked. Right now, the onclientclick event only lets me get information related to the last img button in the page1 table. For example, if page1 creates a table with five rows and the user clicks the img button in row3, page2 should display information related to page1 row3. That's the part that doesn't work; getting the page1 to recognize which row's img button was clicked and showing the appropriate information in page2.
Does that make any more sense?
Chris
|

August 25th, 2008, 02:19 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Not really. All I really see is this:
imgStatus(i).OnClientClick = "javascript :window.open('Review.aspx');"
Basically this says: let each button, regardless of its location, open the page Review.aspx (I assume that's the "page 2" you're talking about.
I don't see any code that tries to determine what button was clicked. That is, I don't see a Click handler, I don't see you recreate controls on post back, I don't see you use CommandName and CommandArgument properties and so on, so all I can assume is that you want to open Review.aspx with the ID if the button. In that case, this should work:
imgStatus(i).OnClientClick = String.Format("javascript :window.open('Review.aspx ?Id={0}');", i.ToString())
If that's not what you want, please be more explicit. Try to avoid implicit terms as Page 2, but try to describe stuff like PostBack on the same page versus browsing client side to a new page like Review.aspx. Please add as much relevant data as you see fit.
What I also don't see is what you mean with "lets me get information related to the last img button in the page1 table." How do you know? What enables you to determine what the last button is? Don't all your buttons do exactly the same? (Open Review.aspx)? And how do you uniquely identify a row / button? TSRNumber seems like a good candidate for a primary key, but you don't pass it in the query string to the next page.
Maybe the answer lies in this:
Session.Add("TSRNumber", txtTsrNumber(i).Text)
Are you, on page two, getting data based on Session("TSRNumber")?
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

August 26th, 2008, 08:20 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks, Imar
This line did the trick with one change:
imgStatus(i).OnClientClick = String.Format("javascript:window.open('Review.aspx ?Id={0}');", row1("TSRNumber"))
I don't know why, but it works and that's the key.
I was able to use page.request("Id") in the database query on the 'Review.aspx' page to correctly retrieve the record from the database.
Chris
|

August 27th, 2008, 11:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
quote:I don't know why, but it works and that's the key.
|
It's important to understand the why, in order to do something like this in the future again.
The idea apparently was to pass the ID of the record through the query string. I suggested that, but you never confirmed. Here's what happens:
imgStatus(i).OnClientClick = String.Format("window.open('Review.aspx?Id={0}');" , row1("TSRNumber"))
When this code is run, you end up with something like:
<img src="onclick=window.open('Review.aspx?Id=12345');" ... />
When you click the image, you are taken to:
Review.aspx?Id=12345
On that page, Request.QueryString("Id") gives you the ID of the selected record.
BTW: you don't need the javascript : prefix, and AFAIK, you don't need the array of ImageButton controls either.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Onclick Post event |
sabu21s |
Classic ASP Databases |
0 |
October 31st, 2005 05:43 PM |
onclick event |
bjackman |
Access |
6 |
July 15th, 2004 06:54 AM |
onclick event |
pigtail |
Javascript |
1 |
April 11th, 2004 03:10 PM |
onClick Event |
mateenmohd |
Javascript |
4 |
December 16th, 2003 01:08 AM |
|
 |