aspx_beginners thread: Problem dynamically adding controls using viewstate data - one click behind
Hello. Help!
I have a page which contains a number of drop down lists containing data
drawn from a database. It is to be used to automatically price a
computer system. The number of lists varies depending on information on
a database and user feedback. Each list contains parts from one category
ie CPUs, HDDs etc.
In Page_Load I grab the information required from the database and use a
loop to add controls three per loop - one dropdownlist, one imagebutton,
and a literal control as a breakline. Each button is related to a
Oncommand subroutine. This displays as expected.
When the user clicks on a button next to any specific category, the page
should reload with state information on current selections maintained
and, this is the tricky bit, must add a extra dropdown list for that
category.
When a button is clicked a value is added to the viewstate of the page
relating to how many times the relevant category should display. eg if
the button was identified with CPUs, Viewstate("CPU")=2 might be added.
On PageLoad, when controls are being generated, the viewstate for each
category is checked and if it = 2 or more, the appropriate number of
dropdownlists are added.
All this works except it is one click behind.
ie
1. 1st page load: Page shows one dropdown list per category
2. Click on say CPU button: Page reloads as before, Viewstate for CPU=2
is added AFTER controls are added in page load. If page is reloaded
again, 2 x CPU lists finally appear
I have found that if I load the page controls after the button command
subroutine has had a chance to run (used Page.IsPostBack) then it won't
run at all. ie the button control must be generated before its
subroutine can run.
It seems to be a catch 22.
The only solution I can think of is to add the extra control in the
button clicked subroutine and insertinto the control collection at the
appropriate spot. This is difficult and messy.
Is there a workaround for this? This is my first asp.net page and I am
not an experienced programmer. I would appreciate any help.
Relevant code is below:
---------------
Sub Page_Load
SetControls
End Sub
Sub SetControls
...CUT - Get the parts data here
dvwParts = dstParts.Tables("Parts").DefaultView
...CUT - Get the category data here
dtrCategories = cmdSelectCategories.ExecuteReader()
While dtrCategories.Read()
numTimesToDisplay = ViewState(dtrCategories("fldcategory"))
If numTimesToDisplay < 1
NumTimesToDisplay = 1
End If
For x = 1 to numTimesToDisplay
dvwParts.RowFilter = "fldCategory = '" & dtrCategories
("fldcategory") & "'"
AddDropDownList(dvwParts, dtrCategories("fldcategory"))
Next x
End While
End Sub
Sub AddDropDownList (ByRef dvwData As DataView, strCategory As String)
Dim moreImageButton As New ImageButton
moreImageButton.CommandName = strCategory
moreImageButton.ImageURL = "images/more.gif"
Dim aList As New DropDownList
aList.DataTextField = "fldTitle"
aList.DataValueField = "fldPartID"
aList.DataSource = dvwData
aList.DataBind()
plhPlaceHolder.Controls.Add(aList)
plhPlaceHolder.Controls.Add(moreImageButton)
AddHandler moreImageButton.Command, AddressOf
Me.MoreImageButton_Command
plhPlaceHolder.Controls.Add(New LiteralControl("<BR>"))
End Sub
Sub MoreImageButton_Command(s As Object, e As CommandEventArgs)
Dim strCategoryToAdd As String
strCategoryToAdd = e.CommandName
Dim intNumTimesDisplayed As Integer
intNumTimesDisplayed = ViewState(strCategoryToAdd)
If intNumTimesDisplayed < 1
intNumTimesDisplayed = 2
Else
intNumTimesDisplayed = intNumTimesDisplayed + 1
End If
ViewState(strCategoryToAdd) = intNumTimesDisplayed
End Sub