문제 설명
포스트백 후 모든 항목이 손실되는 CheckBoxList 컨트롤 (CheckBoxList control losing all items after post back)
I have a CheckBoxList
on page. All controls on page are in UpdatePanel
.
<asp:CheckBoxList ID="AreaCheckBoxList" Width="300px" runat="server" onchange="RemoveItems()" RepeatDirection="Horizontal">
<asp:ListItem Value="" style="display: none"></asp:ListItem>
</asp:CheckBoxList>
Add item to it when click on a anchor tag with javascript.
<a class="button" onclick="addToCheckBoxListControl()" />
function addToCheckBoxListControl() {
var tableRef = document.getElementById('<%= AreaCheckBoxList.ClientID %>');
var list = document.getElementById('<%= AreaDropDownList.ClientID %>');
var valueValue = list.options[list.selectedIndex].value;
var textvalue = list.options[list.selectedIndex].text;
if (!isValueAlreadyExist(tableRef, valueValue)) {
var rowArray = tableRef.getElementsByTagName('tr');
var rowCount = rowArray.length;
var rowElement = tableRef.insertRow(rowCount);
var columnElement = rowElement.insertCell(0);
var checkBoxRef = document.createElement('input');
var labelRef = document.createElement('label');
checkBoxRef.type = 'checkbox';
checkBoxRef.value = valueValue;
labelRef.innerHTML = textvalue;
columnElement.appendChild(checkBoxRef);
columnElement.appendChild(labelRef);
}
}
I have a button on page, CheckBoxList
control losing all items(items add with javascript) after button click.
AreaCheckBoxList.Items.Count ‑‑‑‑‑> =1
참조 솔루션
방법 1:
Seems you are binding the data to CheckBoxList
from the code behind and you are checking the IsPostBack
before data binding. so your data is only available for the direct page loads and not available for the postbacks.
EDIT
Seems your problem is JavaScript code is not executing after the postback. You can register the JavaScript function when updatepanel complete the request.Try this code to call your JavaScript function after updatepanel complete request.
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(addToCheckBoxListControl)
http://msdn.microsoft.com/en‑us/library/bb397523(v=vs.100).aspx
The pageLoaded event is raised after all content on the page is refreshed, whether it was refreshed because of a synchronous (full‑page) postback or an asynchronous postback. You can use this event to provide a custom transition effect for updated content.
UPDATE2
You asked only part of the question. It seems the real problem is, you are adding items to the CheckBoxList
using JavaScript and want to access it in code behind. It is not possible. if you modified the CheckBoxList
in JavaScript, it would not persist server‑side and would disappear after PostBack occurred.
So my advice for this situation is, replace your <a>
tag with asp.net link button and it will do a postback when you click. on that click event you can add the element on serverside. so it will available both serverside and client side for you.
(by ar.gorgin、Chamika Sandamal)