Asp.Net WebForms-如何将ViewData作为参数传递给用户控件 [英] Asp.Net WebForms - How to pass ViewData as param to User Control

查看:45
本文介绍了Asp.Net WebForms-如何将ViewData作为参数传递给用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.net的新手,并且继承了要更新的应用程序.尽管遇到了需要了解一些帮助的情况,但我的情况还不错.

我有一个使用一些参数的用户控件 uc:speciesgrid : applicationId species 应该来自ViewData并作为参数传递,但是我找不到有效的语法.对其进行硬编码的调用(有效)是:

 < uc:speciesgrid runat =服务器" ID ="speciesgrid1" applicationId ="6191" species ="Dog"/> 

我尝试了<%,<%=,<%#的所有组合,但都无效.像这样:

 < uc:speciesgrid runat =服务器" ID ="speciesgrid1" applicationId ='<%AppData ["NoiId"]%>'species ="Dog"/> 

错误都一样:

{无法从其字符串表示形式'applicationData'属性的'ViewData [\"NoiId \"]'创建'System.Int32'类型的对象.}

很明显,我所包含的内容都是按字面意义解释的,而不是用服务器替代值.

我也尝试设置一个变量并使用它,但是错误是相同的.

 <%var applicationId = ViewData ["NoiId"];%> 

正在执行的操作是在用户控制器中访问 ViewData ['NoiId'] :

 受保护的空白Page_Load(对象发送者,EventArgs e){...Debug.WriteLine(在控制器中,ViewData ['NoiId']是:" + ViewData ["NoiId"]));}>在控制器中,ViewData ['NoiId']为:6191 

在我看来:

  • 用户控件最初是在应用程序启动之前在服务器上构建的.因此,为什么我不能传递任何<%参数.
  • 传递参​​数的唯一方法是通过 ViewData 和类似的机制,这些机制将在生命周期的运行时"稍后解释.

但是,如果可能的话,我更希望将参数作为参数传递.

有可能吗?还是我对以上第2段的理解正确?

解决方案

我认为您在ASP.NET中使用了 MVC 模式,并且您倾向于将参数从 View 传递给 UserControl .为此,您不能在视图页面中使用以下代码:

 < uc:speciesgrid runat =服务器" ID ="speciesgrid1" applicationId ="6191" species ="Dog"/> 

因为查看页面没有任何代码.要将参数传递给 UserControl ,您可以使用以下代码:

 <%Html.Partial(〜/Views/Home/SpeciesGridController.ascx",ViewData ["NoiId"]);%> 

Html.Partial Html.RenderPartial 用于在 ASP中加载 ascx Partial View .NET MVC .然后,您可以获取 ViewData ["NoiId"] 并将其设置为 Page_Load 中的 ascx 的变量或属性,如下所示:

I am new to ASP.net and have inherited an app to update. I'm going quite well though have struck a situation I need some help understanding.

I have a user control uc:speciesgrid that takes some arguments: applicationId and species should be from ViewData and passed as arguments, however I cannot find a syntax that works. A hardcoded call to it (that works) is:

<uc:speciesgrid runat="server" ID="speciesgrid1" applicationId="6191" species="Dog"/>

I tried all combinations of <%, <%=, <%# and none will work. So something like:

<uc:speciesgrid runat="server" ID="speciesgrid1" applicationId='<%AppData["NoiId"]%>' species="Dog"/>

The errors are all the same:

{"Cannot create an object of type 'System.Int32' from its string representa{ion 'ViewData[\"NoiId\"]' for the 'applicationId' property."}

It is obvious that whatever I've included is being interpreted literally rather than having the server substitute in the value.

I also tried setting a variable and using that but the error is the same.

<%
var applicationId = ViewData["NoiId"];
%>

What IS working is accessing ViewData['NoiId'] in the User Controller:

protected void Page_Load(object sender, EventArgs e)
{
    ...
    Debug.WriteLine("In the controller, ViewData['NoiId'] is: " + ViewData["NoiId"]);
}

> In the controller, ViewData['NoiId'] is: 6191

It would seem to me that:

  • User Controls are built initialy on the server "before" that application starts. Hence why I can't pass any <% arguments.
  • And to pass arguments the only way is through ViewData and similar mechanisms that are interpreted later in the life cycle "at run time".

However I'd prefer pass arguments as parameters if at all possible.

Is it possible? Or is my understanding in that paragraph 2 above correct?

解决方案

I think you use MVC pattern in ASP.NET and you tend to pass parameter from View to UserControl. For do it, you can not use code below in view page:

<uc:speciesgrid runat="server" ID="speciesgrid1" applicationId="6191" species="Dog"/>

Because view page has not code behind. For pass parameter to UserControl, you can use this code:

<% Html.Partial("~/Views/Home/SpeciesGridController.ascx", ViewData["NoiId"]); %>

Html.Partial or Html.RenderPartial used for load ascx or Partial View in ASP.NET MVC. Then you can get ViewData["NoiId"] and set this to variable or property of ascx in Page_Load like this:

这篇关于Asp.Net WebForms-如何将ViewData作为参数传递给用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆