PDA

View Full Version : dynamically created datagrid event handlers


mitharatowen
06-28-2006, 07:52 PM
hello everyone, my code is long and complex (probably unnessiarily so as I am new and dont know all the coding protocol and shortcuts) but here's a snippet of the part I am having trouble with.

Public Sub SetGridProperties(theGrid, num)
'declare a new datagrid and set properties
theGrid.Width = Unit.Pixel(700)
theGrid.BorderWidth = Unit.Pixel(2)
theGrid.CellPadding = 10
theGrid.GridLines = GridLines.Both
theGrid.BorderColor = Color.Blue
theGrid.ShowHeader = True
theGrid.AutoGenerateColumns = False
theGrid.SelectedItemStyle.BackColor = Color.Yellow

'add bound columns to the datagrid
Dim datagridcol As New BoundColumn()
datagridcol.HeaderText = "Option Number"
datagridcol.DataField = "OptionNumber"
theGrid.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "Manufacturing Part Number"
datagridcol.DataField = "ManfPartNumber"
theGrid.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "Description"
datagridcol.DataField = "Description"
theGrid.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "List Price"
datagridcol.DataField = "ListPrice"
theGrid.Columns.Add(datagridcol)

Dim selectcol As New ButtonColumn()
selectcol.ButtonType = ButtonColumnType.LinkButton
selectcol.Text = "Select"
selectcol.CommandName = "Select"
theGrid.Columns.Add(selectcol)

'add event handlers
AddHandler CType(thegrid, DataGrid).SelectedIndexChanged, AddressOf DataGrid_SelectedIndexChanged

'bind datagrid
theGrid.DataSource = GetDataSet(num)
theGrid.DataBind()
end sub

Public Sub DataGrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
response.redirect("http://www.msn.com")
End Sub

Basically here I have created a dynamic datagrid in a placeholder, in the code above i am setting its properties and populating it from a database (set by the GetDataSet(num) function which is not shown). everything works as expected, the only problem I am having is that when you click on a "Select" button in a row in the grid, the DataGrid_SelectedIndexChanged sub doesnt seem to fire. I am not redirected to msn.com (just a test i put in there to see if the event fires, obviously not the final result) instead the page just does a post back and the datagrid disapears, which is due to it being dynamic it doesnt stick around, thats ok i just basically want to redirect to a new page with a querysting tacked on depending on which element was clicked. Now as i mentioned, im new so im probably doing something wrong here. could anyone please help me out? I need to redirect to a different page and i also need to pull out some of the data in the row that was clicked to put into the query string such as the contents of the Manufacturing Part Number column.

Thanks a bunch in advance! :thumbsup:

mitharatowen
06-28-2006, 07:55 PM
*just posting a new message to be able to subscribe to the thread, forgot to do so on my first post, sorry :p*

otaku149
06-29-2006, 05:05 AM
Hello mitharatowen,

Try to create your dynamic controls in the Page_Init so that they will catch up with the ViewState process.

mitharatowen
06-29-2006, 04:54 PM
i'm not entirely sure what that means.. id write a sub called page_init and call the functions from there right? uhm.. im not sure if i can do that because all the functions i call are based from dropdown box selectedindexchanged events. ill take a look at my code and see what i can do.

in the mean time i had an idea: if i add a new column to the database that has an html/javacript anchor with the word Select, I could bind that column to the datagrid too and have a column with the select links in it that call a javascript to add the querystring. that way i could specify the querysting for every database entry. that would work. it would require a lot of extra typing tho. it'd be a lot easier if i could get ASP to handle it itself. so ill see what can be done about the init i guess.

thanks

oh i have one more question: shouldnt the event handler sub perform its functions before postback? if that were the case then it doesnt matter if my datagrids disapear on the next trip, the function it was supposed to carry out would be performed before it disapears. guess thats not the way it works huh?

otaku149
06-29-2006, 07:44 PM
The best place to create dynamic controls is in the Page_Init function because the ViewState loading phase happens somewhere between Init and Load, that way viewState will be automatically reloaded and event handlers will work as expected:

write a sub called page_init and call the functions from there right?


Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
'create your dynamic controls here
End Sub

mitharatowen
06-29-2006, 07:55 PM
ok, but the problem i am having now is that the controls are created based on a dropdown box selection and it needs to be this way because i use the text in the selecteditem in my query to the database when populating the datagrids. if the selectedindexchanged event doesnt happen untill after all of this, how would i go about creating the datagrids in the init function? im sorry.. i dont really think i understand all this, this is really my first ASP.net application and im trying to teach myself. maybe showing you the rst of my code and exactly what i am attempting to do will help you to help me?

mitharatowen
06-29-2006, 07:58 PM
i guess what i basically need is a way to tell in the page_init which control called the postback. that way i can tell that, if a particular drop down box was the one who caused the postback, i need to create the datagrids.

mitharatowen
06-29-2006, 08:09 PM
no actually on further thought, that wont work either. because the controls will not have been initialized at that point so i wont be able to access the selecteditem of the dropdownbox.. right?

mitharatowen
06-29-2006, 09:05 PM
<Edit> the information in this post was invalid, hold on while i do some more research :p

mitharatowen
06-29-2006, 09:07 PM
<Edit> the information in this post was invalid, hold on while i do some more research :p

... erm sorry double post ...

mitharatowen
06-29-2006, 09:59 PM
ok. i dont know whats goign on here but heres what i did

Sub Page_Init(sender as Object, e as EventArgs)

if Page.IsPostBack and page.Request.Params("__EVENTTARGET") = "lstSystem" then
varlstSystem = Session("lstSystem")
response.write(Session("lstSystem"))
if instr(varlstSystem, "Choose") then
'if so, do nothing
else
'if not, display label text
label1.Text = varlstSystem & " Components List"
'create data grids and show products for slected system
CreateGrids()
end if
end if

End Sub

sub page_Load(sender as Object, e as EventArgs)
Session("lstSystem") = lstSystem.SelectedItem.Text
end sub

so onload puts the text of the dropdownbox into a session variable, then on the next round trip, i use this value in the init event to create the datagrids. so i AM calling the procedure to create the grids from the init event. however they still disapear when select is clicked!! why?? this set up also presents some other problems but right now im mostly concerned with the fact that my datagrids are still disapearing!

might this have anything to do with the fact that theyre in a place holder?


<edit> i changed the code to place the controls in the form itself instead of the place holder. i also put all of the lines executed in the page_init itself instead of having tha page_init call other functions. however my datagrids are STILL DISAPEARING!! PLEASE HELP