Solved

How do I reference and code events for a control inside a nested ListView?

Posted on 2010-11-10
10
618 Views
Last Modified: 2013-11-27
I am creating a quick database application using VWD 2010 Express and a simple architecture of bound controls on AJAX update panels hooked up to SQL Server Stored Procedures.  Hence no abstraction layers, business object classes etc.

I have successfully got three levels of nested ListView reflecting a heirarchy of Organisations, their Offices and People who work in each office.  The general structure used is as the simplified code below.  At the innermost level, I want to have bound checkboxes that will call Stored Procedures to carry out updates and which will need the DataKeyID from SqlDataSource3 as a parameter.  Each of 3 checkboxes will call a different SPROC.

My problem is that the visual development interface does not fully register/recognise the nested controls that are not visible in the design view,eg I can insert a checkbox control in source view, and hook it up to a field in my data source, but I cannot work out how to get event prodedures working.  I have unsuccessfully tried typing them in manually, copying/pasting procedures at the top level and editing the control names, or placing a control at the top (visible) level and then cutting and pasting it deeper into the nested structure in source view.  The latter worked for the SqlDataSources, but no luck with checkboxes.  Have also tried referencing using ListView1$ListView2, but no better.

Is there something simple I'm missing, or will I have to bite the bullet and learn how to instantiate these controls in code?  If the latter, under what page cycle events do I do this?
<asp:Listview1 DataSouceID=”SQLDataSource1” DataKeyNames=”KeyFieldID1”>

	<ItemTemplate>

		<asp:Button CommandName=”Select” />

		<%-- render fields from SQLDataSource1 --%>

	</ItemTemplate>

	<SelectedItemTemplate>

		<%-- render fields from SQLDataSource1 --%>

		<asp:Listview2 DataSouceID=”SQLDataSource2” DataKeyNames=”KeyFieldID2”>

			<ItemTemplate>

				<asp:Button CommandName=”Select” />

				<%-- render fields from SQLDataSource2 --%>

			</ItemTemplate>

			<SelectedItemTemplate>

				<%-- render fields from SQLDataSource2 --%>

				<asp:Listview3 DataSouceID=”SQLDataSource3” DataKeyNames=”KeyFieldID3”>

					<ItemTemplate>

						<asp:Button CommandName=”Select” />

						<%-- render fields from SQLDataSource3 --%>

					</ItemTemplate>

					<SelectedItemTemplate>

						<%-- render fields from SQLDataSource3 --%>

						<asp:CheckBox ID=”GetsACard” Checked=’<%- Eval(“GetsACardColumn”) %> />

					</SelectedItemTemplate>

				</asp:Listview3>

				<asp:SqlDataSource3>

					<SelectParameters>

						<asp:ControlParameter ControlID="ListView2" Name="KeyFieldID2" />

					</SelectParameters>

				</asp:SqlDataSource3>

			</SelectedItemTemplate>

		</asp:Listview2>

		<asp:SqlDataSource2>

			<SelectParameters>

				<asp:ControlParameter ControlID="ListView1" Name="KeyFieldID1" />

			</SelectParameters>

		</asp:SqlDataSource2>

	</SelectedItemTemplate>

</asp:Listview1>

<asp:SqlDataSource1 / >

Open in new window

0
Comment
Question by:IoTecto
  • 5
  • 5
10 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 34104551
are you using VB.NET or C#? What event you are interested in? Do you mean CheckChanged or ???
0
 

Author Comment

by:IoTecto
ID: 34105087
Using VB.NET.  I need to react to CheckedChanged by calling the SPROC using

mySqlDataSource.Select(DataSourceSelectArguments.Empty)

So a related issue is that when I configure the mySqlDataSource for the SPROC that does the update, I need to see the nested control in the list of available controls to assign to the parameter.  Currently the nested controls don't appear in the configure wizard.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34113466
can you try this (bear with my incompetence with VB.NET).
1: Set AutoEventWireUp="true" for Page attribute at top of your .aspx page
2: Add manually OnCheckChanged="GetsACard_CheckChanged" to your Checkbox e.g.

<asp:CheckBox ID=”GetsACard” Checked=’<%- Eval(“GetsACardColumn”) %>' OnCheckChanged="GetsACard_CheckChanged"  />

3: In the code-behind manually add handler like:

Protected Sub GetsACard_CheckChanged(sender As Object, e As EventArgs)
 ''try your code here and see if it works.
End Sub

0
 

Author Comment

by:IoTecto
ID: 34113882
OK - have tried that thanks.  The actual code is below (control names have changed since the earlier example, but principles the same).  I get a build error: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types".

Have also tried it with the Handles clause saying Handles chkUSB.OnCheckedChanged, with same outcome.  If I comment out the Handles clause, I avoid a build error, but the procedure is never called - I have put a break on the lblTest.Text... line but no break happens.


<asp:CheckBox ID="chkUSB" runat="server" Checked='<%# Eval("USB") %>' Text="  USB stick" OnCheckedChanged="chkUSB_CheckedChanged" />



Protected Sub chkUSB_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkUSB.CheckedChanged

        lblTest.Text = CStr(DateAndTime.TimeOfDay)

End Sub

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34114079
-->but the procedure is never called - I have put a break on the lblTest.Text... line but no break happens.
Can you set AutoPostBack="true" for checkbox....
I doubt that's going to help but try ...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:IoTecto
ID: 34114145
Some progress! - I actually tried this while you were replying.  If I comment out the Handles clause in the procedure declaration AND add the AutoPostBack="true", the event procedure fires (although I get an old-fashioned refresh flash of the screen - presumably this is no longer an AJAX transaction with the server?).

The second part of the problem is how to get the ID value of the item whose checkbox has been changed - With buttons I can use the ItemCommand event and CommandArgument to send back Eval("PositionID") (the column I want), but I don't see an equivalent for a checkbox.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 125 total points
ID: 34114201
Try this:
 Dim chb As CheckBox = DirectCast(sender, CheckBox)
        Dim gvr As GridViewRow = DirectCast(chb.NamingContainer, GridViewRow)
        ' now you have the row reference and you can get cell values like
        'gvr.Cells(2).Text
 ' or get the value out of gridview datakeys using gvr.RowIndex
0
 

Author Comment

by:IoTecto
ID: 34114670
Got there in the end!!  I had to do some work to find the ListView equivalent of your example (see code), but very grateful for your help - I would have taken another 6 weeks without it!  Will be clicking accept as solution after posting this...
Protected Sub chkUSB_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chk As CheckBox = DirectCast(sender, CheckBox)

        Dim lvwItem As ListViewItem = DirectCast(chk.NamingContainer, ListViewItem)

        Dim lvw As ListView = DirectCast(lvwItem.NamingContainer, ListView)

        Dim lvwItemIndex As Integer = lvwItem.DataItemIndex

        Dim IDwanted As Int64 = lvw.DataKeys(lvwItemIndex).Value

        lblTest.Text = Convert.ToString(IDwanted) & " = " & Convert.ToString(chk.Checked)

    End Sub

Open in new window

0
 

Author Closing Comment

by:IoTecto
ID: 34114687
See the whole conversation to follow what was being done here and the final solution implemented for a ListView.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34114777
How did I forget the main question involves Listview and not GridView... :-) but glad you got it working.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

13 Experts available now in Live!

Get 1:1 Help Now