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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Checkboxes and checking if they've been checked/unchecked...

Status
Not open for further replies.

sspeedy00

Programmer
Dec 3, 2007
12
US
Hello Everyone,

I have a datagrid that pulls a query from the database, which consists of goals and each goal has an id # in the database.

So, the page displays something like this:
[checkbox] Goal1
[checkbox] Goal2
[checkbox] Goal3

And an "Update" button.

So, let's say I check Goal1 and Goal3 and then click on Update.. I need to be able to keep 'track of' the Goals using the checkboxes.

Right now, I have the datagrid populating fine with the goals and it displays a checkbox option by each one. My code needs to be able to recognize when an option is selected (or unselected), then go into the db and run a query.

Here's a function from the regular .aspx.cs page:
Code:
private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			System.Web.UI.WebControls.CheckBox cb = (CheckBox)sender;
			DataGrid dg = (DataGrid)cb.NamingContainer;
			dgi.FindControl("cb"). ??

		}

I haven't worked with checkboxes and I don't mean for the code to be confusing. First, I need to run a loop, something like 'while dgi exists, do { } '. Second, I'm not sure how to refer to a particular checkbox since... I've declared only one:
Code:
<ItemTemplate>
				<asp:CheckBox id="cb" runat="server" OnCheckedChanged="Check_Clicked" ></asp:CheckBox>
			</ItemTemplate>
but it repeats for every goal (if that makes sense).

Any help will be appreciated. TIA.



 
I've edited the function to this:
Code:
		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			//DataGrid dg;
			//DataGridItem dgi;
			foreach(DataGridItem dgi in dgDataGrid.Items)
			

				chkbx = ( System.Web.UI.WebControls.CheckBox )dgi.FindControl("cb");

			if( chkbx != null && chkbx.Checked )
				Response.Write("[URL unfurl="true"]www.yes.com");[/URL]
			else
				Response.Write("[URL unfurl="true"]www.no.com");[/URL]
				
		
		}
But the problem is that dgDataGrid (in foreach line) is declared in the Controls\file and not the one I'm referring it in. I keep getting an error. I've tried this:
using Name.Controls.ControlName;
only to get this error:
Code:
A using namespace directive can only be applied to namespaces; 'Name.Controls.ControlName' is a class not a namespace.

Any ideas how I can use dgDataGrid object from the Control file within this current file?
 
Since the problem keeps evolving as I try to get through this, here's a small update. Now my problem seems to be sharing the datagrid variable. It is defined like this in Controls\File.aspx.cs:
Code:
public System.Web.UI.WebControls.DataGrid dgDataGrid;

Now in the regular OtherFile.aspx.cs, I need to use dgDataGrid in a for loop like this:
Code:
foreach(DataGridItem dgi in dgDataGrid.Items)

				chkbx = ( System.Web.UI.WebControls.CheckBox )dgi.FindControl("cb");

When I compile, I keep getting an error:
"The type or namespace name 'dgDataGrid' could not be found (are you missing a using directive or an assembly reference?)"

Here's what I have tried:
1. using FileName.Controls (at the very top).
2. declaring dgDataGrid as a static member.
3. instead of just dgDataGrid, refer to it as "System.Web.UI.WebControls.DataGrid.dgDataGrid" (and when I do this, I get the error "System.Web.UI.WebControls.DataGrid" does not contain a definition for 'dgDataGrid'.

I'm sure the solution is simple and obvious, but I'm stuck and I'd appreciate any kind of help. tia.


 
k, for the last part, you have to refer to the datagrid variable through the instance of the File.aspx.cs.

So it would be something like


Msgbox(File.dgDatagrid.rowcount)

remember, everything belongs to something, you just have to figure out what it belongs to.

-Sometimes the answer to your question is the hack that works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top