asp.net c#生成用户控制参数 [英] asp.net c# generate user control parameters

查看:73
本文介绍了asp.net c#生成用户控制参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.net对我来说是新的,我已经交给了一个现有的项目来进行工作.我是这样写的: Asp.Net WebForms -如何将ViewData作为参数传递给用户控件,但是由于没有任何响应,我一定不很清楚.

ASP.net is new to me and I've been handed an existing project to work on. I wrote SO: Asp.Net WebForms - How to pass ViewData as param to User Control but i must not have been clear as there were no responses.

我想这样做:

Consignment.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Daff.Lae.TraceCommon.ValueObjects.NoiReproNLS.NoiNlsVO>" %>
<%@ Register TagPrefix="uc" TagName="speciesgrid" Src="~/Views/Noi/ReproNLS/SpeciesGridController.ascx" %>

<%
    var applicationId = ViewData["NoiId"];
    var applicationSpecies = ViewData["applicationSpecies"] as HashSet<string>;    // Same as the JS var applicationSpecies but from server - needed to build page
%>

...

<%
    foreach (String species in applicationSpecies)
    {
        %>
        <div id="<%=species%>_grid" style="display: none;">
            <uc:speciesgrid runat="server" species=<%=species%>/>
        </div>
    <%}
%>

但是它在<uc:speciesgrid中的<%=species%>上失败,显示为:

But it fails on <%=species%> in <uc:speciesgrid with:

{"Server tags cannot contain <% ... %> constructs."}

用户控件:

SpeciesGridController.ascx

<%@ Control Language="C#" ClassName="SpeciesGrid" %>
<%@ Import Namespace="Kendo.Mvc.UI" %>
<%@ Import Namespace="Daff.Lae.TraceCommon.ValueObjects.NoiReproNLS" %>
<%@ Import Namespace="System.Diagnostics" %>

<script runat="server">
    private IDictionary<string, object> readRouteValueDictionary = new Dictionary<string, object>();
    private int _applicationId;
    private string _species;

    public string species
    {
        set
        {
            if (!value.IsEmpty())
            {
                _species = value;
                readRouteValueDictionary.Add("species", value);
            }
        }
        get
        {
            return _species;
        }
        ...
    }
</script>

<fieldset>
    <legend><%=species%></legend>
    <div>
        <% Html.Kendo().Grid<NoiNlsConsignmentVO>()
               .Name("grdNlsConsignment"+species)
               ...
        %>
    </div>
</fieldset>

如何使<uc:speciesgrid runat="server" species=<%=species%>/>工作?

推荐答案

感谢@Tetsuya Yamamoto的评论.这是我想出的.

Thanks @Tetsuya Yamamoto for your comment. Here is what I came up with.

我认为SpeciesGridView.ascx使用代码隐藏"对象比包含<script ...块会更好.那是我玩的东西.

I think SpeciesGridView.ascx would be better with a Code Behind object rather than include a <script ... block. That is something for me to play with.

Consignment.ascx

<%
    var applicationId = ViewData["NoiId"];
    var allSpecies = ViewData["allSpecies"] as List<ListItem>;       
%>
...

<%
foreach (ListItem speciesItem in allSpecies)
{
    var species = speciesItem.Value.Replace(" ", "_");%>
    <div id="<%=species%>_grid" style="display: none;">
        <%
            Html.RenderPartial("~/Views/Noi/ReproNLS/SpeciesGridView.ascx", new SpeciesGridViewDTO( NoiId : (int) applicationId, SpeciesCode : species));
        %>
    </div>
<%}%>


SpeciesGridViewDTO.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Daff.Lae.TraceCommon.DTO.NoiReproNLS
{
    /// <summary>
    /// This DataTransferObject is for sending a succinct model to the SpeciesGridView
    /// </summary>
    [DataContract, Serializable]
    public class SpeciesGridViewDTO
    {
        [DataMember]
        public Int32 NoiId { get; set; }

        [DataMember]
        public String SpeciesCode { get; set; }

        public SpeciesGridViewDTO(int NoiId, string SpeciesCode)
        {
            this.NoiId = NoiId;
            this.SpeciesCode = SpeciesCode;
        }
    }
}


SpeciesGridView.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Daff.Lae.TraceCommon.DTO.NoiReproNLS.SpeciesGridViewDTO>"%>
<%@ Import Namespace="Daff.Lae.TraceCommon.ValueObjects" %>

<%@ Import Namespace="Kendo.Mvc.UI" %>
<%@ Import Namespace="Daff.Lae.TraceCommon.ValueObjects.NoiReproNLS" %>
<%@ Import Namespace="System.Diagnostics" %>


<%-- SpeciesGrid - render KendoGrid of NoiNlsConsignmentVO.  --%>

<script runat="server">
    private IDictionary<string, object> readRouteValueDictionary = new Dictionary<string, object>();

    protected void Page_Load(object sender, EventArgs e)
    {
        readRouteValueDictionary.Add("applicationId", Model.NoiId);
        // SpeciesCode is optional.  If not given then all species are used.
        if (! Model.SpeciesCode.IsEmpty())
        {
            readRouteValueDictionary.Add("species", Model.SpeciesCode);
        }
    }
</script>
<fieldset>
    <legend><%=Model.SpeciesCode%></legend>
    <div>

   <% Html.Kendo().Grid<NoiNlsConsignmentVO>()
              .Name("grdNlsConsignment"+Model.SpeciesCode)
              ...

这篇关于asp.net c#生成用户控制参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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