Background: I am implementing a asp.net web form in .Net framework 4.7.2.
I am currently trying to retrieve list of values from generated controls in a Panel. Note that the panel is within a Modal.
Code of the aspx
<div class="modal fade" id="addLead" tabindex="-1" role="dialog" aria-labelledby="addLead" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Lead</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-lg-12">
<asp:Panel ID="panelAddAdditionalLeads" runat="server"></asp:Panel>
</div>
</div>
</div>
<div class="modal-footer">
<asp:Button ID="btnAddNewRow" runat="server" class="btn btn-secondary mr-auto mr-auto" Text="+ Add Next Lead" OnClick="btnAddNewRow_Click" />
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<asp:Button ID="btnAdd" runat="server" class="btn btn-outline-secondary" Text="Add" UseSubmitBehavior="false" OnClick="btnAdd_Click" data-dismiss="modal"/>
</div>
</div>
</div>
</div>
Controls are all generated successfully, but when clicking on button (created static), it returns null when I try to do the following (It is in the button click function [btnAdd_Click]):
List<string> currentPanelData = new List<string>();
if (panelAddAdditionalLeads.HasControls())
{
foreach (Control control in panelAddAdditionalLeads.Controls)
{
if (control is TextBox)
{
if (!string.IsNullOrEmpty(((TextBox)control).Text))
{
currentPanelData.Add(((TextBox)control).Text);
}
else
{
currentPanelData.Add("");
}
}
if (control is DropDownList)
{
currentPanelData.Add(((DropDownList)control).SelectedValue);
}
}
}
I have been trying to look for solutions, if there are also any other alternative ways, I would be more than happy to explore as well.
I have also made use of Page_PreInit to load the controls but it isn't working as well. This is the code used in Page_PreInit:
private void LoadControls()
{
List<string> currentPanelData = new List<string>();
if (Session["CurrentDataList"] != null)
{
currentPanelData = (List<string>)Session["CurrentDataList"];
}
int loopCount = 0;
//To load previous controls data
if (currentPanelData.Count >= 1)
{
foreach (Control control in this.panelAddAdditionalLeads.Controls)
{
if (loopCount < currentPanelData.Count)
{
if (control is TextBox)
{
((TextBox)control).Text = currentPanelData[loopCount];
loopCount++;
}
if (control is DropDownList)
{
((DropDownList)control).SelectedValue = currentPanelData[loopCount];
loopCount++;
}
}
}
}
}
Thanks in advance! Meanwhile, I will also keep trying to see if there is any solutions for this...