2017-07-11 46 views
0

在我的網頁中,我使用兩個用戶控件: ucControl1和ucControl2。 ucControl1控件還包含其中的ucControl2控件的一個實例。 當您運行應用程序並轉到相關頁面時,似乎只有ucControl1內部的實例才能正常工作。當試圖執行直接在頁面上的ucControl2的功能時,它會正確地執行後端代碼,正確打開模式,但不會在屏幕上顯示結果。 會發生什麼?asp.net頁面中相同用戶控件的兩個實例

的頁面是這樣

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageSample.aspx.cs" MasterPageFile="~/MasterPage.master" Inherits="PageSample" %> 
<%@ MasterType VirtualPath="~/MasterPage.master" %> 
<%@ Register Src="~/Controls/ucControl2.ascx" TagPrefix="uc2" TagName="ucControl2" %> 
<%@ Register Src="~/Controls/ucControl1.ascx" TagPrefix="uc1" TagName="ucControl1" %> 
<asp:Content runat="server" ContentPlaceHolder ID="cpSampleID"> 
    <div> 
     <uc1:ucControl2 runat="server" ID="uc2" /> 
    </div> 
    <uc1:ucControl1 runat="server" ID="uc1" /> 
</asp:Content> 

而且的UserControl1

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucControl1.ascx.cs" Inherits="ucControl1" %> 
<%@ Register Src="~/Controls/ucControl2.ascx" TagPrefix="uc1" TagName="ucControl2" %> 

<%-- Something Something Something --%> 
    <uc1:ucControl2 runat="server" ID="ucControl2" /> 
+1

認爲我們需要看到自己的用戶控件,並且後端代碼 – ADyson

+0

後端工作得很好。它只是在文本框中設置一些文本值。 我做了一些測試,並且頁面的ucControl字段被填充,但是看起來在渲染時總是呈現ucControl2中的ucControl1。 如果我從頁面中刪除ucControl1,完美地工作。 –

+0

「ucControl2內部的ucControl1」。你的意思是相反的嗎?無論如何,你提供的代碼不足以發現問題。你可能認爲你的後端工作正常,但很明顯,代碼中的某些內容導致了問題,所以我們需要看到它。抽象地說,沒有理由說你所展示的設計不起作用。問題將出現在某處的實現細節中。因此,請不要基於模糊和無法驗證的「正常工作」聲明提出異議,請節省一些時間併發布代碼,以便其他人可以爲您提供有意義的幫助。 – ADyson

回答

1

我已經找到了解決辦法。該模式是通過javascript打開的。

ucControl2

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucControl2.ascx.cs" Inherits="ucControl2" %> 
<%@ Register Src="~/Controls/ucControl2.ascx" TagPrefix="uc2" TagName="ucControl2" %> 
    <div id="modal"> 
    <!-- Some Textboxes --> 
    </div> 

方法來打開模態

public void OpenModal() 
{ 
    string id = "modal"; 
    string js = [email protected]" 
       $(document).ready(function() {{ 
        if ($('.modal.in').length > 0) {{ 
         $('.modal').on('hidden', function() {{ 
          $('.modal').off('hidden'); 
          $('#{id}').modal({{show: true, keyboard: false, backdrop: 'static'}}); 
         }}); 
         $('.modal').modal('hide'); 
        }} else {{ 
         $('#{id}').modal({{show: true, keyboard: false, backdrop: 'static'}}); 
        }} 
       }});"; 

    ScriptManager.RegisterStartupScript(this, this.GetType(), id, js, true); 
} 

因此,當控制被調用時,JavaScript不知道該控件應該打開,在這種情況下,它是開放頁面上的第一個控件。 爲了解決這個問題,我已經通過asp面板更改了div,這樣asp爲每個控件生成一個不同的客戶端ID,並且控件正確顯示。

相關問題