2012-05-02 2 views
6

폼에 ddl1과 ddl2라는 두 개의 드롭 다운리스트가 있습니다. 그들은 함께 텍스트 상자 -txt1의 가시성을 결정합니다. 이를 위해 다음과 같이 확인합니다 :ASP.NET - 프로그래밍 방식으로 드롭 다운 목록 포스트 백 제어

if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2) 
    { 
    if (!txt1.Visible) 
     {txt1.Visible=true;// And then I want to call postback} 

    } 
else 
    { 
    if (txt1.Visible) 
     {txt1.Visible=false;// And then I want to call postback} 
    } 

위와 같은 조건이 충족되는 경우에만 서버에 페이지를 게시하고 싶습니다. 위의 코드는 두 드롭 다운 목록의 SelectedIndexChanged 이벤트에서 트리거됩니다. 어떤 조건 하에서 어떻게 성취 할 수 있습니까?

+0

내가 당신이 원하는 무엇을 이해하지 ... 작품 - 양식이 이미 다시 변경된 인덱스 이후에 게시되어 있습니다 - 정확히 무엇을 달성하려고? –

+0

AutoPostBack을 false로 설정했습니다. 그래서 인덱스를 변경하면 다시 게시되지 않습니다. –

+0

맞아요. 그렇다면 위의 조건이 충족 될 때만 인덱스가 변경 될 때마다 다시 게시하지 않기를 원하십니까? 그 맞습니까? –

답변

6

내가 문제를 이해한다면 모르겠지만 당신이 원하는 : ASP.NET은 항상 다시 게시됩니다, 당신은 그렇지 않으면 당신은 자바 스크립트 유효성 검사를 처리하고 수동으로 페이지를 게시 할 수 있고, 조건을 처리해야 특정 조건이 충족되는 경우에만 다시 게시를 수행합니다. 두 드롭 다운에서 자바 스크립트 함수를 연결할 수 있습니다. onchange = "return onchange();" Set Autopostback = true;

 function Onchange() { 
     var ddl1 = document.getElementById('<%= ddl1.ClientID %>'); 
     var ddl2 = document.getElementById('<%= ddl2.ClientID %>'); 
     var txtbox = document.getElementById('<%= txtbox.ClientID %>'); 
     if (ddl1.selectedIndex == 2 && ddl2.selectedIndex > 2) { 
      txtbox.style.display = "inline"; 
      __doPostBack(ddl1, ''); 
     } 
     else { 
      txtbox.style.display = "none"; 
      return false; 
     } 
    } 

aspx 코드는 다음과 같아야합니다.

<asp:DropDownList runat="server" AutoPostBack="true" ID="ddl1" onchange="return Onchange();" 
      OnSelectedIndexChanged="ddl1_SelectedIndexChanged"> 
      <asp:ListItem Text="text1" /> 
      <asp:ListItem Text="text2" /> 
      <asp:ListItem Text="text3" /> 
      <asp:ListItem Text="text4" /> 
     </asp:DropDownList> 
     <asp:DropDownList runat="server" AutoPostBack="true" ID="ddl2" onchange="return Onchange();" 
      OnSelectedIndexChanged="ddl1_SelectedIndexChanged"> 
      <asp:ListItem Text="text1" /> 
      <asp:ListItem Text="text2" /> 
      <asp:ListItem Text="text3" /> 
      <asp:ListItem Text="text4" /> 
     </asp:DropDownList> 
     <asp:TextBox runat="server" ID="txtbox" /> 

그것을 테스트하고 그것을

+0

감사합니다. Kamran. 그건 나에게 좋다. –

1

AutoPostBack = True 인 경우 이벤트가 실행되는 동안 조건이 충족 될 때 funciton을 호출하면됩니다.

if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2) 
    { 
     if (!txt1.Visible) 
     { 
      txt1.Visible=true;// And then I want to call postback 
      //dowork 
     } 

    } 
    else 
    { 
     if (txt1.Visible) 
     { 
      txt1.Visible=false;// And then I want to call postback 
      //do work 
     } 
    } 
+0

이제 SelectedIndexChanged 이벤트가 서버 측 이벤트라는 것을 알게되었습니다. 따라서 다시 게시가 필요합니다. 이 일을하기 위해서는 자바 스크립트가 필요 하겠지만 어떻게해야할지 모르겠다. –

관련 문제