是否可以将WebMethod放在ascx.cs文件中(对于UserControl),然后从客户端jQuery代码调用它?
出于某些原因,我无法将WebMethod代码放在.asmx或.aspx文件中.
示例:在ArticleList.ascx.cs中,我有以下代码:
[WebMethod]
public static string HelloWorld()
{
return "helloWorld";
}
Run Code Online (Sandbox Code Playgroud)
在ArticleList.ascx文件中,我可以调用WebMethod,如下所示:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
dataFilter: function(data)//makes it work with 2.0 or 3.5 .net
{
var msg;
if (typeof (JSON) !== 'undefined' &&
typeof (JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
url: "ArticleList.ascx/HelloWorld",
success: function(msg) {
alert(msg);
}
});
Run Code Online (Sandbox Code Playgroud)
而firebug的错误是:
<html>
<head>
<title>This type of page is not served.</title> …Run Code Online (Sandbox Code Playgroud) 有没有办法在外部JavaScript文件中使用"<%= someObject.ClientID%>"?
如果我使用代码
<%= someObject.ClientID %>
Run Code Online (Sandbox Code Playgroud)
在我的as(c/p)x页面的脚本标签中,它工作正常.在呈现的页面上,ClientID已解析.但是,如果我输入外部JS文件并添加:
它没有.有没有办法做到这一点,还是我坚持将该代码留在as(c/p)x文件中?
附带问题 - 在您的标记文件中执行<%= ...%>的行为是什么?
我正在使用C#和Ajax开发一个ASP.NET应用程序.
我有一个页面,用于保存动态加载的用户控件.我需要将一些数据(整数值和一些字符串)传递给动态加载的用户控件.
现在我使用Session来传递这些值,但我想我可以用另一种方式; 类似于VIEWSTATE或隐藏的输入.
你推荐我什么?
更新:
我动态加载控件这一事实非常重要,因为每次回发都会加载控件,而且我无法在控件上存储任何值.
尝试在Visual Studio 2010中构建网站项目时,我收到以下错误:
页面'/WebSite/controls/C2.ascx'不能使用用户控件'/WebSite/controls/C1.ascx',因为它在web.config中注册并与页面位于同一目录中.
我有2个Web用户控件:
controls/C1.ascx
controls/C2.ascx
Run Code Online (Sandbox Code Playgroud)
控件已在web.config中注册:
<configuration>
<system.web>
<pages>
<controls>
<add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
<add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>
</controls>
</pages>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
C1.ascx只包含一个静态HTML,C2.ascx试图包含C1:
C1.ascx只包含一些简单的静态简单HTML.C2.ascx试图包含C1.ascx:
<%@ Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>
Run Code Online (Sandbox Code Playgroud)
在尝试构建项目时,我收到顶部的错误消息.我意识到可以通过向C2.ascx添加另一个Register指令来修复此问题...:
<%@ Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>
Run Code Online (Sandbox Code Playgroud)
...但我想知道是否有更清洁的解决方案,为什么我首先得到错误?
我正在尝试实现一个使用WebForm ascx用户控件(非MVC)的MVC Razor _Layout.cshtml页面.我这是根据Scott Hansleman的文章"使用ASP.NET MVC 3混合Razor视图和WebForms母版页"的"是"部分进行的.http ://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
文章说明如何在webform Site.Master页面以及MVC Razor _Layout页面中使用相同的ascx用户控件.
从我在Stackoverflow上的其他地方读到的内容,可以在MVC页面中使用传统的ascx用户控件(以及ASP.NET webform服务器控件).使用以下行应该在我的Razor _Layout中呈现ascx用户控件:
@{ Html.RenderPartial("~/UserControls/WebUserControl1.ascx"); }
Run Code Online (Sandbox Code Playgroud)
但是,这会引发错误:
The view at '~/UserControls/WebUserControl1.ascx' must derive from ViewPage,
ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.
Run Code Online (Sandbox Code Playgroud)
我也试过以下类似的结果:
@Html.Partial("~/UserControls/WebUserControl1.ascx")
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
使用MVC3中的Razor视图引擎,
是否有可能呈现传统的ascx?
我期待能够做到这样的事情:
@Html.RenderPartial("Footer.ascx")
Run Code Online (Sandbox Code Playgroud) 我在ASP.NET中有一个自定义控件(后面的代码中的VB.NET),用ASCX定义:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="Mynamespace.Controls.MyControl" %>
<!-- some html and other custom controls-->
Run Code Online (Sandbox Code Playgroud)
在代码背后:
Namespace Controls
Public Class MyControl
Inherits System.Web.UI.UserControl
Run Code Online (Sandbox Code Playgroud)
这是在库中设置的.另一个项目在页面中使用该控件:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="mypage.aspx.vb"
Inherits="myproject.mypage" culture="auto" meta:resourcekey="Page" uiculture="auto"
Transaction="RequiresNew" MasterPageFile="Mynamespace.Master"
Theme="ThemeBase2" StylesheetTheme="ThemeBase2" %>
<%@ Register tagprefix="Controls" tagname="MyControl" src="../Controls/MyControl.ascx" %>
<%-- some asp.net --%>
<Controls:MyControl ID="mycontrol1" runat="server"
MyCustomProperty="value" />
Run Code Online (Sandbox Code Playgroud)
但是,当我构建时,我得到一个错误说
'MyCustomProperty'不是'System.Web.UI.UserControl'的成员.
在designer.vb页面中,我看到:
Protected WithEvents mycontrol1 As Global.System.Web.UI.UserControl
Run Code Online (Sandbox Code Playgroud)
我如何确保它成为:
Protected WithEvents mycontrol1 As Global.Mynamespace.Controls.MyControl
Run Code Online (Sandbox Code Playgroud)
?
有什么办法可以使用C#加载然后"渲染"ascx控件吗?
本质上我试图用C#函数替换内联ASP,它将返回相同的HTML.然后,我可以将其设置为webmethod,以便我可以使用生成原始html的相同代码使用jQuery更新页面的该部分.
我真的需要一些方法来做这件事,这似乎是一条合乎逻辑的路线.
我有一个用户控件,如果执行某个操作,我想重定向到用户所在的页面,并带有一些额外的查询字符串参数.
所以,如果UserControl.ascx在Home.aspx上,我想重定向到Home.aspx?action = true,如果UserControl.ascx在Profile.aspx上,我想重定向到Profile.aspx?action = true
基本上,在我的UserControl.ascx.cs中,我想获取父页面的URL.我怎么才能得到它?
嗨,我想if condition在.ascx文件中使用.如下所示:
<%= if (value.equals("xyz")) {} %>
Run Code Online (Sandbox Code Playgroud)
如上所示,如果我这样使用.然后我得到" invalid expression if"的错误.
请指导我.
ascx ×10
asp.net ×7
c# ×3
asp.net-ajax ×1
asp.net-mvc ×1
clientid ×1
if-statement ×1
javascript ×1
jquery ×1
razor ×1
vb.net ×1
webforms ×1
webmethod ×1