In my Sharepoint page, on form submission, the elements revert to their original state (if a checkbox was unchecked to begin, was checked by a user, it reverts back to unchecked, etc.).
Fair enough.
But I want to "save state" and return those sorts of things back to the way they were. One thing in particular is the visibility of a Dropdownlist. If it was visible when the "Save" button was selected, it should return to being visible after the form is submitted; the two DropDownLists, of which at most one are visible at any given time, start off invisible or, more specifically, "slid up" like so:
$(window).load(function () {
$('[id$=ddlPayToIndividual]').hide();
$('[id$=ddlPayToVendor]').hide();
});
In trying to save the visible state of the DropDownLists, I've added bools:
bool paymentToAnIndividualDropDownChosen = false;
bool paymentToAVendorDropDownChosen = false;
...and then assign them when the Save button is selected:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
// Note current state of elements, so can be restored after saving/submitting
paymentToAnIndividualDropDownChosen = rbPaymentToIndividual.Checked;
paymentToAVendorDropDownChosen = rbPaymentToVendor.Checked;
. . .
}
I then try to set them back at the end of that same handler (the Save button's click event) like so:
if (paymentToAnIndividualDropDownChosen)
{
//rbPaymentToIndividual.Checked = true; <= This didn't work - jQuery does not respond to programatic clicks!
ddlPaymentToIndividual.Visible = true;
}
The code is reached, and the Visible value set to true (provided I had made the dropdown visible by checking "rbPaymentToIndividual" prior to selecting the Save button, but it has no effect - the dropdown remains hidden.
As you can deduce from the commented-out code, I also tried programatically checking the radiobutton which, which when done manually, sets off the following client-side code:
$(document).on("click", '[id$=rbPaymentToIndividual]', function () {
if (this.checked) {
$('[id$=ddlPayToVendor]').slideUp();
$('[id$=ddlPayToIndividual]').slideDown();
}
});
...but apparently jQuery does not respond to programatically-induced events.
Or is it a timing issue? Is it because the form is still in the "submit" phase/state that this is not working -- either the client-side or the server-side code, or both?
If that is the problem - the submit doesn't finish until after the Save button handler exits, and thus I'm setting the DropDown to visible too soon - where can I put the code to accomplish this setting of form state back to what it was before the submit? Is the Page_Load() event the place to do it and, if so, is there some property I can interrogate to determine if the form has just been submitted?
I moved the code to the end of the Page_Load() event, like so:
if (Page.IsPostBack)
{
if (paymentToAnIndividualDropDownChosen)
{
ddlPaymentToIndividual.Visible = true;
}
}
...but, because the whole shebang starts fresh, paymentToAnIndividualDropDownChosen's initialized value of false is seen, and so my intentions, though good, are keeping company with brimstone.
So how can I revert to pre-submit state - is an .INI file, or Registry usage, or Cookie, the usual way to accomplish this?
What seems to be very odd about this is that I am able to revert to the pre-submit state of an HTMLTable; this code is in the Save button click handler:
// Re-visiblize any rows with vals
if (RowContainsVals(3))
{
foapalrow3.Style["display"] = "table-row";
}
if (RowContainsVals(4))
{
foapalrow4.Style["display"] = "table-row";
}
if (RowContainsVals(5))
{
foapalrow5.Style["display"] = "table-row";
}
if (RowContainsVals(6))
{
foapalrow6.Style["display"] = "table-row";
}
...and it works fine (any rows with any vals are displayed, although at the beginning, only one row displays). Stranger yet, perhaps, is that the function:
private bool RowContainsVals(int rownum)
{
bool rowdirty = false;
switch (rownum)
{
case 3:
rowdirty = ((!String.IsNullOrEmpty(boxFund2.Text)) ||
(!String.IsNullOrEmpty(boxIndex2.Text)) ||
(!String.IsNullOrEmpty(boxOrganization2.Text)) ||
(!String.IsNullOrEmpty(boxAccount2.Text)) ||
(!String.IsNullOrEmpty(boxActivity2.Text)) ||
(!String.IsNullOrEmpty(boxAmount2.Text)));
break;
case 4:
rowdirty = ((!String.IsNullOrEmpty(boxFund3.Text)) ||
. . .
...finds values in the input text boxes; if the state of the DropDownList being visible is lost during the submit, why are not the values in the TextBoxes/text input lost?
Something is rotten in Redmond (or somewhere).
In response to , here is some more code relative to the two dropdowns in question:
Code-behind / C#:
rbPaymentToIndividual = new RadioButton
{
CssClass = "finaff-webform-field-input",
ID = "rbPaymentToIndividual"
};
rbPaymentToIndividual.Text = PAYMENT_OR_REIMBURSEMENT_TO_AN_INDIVIDUAL;
rbPaymentToIndividual.GroupName = "paymentTo";
rbPaymentToIndividual.Checked = false;
cellRadioButton1_1.Controls.Add(rbPaymentToIndividual);
rbPaymentToVendor = new RadioButton
{
CssClass = "finaff-webform-field-input",
ID = "rbPaymentToVendor"
};
rbPaymentToVendor.Text = PAYMENT_TO_A_VENDOR;
rbPaymentToVendor.GroupName = "paymentTo";
cellRadioButton2_1.Controls.Add(rbPaymentToVendor);
Rendered HTM from "View Source":
<input id="ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_rbPaymentToIndividual" type="radio" name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$paymentTo" value="rbPaymentToIndividual" />
<input id="ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_rbPaymentToVendor" type="radio" name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$paymentTo" value="rbPaymentToVendor" />