2013-10-09 38 views
1

我研究過FindControl調用,但沒有發現我提到的Public Shared Sub問題。我正在使用一個aspx頁面,它在vb.net中是代碼隱藏的。我沒有使用母版頁。FindControl不能在Shared Sub中工作

我已經在普通的公共子方法(如下面引用名爲panContent的主面板對象)在這個頁面上成功地使用了FindControl。

Dim rdobtn As RadioButton = DirectCast(panContent.FindControl("rbFarm"), RadioButton) 

但是在公共共享Sub中,不能使用panContent對象。我收到錯誤「引用非共享成員需要一個對象引用。」我嘗試使用Page.FindControl(「panContent」)和Me.FindControl(「panContent」)創建一個Panel對象,並得到相同的錯誤。 aspx頁面的順序是:body標籤,form標籤,scriptmanager標籤,Update Panel(名爲upMain),然後是主面板(名爲panContent)。

如何從控件創建對象以便我可以更改Shared Sub中的對象屬性?

ASPX頁面(編輯空間)

<%@ Page Language="VB" AutoEventWireup="false" Inherits="GM._Default" CodeBehind="Default.aspx.vb" %> 
<% Register Assembly="AjaxControlToolkit, Version, etc... %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>GMN</title> 
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script> 
    <script type="text/javascript"> 
    function fnConfirmMsg() { 
     var ans = confirm("This will delete any saved bank information. Continue?"); 
     if (ans == true) { 
      $.ajax({ 
       type: "POST", 
       url: "Default.aspx/DraftContinue", 
       contentType: 'application/json; charset=utf-8', 
       data: '{}', 
       dataType: 'json', 
       success: function (result) { 
       } 
      }); 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
     <asp:UpdatePanel ID="upMain" runat="server" UpdateMode="conditional"> 
      <ContentTemplate> 
       ...other divs... 
       <div id="centercontent"> 
        <asp:Panel ID="panContent" runat="server"> 
         <table border="0"> 
          ...other <tr> with controls... 
          <tr> 
           <td> 
            <asp:DropDownList ID="ddlDraft" runat="server" AutoPostBack="true"> 
             <asp:ListItem Value="Y">Yes</asp:ListItem> 
             <asp:ListItem Value="N">No</asp:ListItem> 
            </asp:DropDownList> 
           </td> 
           <td> 
            <asp:RadioButton ID="rbFarm" runat="server" AutoPostBack="true" /> 
           </td> 
          </tr> 
          ...other <tr> with controls... 
         </table> 
        </asp:Panel> 
       </div> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </form> 
</body> 
</html> 

代碼隱藏(只顯示相關項目)

Protected Sub ddlDraft_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlDraft.SelectedIndexChanged 
    If ViewState("DraftYorN").Equals("Y") And ddlDraft.SelectedValue = "N" Then 
     ScriptManager.RegisterStartupScript(Me, Me.GetType(), "callConfirmMsg", "fnConfirmMsg();", True) 
    End If 
End Sub 

<System.Web.Services.WebMethod()> _ 
Public Shared Sub DraftContinue() 
    Dim ddlDraft As DropDownList = DirectCast(panContent.FindControl("dlDraftRenewMembership"), DropDownList) 
    Dim rbtnFarm as RadioButton = DirectCast(panContent.FindControl("rbFarm"), RadioButton) 
    If ddlDraft.SelectedValue = "N" then 
     rbtnFarm.Checked = True 
    End If 
End Sub 

它是給錯誤的panContent。所以我想我只是使用容器upMain製作panContent對象。得到了同樣的錯誤。

回答

1

您是否動態創建控件?如果你是,你需要在PageInit中做到這一點。您是否在創建它之前嘗試「找到」它?

更多代碼將是有幫助的。

+0

我不是動態創建控件。我有幾個控件,當我調用該方法時,我想對其進行更改。所有的控件都是在aspx頁面上創建的。但Shared方法不會讓我調用控件,我必須在方法中創建它的一個實例。我通常使用FindControl來做到這一點。但似乎我嘗試使用的每個容器都給我提供了與帖子中提到的相同的錯誤消息。外部容器是頁面本身,但是如何在Shared方法中創建整個頁面的實例? –

+0

添加了我希望的是您請求的相關代碼。 –

+0

我想完成的是:我有一個下拉列表,我需要它打開一個確認框,但只有當它的選擇從YES更改爲NO時。所以我添加了一個if ...然後在它的事件處理函數中調用一個javascript函數。 javascript函數創建確認框,然後將確認框答案並使用ajax調用代碼隱藏方法以在確認答案爲YES時執行其他更改。 –