1

FindControl 呼び出しを調査しましたが、Public Shared Sub の問題について言及しているものは何も見つかりませんでした。aspx ページを使用していますが、vb.net のコード ビハインドです。マスター ページを使用していません。

このページでは、以下のような通常の Public Sub メソッドで FindControl を正常に使用しており、panContent という名前のメイン パネル オブジェクトを参照しています。

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

ただし、Public Shared Sub 内では、panContent オブジェクトを使用できません。「非共有メンバーへの参照にはオブジェクト参照が必要です」というエラーが表示されます。Page.FindControl("panContent") と Me.FindControl("panContent") を使用して Panel オブジェクトを作成しようとしましたが、同じエラーが発生しました。aspx ページの順序は、body タグ、form タグ、scriptmanager タグ、更新パネル (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 オブジェクトを作成するだけでよいと考えました。同じエラーが発生しました。

4

1 に答える 1

1

コントロールを動的に作成していますか? その場合は、PageInit 中にそれを行う必要があります。作成する前に「検索」しようとしていますか?

より多くのコードが役立ちます。

于 2013-10-09T20:53:46.037 に答える