如何将变量传递给代码隐藏中加载的 LayoutTemplate

本文关键字:隐藏 加载 LayoutTemplate 代码 变量 | 更新日期: 2023-09-27 18:34:03

我有一个"for cycle",它创建一个列表视图并加载布局模板(3次(。

我将"j"值(周期数(传递给布局模板的用户控件。

   for (j = 1; j <= 3; j++)
        {
                   ......
            lv = new ListView();
            lv.ID = "LVprogram" + j;
            lv.DataSourceID = ds.ID;
            lv.ItemPlaceholderID = "rlt$itemPlaceholder";
                        ...then I declare the ItemDataBound, LayoutCreated, ItemTemplate....

                        // I put the "j" value in a global variable (App_Code)
                        // in order to pass it to the user control:
            marmar.j = j;
                        // I load the usercontrol
            lv.LayoutTemplate = LoadTemplate("~/rassegne/rlt.ascx");
            PHprograms.Controls.Add(lv);
            lv.DataBind();

用户控件代码隐藏 (rlt.ascx.cs(:

   protected void Page_Load(object sender, EventArgs e)
    {

        if (marmar.j == 1)
            sx = "_a";
        else if (marmar.j == 2)
            sx = "_b";
                 .....

问题:marmar.j 变量总是值"3",即"for cycle"的最终值,就像用户控件在"for cycle"之后加载一样。

如果在加载模板之前它具有值"1",则在用户控件中它具有值"3"。

我不明白其中的逻辑。


非常感谢您试图帮助我。

这是 rlt.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="rlt.ascx.cs" Inherits="rlt" %>
        <div class="program rounding">
            <!-- h2 -->
            <asp:FormView id="FV" runat="server" DataSourceID="QueryShowH1" RenderOuterTable="false">
                <ItemTemplate>
                    <h2 class="program"><span class="rounding"><%# Eval(marmar.jolly) %></span></h2>
                </ItemTemplate>
            </asp:FormView>
            <ul id="itemPlaceholderContainer" runat="server" class="program">
                <asp:PlaceHolder runat="server" id="itemPlaceholder" />
            </ul>
            <m:pager ID="pager" runat="server" size="10" showcount="false" />
        </div><!-- id="program1" -->

marmar.jolly 是另一个全局变量,其值取决于"j"(在 ASPX.cs 中是确定的(m:pager 是一个嵌套的用户控件。

这是 rlt.ascx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class rlt : System.Web.UI.UserControl
{
    string sx;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (marmar.j == 1)
            sx = "_a";
        else if (marmar.j == 2)
            sx = "_b";
        pager.pagesuffix = sx;
        pager.idsuffix = marmar.j;
    }
}

以防万一,有关完整信息,以下是ASPX代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
public partial class rassegne : System.Web.UI.Page
{
    int i;
    int j;
    int pgm;
    protected int psx;
    ListView lv;
    string a1;
    string a2;
    bool autore;
    bool regista;
    bool sceneg;
    PlaceHolder phli;
    public object di;
    string[] matrice;
    AccessDataSource ds;
    string df = "~/App_Data/marmar_be.mdb";
    string pm = "IDshow";
    string fullpm;
    Panel pnc;
    HtmlGenericControl h4;
    string ept;
    string[] autori1 =
    {
        "autore",
        "regista",
        "sceneggiatore",
        "marca"
    };
    string[] autori2 =
    {
        "oautore",
        "autore",
        "oregista",
        "regista",
        "osceneggiatore",
        "sceneggiatore"
    };
    string[] q =
    {
        "program1",
        "program2",
        "tvr"
    };
    void parametrizza(AccessDataSource a)
    {
        QueryStringParameter qp = new QueryStringParameter();
        qp.Name = pm;
        qp.QueryStringField = fullpm;
        qp.Type = TypeCode.Int32;
        a.SelectParameters.Add(qp);
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Request.QueryString == null)
            Response.Redirect("default.aspx", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //marmar.incostruzione();

        fullpm = marmar.pf + pm;
        QueryShowH1.DataFile = df;
        QueryShowH1.SelectCommand = "SELECT IDrassegna, rassegna, status, inizio, fine, programma1, programma2 FROM rassegne WHERE IDrassegna = @" + pm;
        parametrizza(QueryShowH1);

        vh1();

        for (j = 1; j <= 3; j++)
        {
            ds = new AccessDataSource();
            ds.ID = "QueryProgram" + j;
            ds.DataFile = df;
            ds.CancelSelectOnNullParameter = false;
            ds.SelectCommand = File.ReadAllText(this.Server.MapPath("~/sql/" + q[j - 1] + ".sql"));
            parametrizza(ds);
            Page.Controls.Add(ds);

            lv = new ListView();
            lv.ID = "LVprogram" + j;
            lv.DataSourceID = ds.ID;
            lv.ItemPlaceholderID = "rlt$itemPlaceholder";
            if (j == 1)
            {
                lv.ItemDataBound += LVprogram1_ItemDataBound;
                lv.LayoutCreated += new EventHandler(LVProgram1_LayoutCreated);
            }
            else if (j == 2)
            {
                lv.ItemDataBound += LVprogram2_ItemDataBound;
                lv.LayoutCreated += new EventHandler(LVProgram2_LayoutCreated);
            }
            else if (j == 3)
            {
                if (Request.QueryString[fullpm] != "9")
                    lv.Visible = false;
                lv.ItemDataBound += LVprogram3_ItemDataBound;
                lv.LayoutCreated += new EventHandler(LVProgram3_LayoutCreated);
            }
            lv.ItemTemplate = LoadTemplate("~/rassegne/rit.ascx");
            if (j == 2)
                psx = j;
            else
                psx = 1;
            marmar.jolly = "programma" + psx;
            marmar.j = j;
            lv.LayoutTemplate = LoadTemplate("~/rassegne/rlt.ascx");
            lv.EmptyDataTemplate = null;
            PHprograms.Controls.Add(lv);
            lv.DataBind();

            //Response.Write(marmar.j);
            //Response.Write(marmar.jolly);
            /*
            FormView fv = (FormView)lv.FindControl("FV");
            if (fv != null)
            {
                fv.ID = "FV" + j;

                if (fv.CurrentMode == FormViewMode.ReadOnly)
                {
                    if (j == 2)
                        psx = j;
                    else
                        psx = 1;
                    di = DataBinder.GetDataItem(fv.ID);
                    marmar.jolly = (string)DataBinder.Eval(di, "programma" + psx).ToString();
                }
                fv.ItemTemplate = LoadTemplate("~/moduli/rh2.ascx");
            }

            */
        }
    }
    void vh1()
    {

        if (ShowH1.CurrentMode == FormViewMode.ReadOnly)
        {
            PlaceHolder PHH1 = (PlaceHolder)ShowH1.FindControl("PHH1");
            di = DataBinder.GetDataItem(ShowH1);
            string iniziot = (string)DataBinder.Eval(di, "inizio").ToString();
            string finet = (string)DataBinder.Eval(di, "fine").ToString();
            string rassegnat = (string)DataBinder.Eval(di, "rassegna").ToString();
            HtmlGenericControl h1 = new HtmlGenericControl("h1");
            h1.InnerHtml = rassegnat;
            PHH1.Controls.Add(h1);
            HtmlGenericControl p = new HtmlGenericControl("p");
            p.Attributes.Add("class", "year");
            PHH1.Controls.Add(p);
            Literal inizio = new Literal();
            inizio.Text = iniziot;
            p.Controls.Add(inizio);

            if (iniziot != finet)
            {
                Literal ndash = new Literal();
                ndash.Text = "&nbsp;&ndash;&nbsp;";
                p.Controls.Add(ndash);
                Literal fine = new Literal();
                p.Controls.Add(fine);
                if (!string.IsNullOrEmpty(finet as string))
                    fine.Text = finet;
                else
                    fine.Text = "in corso";
            }
        }
    }

    void vdata()
    {
        Panel pnd = new Panel();
        phli.Controls.Add(pnd);
        Literal ltd = new Literal();
        ltd.Text = (string)DataBinder.Eval(di, "data", "{0:dd/MM/yyyy}").ToString();
        pnd.Controls.Add(ltd);
    }

    void opera(string o)
    {
        Literal op = new Literal();
        op.Text = (string)DataBinder.Eval(di, o).ToString();
        h4.Controls.Add(op);
    }
    void punto(string c)
    {
        Literal punto = new Literal();
        punto.Text = ".";
        punto.Visible = !string.IsNullOrEmpty(c as string);
        h4.Controls.Add(punto);
    }

    void quo(string v)
    {
        Literal quo = new Literal();
        quo.Text = v;
        quo.Visible = !string.IsNullOrEmpty(ept as string);
        h4.Controls.Add(quo);
    }
    void titolo()
    {

        h4 = new HtmlGenericControl("h4");
        phli.Controls.Add(h4);
        if (pgm == 1)
            opera("opera");
        else if (pgm == 2)
        {
            string sst = (string)DataBinder.Eval(di, "sottoserie").ToString();
            string stagt = (string)DataBinder.Eval(di, "stagione").ToString();
            string numt = (string)DataBinder.Eval(di, "numero").ToString();
            string segt = (string)DataBinder.Eval(di, "segmento").ToString();
            ept = (string)DataBinder.Eval(di, "episodio").ToString();
            opera("opera");
            Literal ss = new Literal();
            ss.Text = "&nbsp;" + sst;
            ss.Visible = !string.IsNullOrEmpty(sst as string);
            h4.Controls.Add(ss);
            Literal hash = new Literal();
            hash.Text = "&nbsp;#";
            hash.Visible = !string.IsNullOrEmpty(numt as string);
            h4.Controls.Add(hash);
            Literal stag = new Literal();
            stag.Text = stagt;
            h4.Controls.Add(stag);
            punto(stagt);
            Literal num = new Literal();
            num.Text = numt;
            h4.Controls.Add(num);
            punto(segt);
            Literal seg = new Literal();
            seg.Text = segt;
            seg.Visible = !string.IsNullOrEmpty(segt as string);
            h4.Controls.Add(seg);
            Literal colon = new Literal();
            colon.Text = ": ";
            colon.Visible = !string.IsNullOrEmpty(ept as string);
            h4.Controls.Add(colon);
            quo("&ldquo;");
            Literal ep = new Literal();
            ep.Text = ept;
            ep.Visible = !string.IsNullOrEmpty(ept as string);
            h4.Controls.Add(ep);
            quo("&rdquo;");
        }
        else if (pgm == 3)
            opera("tvrip");
    }

    void condizioni()
    {
        if (pgm == 1)
        {
            autore = ((i == 0) || (i == 3));
            regista = (i == 1);
            sceneg = (i == 2);
        }
        else if (pgm == 2)
        {
            autore = ((i == 0) || (i == 1));
            regista = ((i == 2) || (i == 3));
            sceneg = ((i == 4) || (i == 5));
        }
    }
    void doppi(int k)
    {
        a1 = (string)DataBinder.Eval(di, matrice[k]).ToString();
        a2 = (string)DataBinder.Eval(di, matrice[k] + "2").ToString();
        condizioni();
        Literal etic = new Literal();
        etic.Visible = !string.IsNullOrEmpty(a1 as string);
        pnc.Controls.Add(etic);
        Literal primo = new Literal();
        primo.Text = a1;
        primo.Visible = !string.IsNullOrEmpty(a1 as string);
        pnc.Controls.Add(primo);
        Literal virgola = new Literal();
        virgola.Text = ", ";
        virgola.Visible = !string.IsNullOrEmpty(a2 as string);
        pnc.Controls.Add(virgola);
        Literal altri = new Literal();
        altri.Text = a2;
        pnc.Controls.Add(altri);
        marmar.acapo(a1, pnc);
        if (autore)
            etic.Text = "di ";
        if ((regista) || (pgm == 3))
            etic.Text = "Diretto da ";
        if (sceneg)
            etic.Text = "Scritto da ";
    }
    void vautori()
    {
        if (pgm == 3)
        {
            doppi(1);
        }
        else
        {
            for (i = 0; i < matrice.Length; i++)
            {
                doppi(i);
            }
        }

    }
    void corpo()
    {
        pnc = new Panel();
        phli.Controls.Add(pnc);
        if (pgm == 1)
        {
            string spt = (string)DataBinder.Eval(di, "specifiche").ToString();
            Literal sp = new Literal();
            sp.Text = spt;
            pnc.Controls.Add(sp);
            marmar.acapo(spt, pnc);
        }
        vautori();
        if (pgm != 2)
        {
            string punt = (string)DataBinder.Eval(di, "puntata").ToString();
            Literal pun = new Literal();
            pun.Text = punt;
            pnc.Controls.Add(pun);
            Literal ord = new Literal();
            ord.Text = "&ordf;";
            ord.Visible = !string.IsNullOrEmpty(punt as string);
            pnc.Controls.Add(ord);
            Literal puntata = new Literal();
            puntata.Text = " puntata";
            puntata.Visible = !string.IsNullOrEmpty(punt as string);
            pnc.Controls.Add(puntata);
        }
    }

    void popola()
    {
        vdata();
        titolo();
        corpo();
    }


    protected void LVprogram1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        pgm = 1;
        matrice = autori1;
        marmar.lvdi = (ListViewDataItem)e.Item;
        phli = (PlaceHolder)e.Item.Controls[0].FindControl("PHli");
        di = e.Item.DataItem;
        popola();
    }
    protected void LVprogram2_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        pgm = 2;
        matrice = autori2;
        marmar.lvdi = (ListViewDataItem)e.Item;
        phli = (PlaceHolder)e.Item.Controls[0].FindControl("PHli");
        di = e.Item.DataItem;
        popola();
    }
    protected void LVprogram3_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        pgm = 3;
        matrice = autori1;
        marmar.lvdi = (ListViewDataItem)e.Item;
        phli = (PlaceHolder)e.Item.Controls[0].FindControl("PHli");
        di = e.Item.DataItem;
        popola();
    }
    void layout()
    {
        //remove the layout template
        lv.Controls.RemoveAt(0);

        //recreate it
        Control newLayoutContainer = new Control();
        lv.LayoutTemplate.InstantiateIn(newLayoutContainer);
        var userControl = newLayoutContainer.Controls[0];
        userControl.ID = "rlt";
        lv.Controls.Add(newLayoutContainer);
    }

    void LVProgram1_LayoutCreated(object sender, EventArgs e)
    {
        layout();
    }
    void LVProgram2_LayoutCreated(object sender, EventArgs e)
    {
        layout();
    }
    void LVProgram3_LayoutCreated(object sender, EventArgs e)
    {
        layout();
    }
}

ASPX 页:

<%@ Page Title="Rassegne" Language="C#" MasterPageFile="~/marmar.master" AutoEventWireup="true" CodeFile="rassegne.aspx.cs" Inherits="rassegne" %>


<asp:Content ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        p.year
        {
            margin: 0 0 30px 0;
            font-size:1.2em;
            font-weight:bold;
            text-align:center;
        }
        /* INTEGRARE */
        div.program
        {
            max-width:500px;
            margin: 0 auto 50px auto;
            padding:20px;
            background-color:#DDD9C4;
        }

        h2.program
        {
            margin: 0 0 20px 0;
            font-size:larger;
            font-weight:bold;
            text-align:left;
        }
        h2.program span
        {
            display:inline-block;
            padding:5px;
            background-color:orange;
        }
        ul.program li
        {
            padding: 10px 20px 10px 20px;
            border: 1px solid white;
            border-bottom:0;
        }
        ul.program > li:last-child {border-bottom: 1px solid white;}
        li.even {background-color:#FFFF8B;}
        li.odd {background-color:#EDE080;}
        h4 {margin:0;}
    </style>
</asp:Content>
<asp:Content ContentPlaceHolderId="content" runat="server">
    <script>
        window.onload = showsubnav('subnav2');
    </script>


    <!-- h1 -->
    <asp:AccessDataSource
        ID="QueryShowH1"
        runat="server"
        CancelSelectOnNullParameter="False">
    </asp:AccessDataSource>
    <asp:FormView runat="server" id="ShowH1" DataSourceID="QueryShowH1" RenderOuterTable="false">
        <ItemTemplate>
            <asp:PlaceHolder ID="PHH1" runat="server" />
        </ItemTemplate>
    </asp:FormView>


    <asp:PlaceHolder ID="PHprograms" runat="server" />


    <!-- programma1 -->



    <!-- programma2 -->



    <!-- TVR -->


</asp:Content>

顺便问一下,在 aspx.cx 如何设置由 LoadTemplate 加载的用户控件的属性?

再次感谢,米.

如何将变量传递给代码隐藏中加载的 LayoutTemplate

这是因为,当您调用 lv.DataBind(( 时,Page_Load用户控件仍然不会被调用。对此方法的调用发生在页面生命周期的后期阶段。

您需要做的是找到另一种将值传递给用户控件的方法,而不是使用全局变量。例如,如果可以在用户控件代码隐藏中引入属性并调用

userControl.SomeProperty = j; // instead of marmar.j = j;

打电话前

lv.DataBind();

用户控件将正确呈现。我无法提供确切有效的代码,因为我不知道您的完整用户控件代码隐藏。如果您发布了 rlt.ascx.cs 的完整代码,这将很有帮助。