Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将变量传递给codebehind中加载的LayoutTemplate_C#_Asp.net_Listview_Variables_User Controls - Fatal编程技术网

C# 如何将变量传递给codebehind中加载的LayoutTemplate

C# 如何将变量传递给codebehind中加载的LayoutTemplate,c#,asp.net,listview,variables,user-controls,C#,Asp.net,Listview,Variables,User Controls,我有一个for循环,它创建一个ListView并加载LayoutTemplate 3次 我将周期数的j值传递给LayoutTemplate的usercontrol for (j = 1; j <= 3; j++) { ...... lv = new ListView(); lv.ID = "LVprogram" + j; lv.DataSourceID = d

我有一个for循环,它创建一个ListView并加载LayoutTemplate 3次

我将周期数的j值传递给LayoutTemplate的usercontrol

   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();
问题是:marmar.j变量的值始终为3,即for循环的最终值,就像usercontrol在for循环之后加载一样

如果在加载模板之前它的值为1,则在usercontrol中它的值为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" -->
以防万一,完整信息请参见以下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加载的usercontrol的属性

再次非常感谢,
M.

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

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

userControl.SomeProperty = j; // instead of marmar.j = j;
打电话之前

lv.DataBind();
您的用户控件将正确呈现。我不能提供一个准确的工作代码,因为我不知道你的完全用户控制代码隐藏。如果您发布了rlt.ascx.cs的完整代码,这将非常有帮助。

如果您不理解它的逻辑,您将如何理解答案?此外,给出的代码是不完整的。
userControl.SomeProperty = j; // instead of marmar.j = j;
lv.DataBind();