【问题标题】:ASP.NET Framework 4 client callback demo code complains about ListBox1ASP.NET Framework 4 客户端回调演示代码抱怨 ListBox1
【发布时间】:2012-02-22 01:49:37
【问题描述】:

来自这组 ASP.NET 演示代码:

http://msdn.microsoft.com/en-us/library/ms178209.aspx

在 ASP.NET 项目中的 VS2010 中,我收到错误消息:

“当前上下文中不存在名称'ListBox1'”来自引用 ListBox1 的 C# 源文件(第 35-37 行)。如果我在类中添加 ListBox1 的声明,错误就会消失,但是当我按 F5 运行它时,我会收到一条消息,指出 ListBox1 已经定义。我错过了什么?

ClientCallback.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ClientCallback : System.Web.UI.Page,
 System.Web.UI.ICallbackEventHandler
{
// protected ListBox ListBox1;
protected System.Collections.Specialized.ListDictionary catalog;
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
    String cbReference =
        Page.ClientScript.GetCallbackEventReference(this,
        "arg", "ReceiveServerData", "context");
    String callbackScript;
    callbackScript = "function CallServer(arg, context)" +
        "{ " + cbReference + ";}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
        "CallServer", callbackScript, true);

    catalog = new System.Collections.Specialized.ListDictionary();
    catalog.Add("monitor", 12);
    catalog.Add("laptop", 10);
    catalog.Add("keyboard", 23);
    catalog.Add("mouse", 17);

    ListBox1.DataSource = catalog;
    ListBox1.DataTextField = "key";
    ListBox1.DataBind();

}

public void RaiseCallbackEvent(String eventArgument)
{
    if (catalog[eventArgument] == null)
    {
        returnValue = "-1";
    }
    else
    {
        returnValue = catalog[eventArgument].ToString();
    }
}
public String GetCallbackResult()
{
    return returnValue;
}
}

和ClientCallback.aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html  >
<head id="Head1" runat="server">
<title>Client Callback Example</title>
<script type="text/ecmascript">
  function LookUpStock() {
      var lb = document.getElementById("<%=ListBox1.ClientID%>");
      try {
          var product = lb.options[lb.selectedIndex].text;
          CallServer(product, "");
      }
      catch (err) {
          alert("Please select a product.");
      }
  }

  function ReceiveServerData(rValue) {
      document.getElementById("<%=ResultsSpan.ClientID%>").innerHTML = rValue;

  }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
  <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
  <br />
  <br />
  <input type="button" value="Look Up Stock" onclick="LookUpStock()" />
  <br />
  <br />
  Items in stock: <span id="ResultsSpan" runat="server"></span>
  <br />
</div>
</form>
</body>
</html>

【问题讨论】:

    标签: c# asp.net .net callback postback


    【解决方案1】:

    问题是您的 ClientCallBack.aspx.designer.vb 类没有声明列表框。从网站上复制你之前做的完全一样的代码,然后把这行代码放在后面的代码中

      Protected WithEvents ListBox As Global.System.Web.UI.WebControls.ListBox
    

    实现 System.Web.UI.ICallbackEventHandler 受保护的目录作为 ListDictionary

    现在在标记中编辑这两行:

    第 11 行将 ListBox1 更改为 ListBox 第 26 行将 Listbox1 更改为 ListBox

    你应该是金子。

    希望有帮助

    【讨论】:

    • 我一定是看错了你的建议。这适用于演示中的 C# 代码吗?我尝试了它们,但仍然没有运气。是否需要在 ClientCallback.aspx.designer.cs 源文件或 ClientCallback.aspx.cs 源文件中添加一些代码?
    • 搞定了。我需要在 .aspx 文件中指定“CodeBehind”而不是“CodeFile”。
    • Steve 由于部分类的工作方式,您可以将其放在设计器中,或者将其清洁器的代码隐藏放在设计器中,但两者都可以。如果这有效,您可以投票吗?谢谢!
    【解决方案2】:

    看看这篇其他文章,我认为它解决了您的问题: The Namespace * already contains a definition for *

    我认为这些示例文件是为 ASP.Net“网站”设置的,您使用的是 ASP.Net“Web 项目”(大多数人也使用)。

    如果您查看 ASPX 文件,请将 CodeFile 更改为 CodeBehind,它应该适用于您在代码隐藏中的声明。

    页面语言="C#" AutoEventWireup="true" 代码文件="ClientCallback.aspx.cs" Inherits="ClientCallback"

    “网站”类型的项目在后面的代码中不使用声明,但几乎没有人再使用该项目类型,所以不知道他们为什么要这样设置示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2018-11-15
      • 2013-10-11
      • 1970-01-01
      相关资源
      最近更新 更多