简体   繁体   中英

asp.net c# generate user control parameters

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.

I want to do this:

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>
    <%}
%>

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

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

The User Control:

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>

How does one get the <uc:speciesgrid runat="server" species=<%=species%>/> to work?

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

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)
              ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM