포스트백 후 모든 항목이 손실되는 CheckBoxList 컨트롤 (CheckBoxList control losing all items after post back)


문제 설명

포스트백 후 모든 항목이 손실되는 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.gorginChamika Sandamal)

참조 문서

  1. CheckBoxList control losing all items after post back (CC BY‑SA 3.0/4.0)

#list #checkbox #webforms #javascript #ASP.NET






관련 질문

파이썬에서 데이터를 정렬하는 방법 (How arrange data in python)

포스트백 후 모든 항목이 손실되는 CheckBoxList 컨트롤 (CheckBoxList control losing all items after post back)

목록 목록의 효과적인 구현 (Effective implementation of list of lists)

DictReader가 내 파일의 두 줄을 건너뛰고 있습니까? (DictReader is skipping two lines of my file?)

잘못된 값을 얻는 목록 확인 후 (After list checking getting wrong value)

결과를 세로 방향으로 저장하는 방법 (How do i save the result in a Vertical direction)

Python 2.x: 튜플 목록의 항목 합계 (Python 2.x: Summing items in a list of tuples)

itemgetter를 사용하지 않고 n번 발생하는 요소가 있는 목록 내 항목 인쇄 (Printing items inside a list which have an element that occurs n times without using itemgetter)

반환된 목록에서 장소가 바뀐 항목 삭제 (Deleting items that have the place swapped around in a returned list)

arrayToList가 홀수 출력을 생성합니다. 뭐가 문제 야? (arrayToList producing odd outputs. What's wrong?)

R 목록을 벡터로 바꾸는 방법과 목록이 필요한 이유 (R how to turn lists to vectors, and why a list at all)

python, 출력으로 코딩하는 동안 pycharm에서 이 메시지를 받았습니다. :TypeError: can't convert type 'list' to numerator/denominator (python , I got this message in pycharm while coding as output :TypeError: can't convert type 'list' to numerator/denominator)







코멘트