我研究过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对象。得到了同样的错误。
我不是动态创建控件。我有几个控件,当我调用该方法时,我想对其进行更改。所有的控件都是在aspx页面上创建的。但Shared方法不会让我调用控件,我必须在方法中创建它的一个实例。我通常使用FindControl来做到这一点。但似乎我尝试使用的每个容器都给我提供了与帖子中提到的相同的错误消息。外部容器是页面本身,但是如何在Shared方法中创建整个页面的实例? –
添加了我希望的是您请求的相关代码。 –
我想完成的是:我有一个下拉列表,我需要它打开一个确认框,但只有当它的选择从YES更改为NO时。所以我添加了一个if ...然后在它的事件处理函数中调用一个javascript函数。 javascript函数创建确认框,然后将确认框答案并使用ajax调用代码隐藏方法以在确认答案为YES时执行其他更改。 –