Link to home
Start Free TrialLog in
Avatar of coleenholley
coleenholleyFlag for United States of America

asked on

Need to know where to put this in Gridview Order of Events

I have been working on a project for over a month, and since I have had no training on Gridviews, am learning this by the seat of my pants.  I currently have a gridview that has two "linked" dropdownlist boxes - one is visible and is where the user makes a selection which is/needs to be tied into the "Invisible" DDL  - thses two DDL' s really have the same data only one's data is coming from the database at retrieval and is the "Hidden" DDL and the other is tied to a datasource that stores all of our Dropdownlist values for ALL dropdowns used throughout our applications.

the hidden DDl must be changed to have the same "Key" value when the "Visible" DDl is changed/updated.  I actually have that portion of it working, but am at a loss because in my DataBound Event I am changing the value of the "Hidden" DDL to match that of my "Visible" DDL - which would be fine except that on update, since the DataBound event is hit again before the Gridview is displayed, the code is changing my values on the display of the Gridview to be the exact same in ALL rows of the the dropdownlist, not just the row that has been changed.

I Need some help in where/what event to put the code in so that it is hit on the first retrieveal of the data to make the "Visible" DDL match the "Hidden" dDDL, but NOT to change ALL rows of the Visible DDl after a change is made.  I am attaching the code I have that is changing the values on all rows - it resides in my DataBound event:
'This portion of code is to get the values from the dropdownlists inside the grideview 
'(the ddlModeTy) is a "hidden" field used to set/get the key value from the Rt_database 
'for correct display and update purposes. - CH 1/2008.
For liIndex = 0 To liRowNum -1
Dim ddlModeTy as DropDownList = CType(gdv_rcpt_srch.Rows(liIndex).FindControl("ddlModeTy"), DropDownList)
Dim ddlMode2 as DropDownList = CType(gdv_rcpt_srch.Rows(liIndex).FindControl("ddlMode2a"), DropDownList)
     
   If gdv_rcpt_srch.Rows(liIndex) is Nothing then
   'If ctype(gdv_rcpt_srch.Rows(liIndex).FindControl("ddlModeTy"), DropDownList).Text is nothing then   
   lbl_Error.Text = "No rows were retreived from the datasource"
   Else
   'Set the values of the "hidden" dropdownlist to have the same values as the description ddl
   'so that on update the value for the "hidden" ddl can be changed and saved back to the database
      If ddlModeTy.SelectedValue = "BA" then
         ddlMode2.SelectedValue = ddlModeTy.SelectedValue
         ddlModeTy.Items.Remove("BA")
         ddlModeTy.Items.Add("J")
         ddlModeTy.Items.Add("PL")
         ddlModeTy.Items.Add("R")
         ddlModeTy.Items.Add("ST")
      ElseIf ddlModeTy.SelectedValue = "J" then
         ddlMode2.SelectedValue = ddlModeTy.SelectedValue
         ddlModeTy.Items.Remove("J")
         ddlModeTy.Items.Add("BA")
         ddlModeTy.Items.Add("PL")
         ddlModeTy.Items.Add("R")
         ddlModeTy.Items.Add("ST")
     'More code like above to cover each Mode Type - not shown
     Else
     'Reurn error message that data retrieved doesn't match data in dropdown
     lbl_Error.Text = "The data retreived doesn't match the data available in the list"
     End If
   End If
   'Unrem below an make invisible before going into prod
   'gdv_rcpt_srch.Columns(2).Visible = False
Next

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Where is that code being called from?

Bob
Avatar of coleenholley

ASKER

That code is a called on the DataBound event.  The problem is that it is called every time the gridview is bound - so of course it changes my values every time to match the "hidden" column value.  When a user updates it, I have code in the update section that sets the "Hidden" column value to match the value of the selected Item in the Dropdown - but then it goes back and does the DataBound event again and changes it back to what was originally in the "Hidden" column.  I tried moving the code to the RowDataBound event, but I basically have the same problem.  It works perfectly in the RowDataBound event for the first initial load of the Gridview, but then again, everytime the gridview is bound i calls the RowDataBound event and changes it so it does not match the updated values!

What I really need to know is where to put this code so that it is ONLY triggered on the initial binding of the Gridview and then it is NOT called again.  What event/where would I put this code to do that?

Thanks :-)

Coleen
Coleen,

That sounds like you need to check if Page.IsPostBack before processing in the RowDataBound event.

Bob
I do check for Is Postback.  Attached is the entire bit of code - I had abreviated it for posting purposes.  If I put it as If Not Page.IsPostback then it doesn't hit the code that syncs up the "hidden" DDL and the "Visible" DDL and I end up with "Select a Mode" in my "Visible" DDL instead of the correct retrieved data.
Protected Sub gdv_rcpt_srch_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdv_rcpt_srch.RowDataBound
Try
     If Page.IsPostback then
     
	If gdv_rcpt_srch.Rows.Count > 0 Then
	     btnReport.Enabled = False
	End If
	
	'If Repord Period is closed, then disable all the fields.
	If Session("rpt_prd_stat") = "Y" Then
	     btn_Update.Enabled = False
	     btn_Delete.Enabled = False
	     btn_DeleteAll.Enabled = False
	     btn_Cancel.Enabled = False
	     Try
		Dim ls_error_temp As String = ""
		Dim lo_misc_func As New misc_func()
		lo_misc_func.GetErrDescSQL("MF00058", "", ls_error_temp, "")
		lbl_Error.Text = ls_error_temp
		lbl_Error.Visible = True
		'Destroy the objects.
		lo_misc_func = Nothing
	     Catch
		'lbl_error.Text = ls_error_temp
		lbl_Error.Visible = True
	     End Try
	     'The code (see original code) to disable all rows/cells is now done in the ASP.Net 
	End If
		
	Dim liRowNum as Integer = 0
	Dim liIndex as Integer = 0
	liRowNum = gdv_rcpt_srch.Rows.Count
	
         If liRowNum <> -1 then
	     btn_Update.Visible = True
	     btn_Delete.Visible = True
	     If Session("ls_fuelcd") <> "" And Session("ls_sch") <> "" then
	     'If the selection Criteria was fuel code and Schedule type, disable the Deleteall button
	         btn_DeleteAll.Visible = False
	     Else
		btn_DeleteAll.Visible = True	
	     End If
		btn_Cancel.Visible = True
		lbl_Error.Visible = False
		btnSearch.Enabled = False
		btnClear.Enabled = False
		'Destroy the misc_func object
		io_misc_func  = Nothing
		
		'This portion of code is to get the values from the dropdownlists inside the grideview 
		'(the ddlModeTy) is a "hidden" field used to set/get the key value from the Rt_database 
		'for correct display and update purposes. - CH 1/2008.
		For liIndex = 0 To liRowNum -1
		     Dim ddlModeTy as DropDownList = CType(gdv_rcpt_srch.Rows(liIndex).FindControl("ddlModeTy"), DropDownList)
		     Dim ddlMode2 as DropDownList = CType(gdv_rcpt_srch.Rows(liIndex).FindControl("ddlMode2a"), DropDownList)
 
		     If gdv_rcpt_srch.Rows(liIndex) is Nothing then
		     'If ctype(gdv_rcpt_srch.Rows(liIndex).FindControl("ddlModeTy"), DropDownList).Text is nothing then   
			lbl_Error.Text = "No rows were retreived from the datasource"
		      Else
			gdv_rcpt_srch.DataBind
		         'Set the values of the "hidden" dropdownlist to have the same values as the description ddl
			'so that on update the value for the "hidden" ddl can be changed and saved back to the database
			If ddlModeTy.SelectedValue = "BA" then
			     ddlMode2.SelectedValue = ddlModeTy.SelectedValue
			     ddlModeTy.Items.Remove("BA")
			     ddlModeTy.Items.Add("J")
			     ddlModeTy.Items.Add("PL")
			     ddlModeTy.Items.Add("R")
			     ddlModeTy.Items.Add("ST")
			ElseIf ddlModeTy.SelectedValue = "J" then
			     ddlMode2.SelectedValue = ddlModeTy.SelectedValue
			     ddlModeTy.Items.Remove("J")
			     ddlModeTy.Items.Add("BA")
			     ddlModeTy.Items.Add("PL")
			     ddlModeTy.Items.Add("R")
			     ddlModeTy.Items.Add("ST")
			ElseIf ddlModeTy.SelectedValue = "PL" then
			     ddlMode2.SelectedValue = ddlModeTy.SelectedValue
			     ddlModeTy.Items.Remove("PL")
			     ddlModeTy.Items.Add("BA")
			     ddlModeTy.Items.Add("J")
			     ddlModeTy.Items.Add("R")
			     ddlModeTy.Items.Add("ST")
			ElseIf ddlModeTy.SelectedValue.Trim = "R" then
			     ddlMode2.SelectedValue = ddlModeTy.SelectedValue
			     ddlModeTy.Items.Remove("R")
			     ddlModeTy.Items.Add("BA")
			     ddlModeTy.Items.Add("J")
			     ddlModeTy.Items.Add("PL")
			     ddlModeTy.Items.Add("ST")
			ElseIf ddlModeTy.SelectedValue = "ST" then
			     ddlMode2.SelectedValue = ddlModeTy.SelectedValue
			     ddlModeTy.Items.Remove("ST")
			     ddlModeTy.Items.Add("BA")
			     ddlModeTy.Items.Add("J")
			     ddlModeTy.Items.Add("PL")
			     ddlModeTy.Items.Add("R")
			Else
			'Reurn error message that data retrieved doesn't match data in dropdown
			     lbl_Error.Text = "The data retreived doesn't match the data available in the list"
			End If
		    End If    
		    'Unrem below an make invisible before going into prod
		    'gdv_rcpt_srch.Columns(2).Visible = False
		Next
		
	Else
	    gdv_rcpt_srch.Visible = False
	    btn_Update.Visible = False
	    btn_Delete.Visible = False
	    btn_DeleteAll.Visible = False
	    btn_Cancel.Visible = False
	    lbl_Error.Text = "<h3 style='COLOR: red; FONT-FAMILY: Arial'>No records found for the selected criteria.</h3>"
	    lbl_Error.Visible = True
	    'Destroy the misc_func object
	    io_misc_func  = Nothing
	    Exit Sub
	End If
	
    End If
	
Catch ex As Exception
     'Call the fatal error routine.
     fatal_error_handle()
End Try	
	
End Sub

Open in new window

Hi Bob :-)

Okay I have made some changes to what I was doing but am still having some trouble and coul really use some help on figuring out how to get this to work correctly.  Here is what I'm doing and what is happening:

I have a web page that the user makes a selection from a separate DDL which a search is performed and call a RPC (a COBOL program) to get the data from the DB2 database.  There is a field/column (Mode_Type) that comes back from DB2 with just a Key value.  What needs to be displayed for the users is data that comes from a SQL database wherre we store ALL the values for our organizations DDL's.  So, since the two DB's don't have the same field name for the key value, I can't tie the Mode_type value that comes from DB2 into the the key value from our SQL DB.  I created two DDL's inside my Gridview.  The one that is returned from DB2 is "Hidden" and the one that comes from SQL is "Visible" and I tie it into the key value behind the scene using the code I sent in my previous post.  This would work perfectly well, except that when I tie this in using the DataBound event - it always sets both (in my test case I only have two rows) Rows to have the exact same value - even if one is different than the other coming from the DB2 DB.  Clear as mud right?  Okay, so the issue is the sync-up of the two DDL's in the Gridview.  The Hidden one needs to make the "Visible" one have the same Key value on the intitial retrieval of the data.  then, if a user makes a change/updates the value for the Mode_type in the "Visible" DDL, I need to sync the "Hidden" DDL up with the Visible one.  I have that portion working just fine - EXCEPT that if I leave the code for the initial sync-up in the DataBound event - it syncs up incorrectly on the dsiplay (Visible) field, even though it has correctly been changed/updated in the DB2 database.  Still with me?

Now what I did was to move my code into the RowDataBound event, which seemed to work great on the initial display/load of the gridview, but was being hit every time and there fore not displaying the correct values from the DB2 database after an update.  So I then moved my code into the Pre-Render event and call a separate function to do the sync.  Which works - sort of.  On the initial load/display of the data it now correctly shows one of the rows, but doesn't sync up both rows (even though in the debbugger it looks like it is working) when the gridview is displayed I get the last row with the correct data displayed, but the first row always comes up with "Select a Mode" - the default when nothing is selected or returned from DB2.  I can plainly see in my DB2 data that the values are there and should be displayed.  This is where I need some help.  Please?

Attached is a .net 2.0 file with all my code for both VB.Net and ASP.Net.  I could REALLY use some hlep on this.  I've attached both my aspx and aspx.vb files as text files.  If you can take a look at them you will see a lot of code remmed out in the DataBound and now in the Pre-Render and a Sub called syncDDLs that has been remmed.  I'm back to doing this a different way - but I really want to know how to get this to work.  Any help would be much appreciated.

Thanks,

Coleen
tesrcptdisb.aspx.txt
tesrcptdisb.txt
Coleen,

Let me see if I can understand the process (without looking at code):

1) The page initially loads, and the DropDownList controls have the same selected value.

2) One control is visible, and the other is not.

3) When the visible control's SelectedIndexChanged event is fired, the hidden control's selected index is set to the same value.

4) Are you using AutoPostBack with the visible control?

5) Did you attach an event handler to the SelectedIndexChanged event for the visible control?

Bob
Well partially right.  1) the DDL's should have the same selected value but don't - that's the problem.  the DDL coming from the SQL database does not have the same initial value as the DDL coming from the DB2 database - I nedd to synch it on the initial load so that it does have the same value.
2) Correct
3) Correct
4) No - I was, I took that out
5) No - it is done on the click of the Update button when the visible control value get's changed, behind the scene I synch the two in the update process.  You can see that in the btn_update event - it works but then gets over-written with the code that I have/had on the DataBound/RowDataBound/Pre-Render which should only happen once on the initial load.  that's the problem...I can't figure out where to put that code to get it to fire once and only once - AND get it to sync up both rows initially returned from the DB2 databse.  Thanks for your help :-)

Coleen
Where is the hidden DropDownList bound?

Bob
Grrrr - sorry I may have sent you the wrong file.  I made so many changes yesterday that I probably sent the file where I had taken out the hidden drop down.  We found that the problem with the connection to the Mode_ty dropdown was not so much in the name of the filed being different (although that is a pain in the butt) we found that it was a data issue.  The COBOL program sends the value of the Mode_ty as two characters no matter what, because that is what the field length is declared as in the DB2 db and is how COBOL always sneds and (needs to receive) the data.  So when I was sending the data back to the COBOL program I was using a function (RPC_Mgr) that pads spaces on the end of any field that is passing less than the required feild length to the COBOL program. Did I mention that I REALLY HATE COBOL?

Any Way the bottom line is that we were able to fix the entire problem by changing the data that is passed from a one character field to a 2 character field.  So all the data that was being passed as "J" or "R" is now being pssed as "JA" or "RA" - which is fine because as long as we changed the values in the SQL database (which I did) then it alwyas now has two characters to pass to the DB2 db through the COBOL program.

Regardless, I would still like to know if there is a way with the GridView to set values only on the inital load of the data and then not have that code hit again when the Gridview is bound.  I could not find a method that did not call the DataBound or RwoDataBound or even the Pre-Render again.  Are there any methods that will only get called once?  I also tried writing the"synch" code as a separate function (as you can see by my remmed code in the file that I sent) but it gets called on every Pre-render too  (of course).  any suggestions on how to do that?  I am attaching the snippet of code that I removed that had the bind for the hidden dropdown - it is the ASP.Net code for both the hidden and visible columns.
<asp:TemplateField HeaderText="ModeTyKey">
	<EditItemTemplate>
		<asp:DropDownList ID="ddlModeTy2" runat="server"
		DataSourceID="DMVObjectDataSourceRcptDisb" DataValueField="MODE_TY"
		AppendDataBoundItems=true Width="40px">
		</asp:DropDownList>
	</EditItemTemplate>
	<ItemTemplate>
		<asp:DropDownList ID="ddlModeTy" runat="server" 
		DataSourceID="DMVObjectDataSourceRcptDisb"
		DataValueField="MODE_TY" AppendDataBoundItems=true Width="40px">
		</asp:DropDownList>
	</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mode Type">
	<EditItemTemplate>
		<asp:DropDownList ID="ddlMode2" runat="server" DataSourceID="odsuf_rtdata_mfmo" 
		DataTextField="data_txt" DataValueField="key_txt" AppendDataBoundItems=true>
		<asp:ListItem Value="">Select a Mode</asp:ListItem>
		</asp:DropDownList>
	</EditItemTemplate>
	<ItemTemplate>
		<asp:DropDownList ID="ddlMode2a" runat="server" DataSourceID="odsuf_rtdata_mfmo"
		DataTextField="data_txt" DataValueField="key_txt" AppendDataBoundItems=true>
		<asp:ListItem Value="">Select a Mode</asp:ListItem>
		</asp:DropDownList>
	</ItemTemplate>
</asp:TemplateField>

Open in new window

The only event that is called once is the RowCreated event.

Bob
And that is only called when the row is created the first time?
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will try putting the call to my synch sub there and let you know how it works.  Thank you!

Coleen
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial