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

Reading control in a nested datagrid

Status
Not open for further replies.

Menthos

IS-IT--Management
Apr 14, 2003
24
0
0
GB
Ok, I've got a parent/child datasource setup and bound to a datagrid with a nested datagrid within to show hierarchal info. For each row of the nested datagrid shown, I have a DropDownList with 3 options.

What I want to be able to do is click a submit button and read through each of the dropdownlists and grab the selected value.

The submit code currently looks like this:
Code:
Private Sub Submit_changes(source As Object, e As EventArgs)
	Dim griditem as datagriditem
	dim txtString as String
	DIM DropStr as String 
	Dim childgrid as datagrid
	DIM ddl as DropDownList

	childgrid = CType(DataGrid1.FindControl("datagrid2"), Datagrid)
	txtString="Test"
	If not(childgrid is nothing) then
		For Each griditem in childgrid.Items
		ddl = CType(griditem.FindControl("drop1"), Dropdownlist)
		DropStr=cStr(ddl.selecteditem.value)
		txtString = txtString & DropStr
		Next
	end if
	Textbox1.text=txtString
End Sub

Apologies for the noobish state, I'm new to ASP.NET.

As far as I can see, it should add the values of all the DropDownLists (drop1) to the string txtString then dump it in the textbox, but it keeps coming back blank.

Anyone got any ideas??

Cheers,
Menthos
 
OK, found out where I was going wrong, I had to cycle through the parent datagrid to get all the child datagrid controls... the code changes are as follows:

Code:
Private Sub Submit_changes(source As Object, e As EventArgs)
	Dim ParentGriditem as datagriditem
	dim ChildGridItem as datagriditem
	dim txtString as String
	DIM DropStr as String 
	Dim childgrid as datagrid
	DIM ddl as DropDownList

	FOR Each ParentGridItem in DataGrid1.items
		childgrid = CType(ParentGridItem.FindControl("datagrid2"), Datagrid)
		IF NOT(childgrid is nothing) THEN
			FOR EACH ChildGridItem in childgrid.Items
				DropStr = CType(ChildGridItem.FindControl("drop1"), Dropdownlist).selecteditem.value
				txtString = txtString & DropStr
			NEXT
		END IF
	NEXT
	Textbox1.text=txtString
End Sub
 
OK, I've run into another problem :)

The desired outcome of this datagrid setup is to be able to use the DDL on each row to set the access level for a corresponding forum (the rows show forum names).

The child datagrid's rows have a hidden unique ID column which I use to reference an array to get the corresponding value for the DDL's for each row. I'm then setting those DDL's to display the value from the array... all is fine there.

I have a 'submit' button which calls a sub to cycle through the datagrid's and return the selected value from the DDLs. The problem is that when the subit button is clicked, it seems to read only the default values of the DDL controls, and not the values that they've been changed to.

My sub looks like this:

Code:
Private Sub Submit_changes(source As Object, e As EventArgs)
	Dim ParentGriditem as datagriditem
	dim ChildGridItem as datagriditem
	dim txtString as String
	DIM DropStr as String 
	Dim childgrid as datagrid
	DIM ForumID as Integer

	FOR Each ParentGridItem in DataGrid1.items
		childgrid = CType(ParentGridItem.FindControl("datagrid2"), Datagrid)
		IF NOT(childgrid is nothing) THEN
			FOR EACH ChildGridItem in childgrid.Items
				ForumID = ChildGridItem.Cells(0).Text
				DropStr = CType(ChildGridItem.FindControl("drop1"), Dropdownlist).selectedvalue
				txtString = txtString & cstr(ForumID) & DropStr & " "
			NEXT
		END IF
	NEXT
	Textbox1.text=txtString
End Sub

I'm dumping the debug string txtString out to see what it's getting... I'll be processing that back into the array when I know it's working.

Any of you gurus got any ideas?

Cheers,
Menthos
 
It sounds like the drop down lists are being reset on the page load. Do you check if the page is a postback on the page load?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I wasn't checking for that, however I set the default values for the DDL's in the main page load, and if I check for postback and don't set them they reset to the first item in the list.

Does that make sense?
 
Hmmm - Are you saying they reset to the first item, even if a user has selected a different item? You may have to post some of your code so we can see where you may be going wrong.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yep, exactly that. Here's the datagrid code:

Code:
<html>
	<body>
		<form id="form1" runat="server">
			<asp:TextBox id="Textbox1" text="Test textbox" runat="server" />
			<asp:Button id="Submit" onclick="Submit_changes" text="Submit" runat="server" />

		   <asp:DataGrid id="Datagrid1" runat="server" AutoGenerateColumns="False" ShowHeader="false" GridLines="None">
		   		<columns>
					<asp:TemplateColumn>
						<itemtemplate>
							<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
								<TR>
    								<TD bgColor="#999966">
										<B><%# DataBinder.Eval(Container.DataItem, "Name") %></B>
      								</TD>
								</TR>
								<TR>
    								<TD>
										<asp:DataGrid id="DataGrid2" runat="server" AutoGenerateColumns="False" BorderColor="#33FF33" DataSource='<%# CType(Container.DataItem,DataRowView).CreateChildView("TestRelationDS") %>' ShowHeader="false" GridLines="None">
											<Columns>                          						
  												<asp:BoundColumn Visible="False" DataField="ID" ReadOnly="True"></asp:BoundColumn>
  												<asp:TemplateColumn ItemStyle-Width="300px">  													
													<ItemTemplate>
														<%# DataBinder.Eval(Container.DataItem, "Name") %>														
													</ItemTemplate>
												</asp:TemplateColumn>
												<asp:TemplateColumn>  													
													<ItemTemplate>
														<asp:DropDownList id="drop1" runat="server">
															<asp:ListItem value="NA">No Access</asp:ListItem>
															<asp:ListItem value="RO">Read Only</asp:ListItem>
															<asp:ListItem value="FA">Full Access</asp:ListItem>
														</asp:DropDownList>																	
													</ItemTemplate>
												</asp:TemplateColumn>																				
											</Columns>
										</asp:DataGrid>
									</TD>									
								</TR>
							</TABLE>
						</ItemTemplate>
					</asp:TemplateColumn>
				</Columns>
			</asp:DataGrid>
		</form>
	</body>
</html>

(my indenting will probably go bandy... sorry about that :) )

I did try EnableViewState="true" for the DDL's, but that doesn't seem to help.
 
OK - can you also post your code for your page load and how you bind the data?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
OK, here goes... apologies for the sloppy code :)

Code:
<%@ Page Language ="VB" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ODBC" %>

<script  runat="server">

Sub Page_Load(Source as Object, E as EventArgs)


'DB connect goes here
	

	
	Dim objDataSet As New DataSet()
	'Connect to database
	objConn.Open()
	objComm.CommandText = "SELECT ID, Name, IntOrder FROM groupdb WHERE active=1 ORDER BY IntOrder"
	objDataAdapter.Fill(objDataSet,"Group")

	objComm.CommandText = "SELECT ID, Name, GroupID, IntOrder FROM forumdb WHERE active=1 ORDER BY GroupID, ID"
	objDataAdapter.Fill(objDataSet,"Forum")
	objConn.Close()
	
	DIM RelationDS as DataRelation
	RelationDS = new DataRelation("TestRelationDS", ObjDataSet.Tables("Group").Columns("ID"), ObjDataSet.Tables("Forum").Columns("GroupID"),False)
	objDataSet.Relations.Add(RelationDS)
	
	Datagrid1.datasource=objdataset
	Datagrid1.databind
	
	IF NOT ispostback THEN
		'grab access string for user
		Dim dsForumAccess As New DataSet()
		DIM txtForumAccess as String
		objConn.Open()
		objComm.CommandText = "SELECT ForumAccess, PostAccess FROM tblForumAccess WHERE UserID=1"
		objDataAdapter.Fill(dsForumAccess,"ForumACS")
		txtForumAccess = dsForumAccess.Tables("ForumACS").rows(0).item("ForumAccess")
		objConn.Close()
		Textbox1.text=LEN(txtForumAccess)
		'transfer to array
		DIM ForumACSArray(Len(txtForumAccess)) as String
		DIM iCount as integer
		FOR iCount=1 to Len(txtForumAccess)
			IF MID(txtForumAccess,iCount,1)="y" THEN ForumACSArray(iCount)="FA" ELSE ForumACSArray(iCount)="NA"
		NEXT
		
		
		Dim ParentGriditem as datagriditem
		dim ChildGridItem as datagriditem
		DIM DropStr as String 
		Dim childgrid as datagrid
		DIM ForumID as Integer
	
		FOR Each ParentGridItem in DataGrid1.items
			childgrid = CType(ParentGridItem.FindControl("datagrid2"), Datagrid)
			IF NOT(childgrid is nothing) THEN
				FOR EACH ChildGridItem in childgrid.Items
					ForumID = Cint(TRIM(ChildGridItem.Cells(0).Text))
					CType(ChildGridItem.FindControl("drop1"), Dropdownlist).selectedvalue = ForumACSArray(ForumID)
				NEXT
			END IF
		NEXT
	END IF

End Sub

Private Sub Submit_changes(source As Object, e As EventArgs)
	Dim ParentGriditem as datagriditem
	dim ChildGridItem as datagriditem
	dim txtString as String
	DIM DropStr as String 
	Dim childgrid as datagrid
	DIM ForumID as Integer

	FOR Each ParentGridItem in DataGrid1.items
		childgrid = CType(ParentGridItem.FindControl("datagrid2"), Datagrid)
		IF NOT(childgrid is nothing) THEN
			FOR EACH ChildGridItem in childgrid.Items
				ForumID = ChildGridItem.Cells(0).Text
				DropStr = CType(ChildGridItem.FindControl("drop1"), Dropdownlist).selectedvalue
				txtString = txtString & cstr(ForumID) & DropStr & " "
			NEXT
		END IF
	NEXT
	Textbox1.text=txtString
End Sub

</script>
 
Ok from what I can see you have a dropdownlist named "drop1" which is part of a datagrid named "DataGrid1".

As the datagrid is bound on the page load, regardless of wether it is a postback e.g.
Code:
Datagrid1.datasource=objdataset
Datagrid1.databind

these dropdownlists will also be rebound hence the reason why they are defaulting to the first item. You only want to bind the grid if the page is not a postback, or you need to get the values before the grid is rebound.



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Looks like that did the trick!

Thanks!!! :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top