0

다른 웹 양식에 데이터를 게시하는 asp webform을 만들려고합니다.마스터 페이지를 사용하여 한 콘텐츠 페이지에서 다른 콘텐츠 페이지로 데이터를 전달할 수 없습니다.

두 개의 별도 프로젝트를 만들었습니다. 하나는 마스터 페이지를 사용하고 다른 하나는 그렇지 않습니다.

Senario :

WebForm1.aspx를는 두 개의 텍스트 상자를 가지고 있으며, 버튼

<table> 
     <tr> 
      <td >Name:</td> 
      <td > 
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
      </td> 
      <td class="auto-style1"></td> 
     </tr> 
     <tr> 
      <td>Id:</td> 
      <td> 
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
      </td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td> 
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
      </td> 
      <td>&nbsp;</td> 
     </tr> 
    </table> 

WebForm2.aspx.cs 제출는 데이터가 WebForm1.aspx를에서 수신 표시됩니다 두 개의 레이블이 있습니다

Page prevPage = this.PreviousPage; 
     if (prevPage != null) 
     { 
      Label1.Text = ((TextBox)prevPage.FindControl("TextBox1")).Text; 
      Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text; 
     } 

사례 1 : [마스터 페이지가없는 게시]

데이터는 으로 보통으로 게시됩니다.

사례 2 :

[마스터 페이지에 게시] 난 NullReferenceException이를 얻을.

그래서 코드를 무너 뜨 렸습니다. 디버깅하는 동안

Page prevPage = this.PreviousPage; 
     if (prevPage != null) 
     { 
      ControlCollection collec = prevPage.Controls; 
      Control ctrl= prevPage.FindControl("TextBox1"); 
      TextBox txtbx = (TextBox)ctrl; 
      Label1.Text = txtbx.Text; //Exception raised here 

      Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text; 
     } 

: 내가 직접 실행 창에 "collec.Count"을 실행.

케이스 1 :

collec.Count 1을 [리턴 [마스터 페이지와 전기] [마스터 페이지없이 전기]는

collec.Count은 5

사례 2 리턴 왜? ]

나중에

는 I 데이터가 공개 특성

WebForm1.aspx.cs로

protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Server.Transfer("WebForm2.aspx"); 
    } 

    public string Name { get { return TextBox1.Text; } } 
    public string ID { get { return TextBox2.Text; } } 

WebForm2.aspx.cs

WebForm1 prevPage = (WebForm1)this.PreviousPage; 
     if (prevPage != null) 
     { 
      ControlCollection c = prevPage.Controls; 
      Label1.Text = prevPage.Name; 
      Label2.Text = prevPage.ID; 
     } 
를 사용하여 전달하려고

이제는 마스터 페이지에서도 제대로 작동합니다.

아무도 나에게 무슨 일이 일어나는 이유와 내가 왜 NullReferenceException을주는 마스터와 함께 다른 콘텐츠 페이지로 한 콘텐츠 페이지에서 게시 할 수 있습니까?

답변

0

먼저

따라서 제출 페이지의 내용 자리에서 봐야한다, 코드는 더 같을 것이다 :

ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1"); 
     TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1"); 

당신이 TextBox1에의 ID로 마스터 페이지를 텍스트 상자를 사용하는 ContentPlaceHolder1에 묶여 콘텐츠 컨트롤을해야합니다 내부의 id 속성은 다음과 같이 연장하지 :

<input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" /> 

을하지만 당신이 마스터 페이지를 사용하지 않을 경우에는이있다 'ContentPlaceHolder'이므로 TextBox1은 다음과 같이 렌더링됩니다.

<input name="TextBox1" type="text" id="TextBox1" /> 
+0

고마워요! 이것은 내 문제를 해결합니다. 그러나 우리는 하나의 콘텐츠 페이지에서 다른 콘텐츠 페이지로 옮길 때 Master.FindControl()이 필요한 이유를 여전히 이해하지 못합니까? – gaganKapula

+0

이전 페이지는 마스터 페이지를 기반으로하는 콘텐츠 페이지입니다. – Aria