Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 如何保存访问动态控件的值_C#_Asp.net - Fatal编程技术网

C# 如何保存访问动态控件的值

C# 如何保存访问动态控件的值,c#,asp.net,C#,Asp.net,大家好,提前谢谢 我读了好几篇文章,但是我很困惑。问题是,我正在面板控件中动态创建一些文本框,该控件位于formview中,其中包含以下代码 protected void Muestra_Precio_Stock_BT_Click(object sender, EventArgs e) { CheckBoxList FormatosCL = (CheckBoxList)FormViewDiscos.FindControl("FormatosCL");

大家好,提前谢谢

我读了好几篇文章,但是我很困惑。问题是,我正在面板控件中动态创建一些文本框,该控件位于formview中,其中包含以下代码

    protected void Muestra_Precio_Stock_BT_Click(object sender, EventArgs e)
    {
        CheckBoxList FormatosCL = (CheckBoxList)FormViewDiscos.FindControl("FormatosCL");
        Panel Precio_Stock_PN = (Panel)FormViewDiscos.FindControl("CB_Precio_Stock_PN");

        Label NuevoPrecioLB = null;
        TextBox NuevoPrecioTB = null;
        Label NuevoStockLB = null;
        TextBox NuevoStockTB = null;

        foreach (ListItem item in FormatosCL.Items)
        {
            if(item.Selected)
            {
                NuevoPrecioLB = new Label();
                NuevoPrecioTB = new TextBox();
                NuevoStockLB = new Label();
                NuevoStockTB = new TextBox();

                NuevoPrecioLB.ID = "NuevoPrecioLB_" + FormatosCL.Items.IndexOf(item);
                NuevoPrecioLB.Text = "PRECIO " + item.Text + ": ";
                NuevoPrecioTB.ID = "NuevoPrecioTB_" + FormatosCL.Items.IndexOf(item);

                NuevoStockLB.ID = "NuevoStockLB_" + FormatosCL.Items.IndexOf(item);
                NuevoStockLB.Text = " STOCK " + item.Text + ": ";
                NuevoStockTB.ID = "NuevoStockTB_" + FormatosCL.Items.IndexOf(item);

                Precio_Stock_PN.Controls.Add(NuevoPrecioLB);
                Precio_Stock_PN.Controls.Add(NuevoPrecioTB);
                Precio_Stock_PN.Controls.Add(NuevoStockLB);
                Precio_Stock_PN.Controls.Add(NuevoStockTB);

                Precio_Stock_PN.Controls.Add(new LiteralControl("<br />"));
            }
        }
    }
当执行到达这些行时,动态创建的文本框丢失其值,始终返回0

     cmd.Parameters.AddWithValue("@IdPrecio", Convert.ToInt32(Precio.Text));
     cmd.Parameters.AddWithValue("@IdStock", Convert.ToInt32(Stock.Text));

如何保存它们的用户类型值?

您是否尝试过将Page\u Load方法中的代码放入具有

if (!IsPostBack)
{
    //
} 
如果不起作用,可以尝试的另一件事是将键入的值保存在会话变量中

我认为InsertButton在这里不重要。顺便说一下,如果在不必使用FindControl的对象上使用runat=server,服务器将能够管理它。另外,对于动态控件,您可能希望尝试不同的方法,如:GridView(删除标题,它看起来就不像网格)、DataList或ListView。ItemTemplate中包含的是动态重复/创建的对象。您可以在此处添加标签和文本框。例如:

<asp:ListView ID="repeatedStuff" runat="server"><ItemTemplate>                                                 
</ItemTemplate>
</asp:ListView>  



这种方法之后剩下的是管理绑定,以及在触发事件时如何识别它们。就像在服务器端添加textchange事件一样,如果您在事件上绑定了ID值,那么您可以请求发送者的ID,然后您就可以知道哪个是“textchanged”

谢谢你的回答,芬利88。我已经把Muestra_Precio_Stock_BT_Click代码放在页面中,就是创建控件的人,在你指定的加载页面中,但是值仍然是0,你是说InsertButton_Click的代码吗?嗯。。。好的,为了确保这一点,您可以共享页面加载事件代码。如果我正确理解了代码,当单击Muestra_Precio_Stock按钮时,您将为标签和文本框指定Null(这将删除其中的数据)。然后,如果选择了某个项目,则再次创建它们并将其添加到所选项目的文本中。这是正确的吗?好的Fenrir88,多亏了你给我的一些线索,比如把代码放在Load_Page事件中,删除控件的空赋值,嗯,还有一个,我们称之为“小细节”。。。如果你注意我插入语句的结尾,那么。。。我传递的是0,而不是文本框的值“:-O,这是我的错,对不起。但正如我之前所说,你的线索是基本的,所以再次感谢你。
<asp:ListView ID="repeatedStuff" runat="server"><ItemTemplate>                                                 
</ItemTemplate>
</asp:ListView>  
<asp:DataListID="list"
CellPadding="2"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="2"
runat="server">
<ItemTemplate>

<asp:CheckBoxID="cboxP"AutoPostBack="True"runat="server"/>
<asp:Labelrunat="server"Text='<%#Bind("Nombre")%>'ID='<%#Bind("ID")%>'></asp:Label>

</ItemTemplate>
</asp:DataList>