Susan O
Susan O

Reputation: 65

FindControl not working in Shared Sub

I have researched the FindControl call but nothing I have found mention the Public Shared Sub problems. I am using an aspx page and it's code-behind in vb.net. I am not using a Master page.

I am already using FindControl successfully on this page in normal Public Sub methods like below referencing the main Panel object named panContent.

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

But within a Public Shared Sub the panContent object cannot be used. I get the error "Reference to a non-shared member requires an object reference." I tried to create a Panel object using Page.FindControl("panContent") and Me.FindControl("panContent") and get the same error. The aspx page is in this order: body tag, form tag, scriptmanager tag, an Update Panel (named upMain), then the main Panel (named panContent).

How can I create an object from a control so I can change the object properties in the Shared Sub?

Aspx page (edited for space)

<%@ 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>

Code-Behind (only showing relevant items)

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

Its the panContent that is giving the error. So I figured I would just make the panContent object using its container upMain. Got the same error.

Upvotes: 1

Views: 791

Answers (1)

andleer
andleer

Reputation: 22578

Are you dynamically creating the control? If you are, you will need to do that during PageInit. Are you attempting "find" it before you have created it?

More code would be helpful.

Upvotes: 1

Related Questions