Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access controls within a ListView control? 1

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hi,

I'm trying out the ListView control, and I'm having difficulty accessing the controls within it from my code. For example, if I have the following:

Code:
		<asp:ListView ID="lvwMyMemos" runat="server" DataMember="DefaultView" DataSourceID="sqlMyMemos">
			<LayoutTemplate>
				<table border="1" width="100%">
...
						<tr>
							<td colspan="6"><asp:Button ID="btnDelete" runat="server" Text="Delete" /></td>
						</tr>
			</LayoutTemplate>
			<ItemTemplate>
				<tr>
					<td align="center"><input id="chkDelete" type="checkbox" value="<%# Eval("MemoID") %>" /></td>
...
			</ItemTemplate>
...
		</asp:ListView>
...

I get an error (CS0103: The name 'btnDelete' does not exist in the current context) when I try to do the following:

Code:
 	protected void Page_Load(object sender, EventArgs e)
 	{
 		btnDelete.Click += new EventHandler(btnDelete_Click);

I need to be able to code an event for the "Delete" button that is embedded within the ListView control's table, and I would also like to be able to access the chkDelete check boxes from within my code.

Essentially, I want to be able to tell which check boxes were checked when the "Delete" button is clicked.

At the moment, I can't figure out how to access these controls via code. If it matters, I'm using Visual Web Developer 2008 Express Edition.

TIA
 
You have to use FindControl to get a reference to the control.

something like:
Code:
{ 
    Button btn = new Button(); 
    btn = (Button)this.lvwMyMemos.FindControl("btnDelete"); 
}
 
Hmmm...

Unfortunately, I get the following error:
Code:
 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 24: 		Button b = (Button)lvwMyMemos.FindControl("btnDelete");
Line 25: 		b.Click += new EventHandler(btnDelete_Click);

Also, assuming that I get the above working, can FindControl() return an array of the chkDelete check box controls so I can determine which ones are checked in my btnDelete_Click() method?

Thanks
 
yes that fails because you are looking the aggregate object. you need to look in the specific listitem.

in the create event of listitem it would look something like
Code:
protected void dosomething(object sender, createdlistitemeventargs e)
{
    Button delete = e.Item.FindControl("btnDelete") as button;
    if(delete != null)
    {
       delete.Click += DoSomethingElse;
    }
}
if you need to cycle through each item then you need to iterate over listview items
Code:
foreach(ListViewItem item in MyListView.Items)
{
   item.FindControl("") as Whatever;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the help. I ended up setting the OnCommand property of the asp:Button object, rather than adding the Click method via code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top