2011-07-06 2 views
1

사용자가 여러 전자 메일받는 사람을 입력 할 수있는 사용자 정의 컨트롤이 있습니다. 페이지로드시 이름 및 전자 메일 주소 텍스트 상자 (제거 단추 제외)가 포함 된 테이블 행이 하나씩 있습니다.jQuery를 사용하여 ASP .NET에서 테이블 행을 동적으로 추가/제거 할 때 발생하는 문제

다른 모든 테이블 행은 jQuery를 사용하여 추가되며, 추가 된 각 행에는 제거 버튼이 있습니다. 특정 행을 삭제하십시오.

<div> 
<table cellspacing="5" runat="server" id="tblEmailRecipients" clientidmode="Static"> 
    <tr> 
     <td colspan="4">Custom Email Recipients</td> 
    </tr> 
    <tr> 
     <td colspan="3"> 
      <input type="button" value="Add More" onclick="AddNewTableRow();" id="btnAdd" /> 
     </td> 
    </tr> 
    <tr style="font-weight:bold"> 
     <td>Name</td> 
     <td>Email Address</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td><asp:TextBox runat="server" ID="txtName" ></asp:TextBox> </td> 
     <td><asp:TextBox runat="server" ID="txtEmailAddress" ></asp:TextBox> 
       <asp:RegularExpressionValidator ID="regexValidatorEmailAddress" runat="server" ErrorMessage="Invalid Email Address" ControlToValidate="txtEmailAddress" 
        Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> 
     </td> 
     <td> 
      <%--<input type="button" value="Remove" onclick="DeleteTableRow(this);" id="btnRemove" name="btnRemove" />--%> 
     </td> 
    </tr>   
</table> 
</div> 
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" 
    style="height: 26px" /> 

jQuery를 : 다음은 내 코드입니다

function DeleteTableRow(ctrl) { 
    //delete control table row 
    $($(ctrl).closest("tr")).remove(); 
} 

function AddNewTableRow() { 
    //Get Row clone and add as last row 
    //Also, created row clone but with different control ids 
    var i = $("#tblEmailRecipients tr").length; 
    var newRow = $("#tblEmailRecipients tr:last").clone().find("input").each(
     function() { 
      $(this).attr({ 
       'id': function (_, id) { return id + i }, 
       'name': function (_, name) { return name + i }, 
      }); 
     }).end(); 

    $("#tblEmailRecipients tr:last").after(newRow); 
    $("#tblEmailRecipients tr:last input:text").val(""); 

    //Add 'Remove' button in the last column - this needs to be done only for second row, all rows after 2nd row will auto. contain Remove button as they are cloned from prev. row 
    var lastCell = $("#tblEmailRecipients tr:last td:last"); 
    var lastCellContents = jQuery.trim($(lastCell).html()); 

    if (lastCellContents == '') { 
     var btnRemoveName = "btnRemove" + i; 
     var btnRemove = "<input type='button' value='Remove' onclick='DeleteTableRow(this);' id='" + btnRemoveName + "' name='" + btnRemoveName + "' />"; 
     $(btnRemove).appendTo($(lastCell)); 
    }  

    //Move focus to newly added row Textbox 
    $("#tblEmailRecipients tr:last input:first").focus(); 
} 

내가 뒤에있는 코드에서, 2 명 이상의 수신자 (테이블 행)을 추가하더라도 btnSave_Click,
을 처리하는 동안 내가 직면하고 문제입니다 - 전자 메일받는 사람이 포함 된 첫 번째 행만 보았습니다.

런타임에 추가 된 다른 테이블 행에 액세스 할 수없는 이유가 궁금합니다. 다시 게시 한 후에도 UI에서 누락 될 수 있습니까?

안내해주세요.

감사합니다.

답변

0

각 행의 편집 상자에 동적 ID를 부여하면 모든 행의 ID가 동일한 모든 편집 상자가 제공됩니다. 각 행마다 id가 달라야합니다.

+0

답장을 보내 주셔서 감사합니다. 내 질문을 수정하여 복제 된 제어 ID를 업데이트했습니다. 이제는 텍스트 상자 내용을 함께 결합하지 않지만 첫 번째 전자 메일 수신자 (페이지로드에있는 테이블 행) 만 볼 수 있습니다. – iniki

+0

hmmm 기본적으로 사용자에게 새로운 행이 추가되었음을 보여주는 클라이언트 스크립팅 (jquery)을 사용하여 새 행을 추가하지만 서버는 새 행이 추가되고 서버 측 코드가 새 행을 추가하지 않는다는 것을 알 수 없습니다 . 새로운 행을 추가해야한다는 것을 서버에 알리기 위해 카운터 또는 변수를 추가해야합니다. – Pukhoo

+0

나는 최근에 asp.net C#에서 페이지로드에서 패널 및 델리게이트를 사용하는 대신 자바 스크립트를 사용하여 이와 비슷한 작업을 수행했습니다. – Pukhoo

관련 문제