After a few days of searching forums, reading docs, and meeting with teammates I have yet to find a solution to this issue.
I have a form in a VB.NET project that dynamically adds user controls with the postback from a click of an asp button. The reason there is a postback is because there are calculations happening between the user control and the main page.
There is a dropdown list that needed to have the <optgroup>
tag. To do this, I wrote a list of data into an XML file with a structure that follows:
<AccountListData>
<account>
<group>OPTION GROUP NAME HERE</group>
<value>OPTION VALUE/TEXT HERE</value>
</account>
<account>...</account>
</AccountListData>
The XML file is 1117 lines long. It wasn't fun to write out. But, the reason I did was to use it to create the <select>
tag in the code behind and assign it to the innerHTML
attribute of a div
tag in the front end. It looks something like this:
Form.aspx
<table>
<tr>
<td id="DateLabel" runat="server">Date:</td>
<td id="DescriptionLabel" runat="server">Description:</td>
<td id="AmountLabel" runat="server">Amount:</td>
<td id="DropDownLabel" runat="server">Select:</td>
</tr>
<asp:Placeholder ID="ph1" runat="server"></asp:Placeholder>
</table>
<asp:Button ID="AddItemButton" runat="server" CausesValidation="false" Text="Add Item">
<asp:Label ID="RowCountLabel" runat="server"></asp:Label>
Form.aspx.vb
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'Add/Removed User Control
AddRemoveUserControl()
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "CalculateCosts", "Javascript function();"), True)
End Sub
...
Public Function GetPostbackControlName(ByVal Page As Page) As String
Dim ctrlName As String
ctrlName = Page.Request.Params.Get("__EVENTTARGET")
Return ctrlName
End Function
...
Private Sub AddRemoveUserControl()
'Get and check the ID of what caused the postback
Dim c As Control = GetPostbackControlName(Page)
If Not IsNothing(c) Then
If c.ID.ToString = "AddItemButton" Then
RowCountLabel.Text = CInt(RowCountLabel.Text) + 1
End If
End If
'Get ID for dynamic user controls
Dim ControlID As Integer = 0
For i As Integer = 1 To (CInt(RowCountLabel) - 1)
Dim DynamicControl As ItemTable = LoadControl("usercontrols/ItemTable.ascx")
DynamicUserControl.ID = "uc" & CStr(ControlID)
'Add an event handler for the usercontrol
AddHandler DynamicUserControl.RemoveUserControl, AddressOf Me.HandleRemoveUserControl
'Add user control to placeholder on front end
ph1.Controls.Add(DynamicUserControl)
'Number the amount of user controls
ControlID += 1
Next
NumberItemTable()
End Sub
...
Private Sub HandleRemoveUserControl(sender As Object, e As EventArgs)
'Removes usercontrol from placeholder and lowers count #
End Sub
...
Private Sub NumberItemTable()
'Gives a number value to a property in the usercontrol
End Sub
Control.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ItemTable.ascx.vb" Inherits=".ItemTable" %>
<tr>
<td>
<asp:Textbox ID="Date" runat="server" CssClass="datepicker"></asp:Textbox></td>
<td>
<asp:Textbox ID="Description" runat="server"></asp:Textbox></td>
<td>
<asp:Textbox ID="Amount" runat="server"></asp:Textbox></td>
<td>
<div ID="DynamicSelect" runat="server"></div></td>
</tr>
Control.ascx.vb
Dim XMLInfo As XDocument
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
FillDropDownList()
End Sub
Private Sub FillDrowDownList()
'Get XML data
Dim XMLData 'all the XML data is loaded as an XDocument here
Dim temp As string
'WHERE THE DROPDOWN IS DYNAMICALLY CREATED WITH XML DATA
DynamicSelect.InnerHTML += "<select ID='" + Me.ID.ToString + "CreatedSelect'><option value='' selected disabled>- Select -</option>"
For Each element As XElement In XMLData.Descendants("account")
Dim group As String = element.<group>.Value.ToString
Dim value As String = element.<value>.Value.ToString
If group <> "" Then
If group <> temp Then
temp = group
DynamicSelect.InnerHTML += "</optgroup>"
DynamicSelect.InnerHTML += "<optgroup label='" + group + "'>"
End If
End If
DynamicSelect.InnerHTML += "<option value='" + value + "'>" + value + "</option>"
Next
DynamicSelect.InnerHTML += "</select>"
End Sub
When a value is selected on the dynamic dropdown list, any postback clears that selection. Any insight into why that is happening and what I can do to solve this issue would be GREATLY appreciated.