简体   繁体   English

在asp.net c#中,如何在单击“提交”按钮后获取动态创建的控件的值

[英]in asp.net c# how to get the value of dynamically created controls after clicking submit button

In my aspx page I have a panel(generic panel). 在我的aspx页面中,有一个面板(通用面板)。 This one is populate by several panels build dynamically. 这是由几个动态构建的面板填充的。 Each inside panel contains one label (id = "lbl"+ i.ToString()) , one photo (id = "img"+ i.ToString()) and one button submit ("btn" + i.ToString()) created dynamically and populate with sql data. 每个内部面板包含一个标签(id = "lbl"+ i.ToString()) ,一张照片(id = "img"+ i.ToString())和一个按钮提交("btn" + i.ToString())动态创建并填充sql数据。 When the submit button is clicked, I would like to display the name in the label in an update panel beside the generic panel. 单击提交按钮后,我想在通用面板旁边的更新面板的标签中显示名称。 How can I do that? 我怎样才能做到这一点? Is somebody have a solution? 有人有解决办法吗? Thanks a lot for your help My aspx file 非常感谢您的帮助我的aspx文件


  
 
  
  
  
    <asp:Panel ID="Panel_Generic" runat="server" Style="margin-left:30px;margin-right:30px;">
 <div class="row">
                <div class="col-lg-12">
                    <h2 class="page-header">
                        Vente à emporter
                        <small>Restaurant</small>
                    </h2>

                    <ol class="breadcrumb">
                        <li>
                            <i class="fa fa-dashboard"></i>Vos Informations
                        </li>
                        <li>
                            <i class="fa fa-file"></i> Votre commande
                        </li>
                        <li class="active">
                            <i class="fa fa-file"></i> Valider
                        </li>
                    </ol>
                </div>
            </div>

            <div style="display:flex;justify-content:space-around;margin-bottom:30px">
                <asp:Button ID="btn_Entree" width="150px" class="btn btn-info btn-lg" runat="server" Text="Entrées" />
                <asp:Button ID="btn_Plat" width="150px" class="btn btn-warning btn-lg" runat="server" Text="Plats" />
                <asp:Button ID="btn_dessert" width="150px" class="btn btn-success btn-lg" runat="server" Text="Desserts" />
                <asp:Button ID="btn_boisson" width="150px" class="btn btn-primary btn-lg" runat="server" Text="Boissons" />
            </div>
            <asp:TextBox ID="TextBox1"  runat="server"></asp:TextBox>   
            <div>
                <asp:Panel ID="Panel_Entree" class="col-md-9" runat="server" style="display:flex;flex-wrap:wrap">
                </asp:Panel>
                <asp:UpdatePanel ID="UpdatePanel1" class="col-md-3" runat="server">
                    <ContentTemplate>
                        <div class="panel panel-danger">
                            <div class="panel-heading">A emporter</div>
                            <div class="panel-body">
                                <asp:Button ID="Button1"  class="btn btn-danger btn-lg" runat="server" Text="Valider votre commande" style="justify-content:center"/>
                                <div style="border-bottom:1px solid black;margin-top:20px"></div>
                                <div>
                                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   
                                </div>
                            </div>
                        </div>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </asp:Panel>

My code behind 我背后的代码


  
 
  
  
  
    string constr = ConfigurationManager.ConnectionStrings["restau"].ConnectionString;

            protected void Page_Load(object sender, EventArgs e)
            {
                //if (!IsPostBack)
                //{
                    BindEntree();
                //}
            }

            protected void BindEntree()
            {
                SqlConnection con = null;
                SqlCommand cmd = null;
                SqlDataAdapter sda = new SqlDataAdapter();
                DataSet ds = new DataSet();
                string strQuery = "select * from recette_restau2 where type_plat='entree' and prix_vente is not null and nbre_pers is not null and picture is not null";

                con = new SqlConnection(constr);

                try
                {
                    con.Open();

                    cmd = new SqlCommand(strQuery, con);

                    sda.SelectCommand = cmd;
                    sda.Fill(ds, "TableEntree");
                   
                    for (int i = 0; i < ds.Tables["TableEntree"].Rows.Count; i++)
                    { 
                        byte[] barrImg = (byte[])ds.Tables["TableEntree"].Rows[i][15];

                        if (barrImg != null && barrImg.Length > 0)
                        {
                            int identree = Convert.ToInt16(ds.Tables["TableEntree"].Rows[i][0]);

                            string base64String = Convert.ToBase64String(barrImg, 0, barrImg.Length);

                            System.Web.UI.HtmlControls.HtmlGenericControl divEntree = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                            divEntree.Attributes.Add("class", "panel panel-body");
                            divEntree.Style.Add("Border", "1px solid  #C7CAC9");
                            divEntree.Style.Add("BorderColor", "red");
                            divEntree.Style.Add("width", "350px");
                            divEntree.Style.Add("height", "350px");
                            divEntree.Style.Add("margin-right", "20px");
                            divEntree.Style.Add("margin-bottom", "20px");
                            divEntree.Style.Add("display", "flex");
                            divEntree.Style.Add("flex-direction", "column");
                            divEntree.Style.Add("justify-content", "space-between");

                            System.Web.UI.HtmlControls.HtmlGenericControl divInfo = new System.Web.UI.HtmlControls.HtmlGenericControl("div");

                            System.Web.UI.HtmlControls.HtmlGenericControl divTitrePrix = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                            divTitrePrix.Style.Add("display", "flex");
                            divTitrePrix.Style.Add("justify-content", "space-between");




                            Label lblTitre = new Label();
                            lblTitre.ID = "lblTitre" + i.ToString();
                            lblTitre.Style.Add("font-size", "1.5em");
                            lblTitre.Style.Add("margin-bottom", "10px");
                            lblTitre.Text = Convert.ToString(ds.Tables["TableEntree"].Rows[i][2]);

                            //Label lblObserv = new Label();
                            //lblObserv.ID = "lblObserv" + i.ToString();
                            //lblObserv.Text = Convert.ToString(ds.Tables["TableEntree"].Rows[i][16]);

                            TextBox lblPrix = new TextBox();
                            lblPrix.ID = "lblPrix" + i.ToString();
                            lblPrix.Style.Add("font-size", "1.1em");
                            lblPrix.Style.Add("margin-bottom", "10px");
                            lblPrix.Style.Add("height", "30px");
                            lblPrix.Style.Add("width", "70px");
                            lblPrix.Style.Add("color", "white");
                            lblPrix.Style.Add("background-color", "red");
                            lblPrix.Style.Add("justify-content", "center");
                            lblPrix.Style.Add("align-items", "center");
                            lblPrix.Style.Add("text-align", "center");
                            lblPrix.Attributes.Add("ReadOnly", "true");
                            lblPrix.Attributes.Add("enabled", "false");



                            lblPrix.Text = Convert.ToString(ds.Tables["TableEntree"].Rows[i][4] + " €");

                            Image ImageEntree = new Image();
                            ImageEntree.ImageUrl = "data:image/jpg;base64," + base64String;
                            ImageEntree.Style.Add("width", "50%");
                            ImageEntree.Style.Add("height", "50%");
                            ImageEntree.Style.Add("margin-right", "10px");



                            System.Web.UI.HtmlControls.HtmlGenericControl divChoix = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                            divChoix.Style.Add("display", "flex");
                            divChoix.Style.Add("justify-content", "center");
                            divChoix.Style.Add("align-items", "center");



                            System.Web.UI.HtmlControls.HtmlGenericControl divBtnMinusPlus = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                            divBtnMinusPlus.Attributes.Add("class", "input-group");
                            divBtnMinusPlus.Style.Add("margin-right", "10px");

                            System.Web.UI.WebControls.Image img1 = new System.Web.UI.WebControls.Image();
                            img1.ID = "img1" + i.ToString();
                            img1.Attributes.Add("width", "15px");
                            img1.Attributes.Add("height", "15px");
                            img1.Attributes.Add("OnClick", "img1_Click");
                            img1.ImageUrl = "Content/images/moins_rond.png";

                            System.Web.UI.WebControls.Image img2 = new System.Web.UI.WebControls.Image();
                            img2.ID = "img2" + i.ToString();
                            img2.Attributes.Add("OnClick", "img2_Click");
                            img2.ImageUrl = "Content/images/plus_rond.png";
                            img2.Attributes.Add("width", "15px");
                            img2.Attributes.Add("height", "15px");
                            
                            TextBox txtBoxNbre = new TextBox();
                            txtBoxNbre.ID = "txtBoxNbre" + i.ToString();
                            txtBoxNbre.Style.Add("width", "50px");
                            txtBoxNbre.Style.Add("text-align", "center");

                            AjaxControlToolkit.NumericUpDownExtender numericExtender = new AjaxControlToolkit.NumericUpDownExtender();
                            numericExtender.ID = "numericExtender" + i.ToString();
                            numericExtender.Width = 80;
                            numericExtender.Minimum = 1;
                            numericExtender.Maximum = 20;
                            numericExtender.TargetButtonUpID = "img2" + i.ToString();
                            numericExtender.TargetButtonDownID = "img1" + i.ToString();
                            numericExtender.TargetControlID = "txtBoxNbre" + i.ToString();




                            divBtnMinusPlus.Controls.Add(img1);
                            divBtnMinusPlus.Controls.Add(txtBoxNbre);
                            divBtnMinusPlus.Controls.Add(img2);
                            divBtnMinusPlus.Controls.Add(numericExtender);
                            
                            Button btnPrix = new Button();
                            btnPrix.ID = "btnPrix" + i.ToString();
                            //btnPrix.Attributes.Add("Class", "btn btn-danger hvr-icon-forward");
                            btnPrix.CssClass = "btn btn-danger hvr-icon-forward";
                            btnPrix.Attributes.Add("runat", "server");
                            btnPrix.Text = "Choisir";
                            btnPrix.Click += new System.EventHandler(btnPrix_Click);
                            //btnPrix.CommandArgument = "<%# Eval('id_recette') %>";

                            HiddenField hf = new HiddenField();
                            hf.ID = "hf" +i.ToString();
                            hf.Value = Convert.ToString(ds.Tables["TableEntree"].Rows[i][0]);



                            divTitrePrix.Controls.Add(lblTitre);
                            divTitrePrix.Controls.Add(lblPrix);




                            divInfo.Controls.Add(divTitrePrix);
                            //divInfo.Controls.Add(lblObserv);

                            divEntree.Controls.Add(divInfo);
                            divEntree.Controls.Add(new LiteralControl("<br />"));

                            divChoix.Controls.Add(ImageEntree);

                            divChoix.Controls.Add(divBtnMinusPlus);

                            divEntree.Controls.Add(divChoix);

                            divEntree.Controls.Add(new LiteralControl("<br />"));

                            divEntree.Controls.Add(btnPrix);

                            divEntree.Controls.Add(hf);




                            Panel_Entree.Controls.Add(divEntree);
                protected void btnPrix_Click(object sender, EventArgs e)
            {            
                                        try
                {//string s = (sender as Button).Text;
                    Button btn = (Button)sender;

                    string id = btn.CommandArgument;

                    Label1.Text = id;
                }
                catch (Exception ex)
                {
                    Label2.Text = ex.Message.ToString();

                }
                }

It should be something like 应该是这样的

    for (int i=1; i<=5; i++)
 {
   Label myLabel = new Label();

   // Set the label's Text and ID properties.
   myLabel.Text = "Label" + i.ToString();
   myLabel.ID = "Label" + i.ToString();
   PlaceHolder1.Controls.Add(myLabel);
   // Add a spacer in the form of an HTML <br /> element.
   PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
 } 

it create dynamic labels with ids label1,label2 & so on 它创建ID为label1,label2等的动态标签

if you want to create dynamic button and its click so 如果您要创建动态按钮及其点击,

 Button btn = new Button();
btn.ID = "btnSubmit";
btn.Text = "Submit";
btn.Click += new EventHandler(btnSubmit_Click);
this.Controls.Add(btn);

You will need event handler btnSubmit_Click to add click event to the button 您将需要事件处理程序btnSubmit_Click将click事件添加到按钮

hope this will help 希望这会有所帮助

I may have misunderstood your intention but I think this will do what you ask. 我可能误解了您的意图,但是我认为这可以满足您的要求。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEntree();
            }
            else
            {
                Label1.Text = TextBox1.Text;
            }

        }

* you will have to eliminate the text value of your label: *您将必须消除标签的文本值:

 <asp:Label ID="Label1" runat="server" ></asp:Label> 

Bear in mind there are several ways of doing this and this is rather crude but it will give you an idea how the postback works with update panels.. 请记住,有几种方法可以做到这一点,虽然这很粗糙,但是它将使您了解回发如何与更新面板一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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