Please find the scenario I'm trying to work on:
I have an aspx page, whose content is loaded by dynamically loading various user controls. Each of these user controls has their set of controls like a textbox, checkboxlist or a checkbox.
On click of "Save" button, I would like to capture the data in the textbox and checked boxes from the checkboxlist.
I was able to gather the data from these controls successfully using the following piece of code:
UserControl ctl1 = placeholder.FindControl("user_control_1") as UserControl;
if (ctl1 != null)
{
RadTextBox txtName = ctl1 .FindControl("txtFirstName") as RadTextBox;
if (txtName != null)
{
// Perform required CRUD
}
}
This approach was working well until I had a new requirement. To address the new requirement I had to change the logic to make an AJAX request from jQuery as below:
$.ajax({
type: "POST",
url: window.location.href,
data: {
data1: data
},
success: function (result) { //success
},
failure: function (result) {
//failure
}
}).done(function (o) {
//Error free
});
With this kind of request, I'm unable to access textbox data (or checkboxlist's checked items) on my .cs. Any call to txtFirstName.Text is coming back as null.
UserControl ctl1 = placeholder.FindControl("user_control_1") as UserControl;
if (ctl1 != null)
{
RadTextBox txtName = ctl1 .FindControl("txtFirstName") as RadTextBox;
if (txtName != null)
{
// Works well till this point.
// txtName.Text returns empty value, inspite of having data in it
}
}
Can anyone please provide pointers on how I can achieve this?
EDIT: Since a new AJAX request is generated on the click of "Save" button, all the user controls are loaded again which is resulting in losing all data entered by an end user. What are some of the ways I can capture all the end user entered data before the AJAX request is made? Perhaps, create a JSON in jQuery for textboxes and checkboxlists, then pass that in "data"?