how to get textbox value in placeholder on code behind?

前端 未结 3 2067
栀梦
栀梦 2021-01-25 03:11

I created some textbox and I want to get their value dynamically. Briefly, ı explain my page:

I have dropDown list has number 1 to 15.When the user select number and I

3条回答
  •  时光说笑
    2021-01-25 03:38

    Problem

    If you dynamically add controls to a page, you need to reloaded them on Page Init or Page Load.

    Otherwise, you won't be able to find them when you post back.

    ASPX

    
        
        
        
        
        
        
    
    

    Code Behind

    private int Total
    {
        get
        {
            int total;
            if (Int32.TryParse(ddlUserSelected.SelectedItem.Text, out total)) 
                return total;
            return 0;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateTextBoxes(Total);
    }
    
    protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTextBoxes(Total);
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int total = Total;
        for (int a = 1; a <= total; a++)
        {
            var textbox = PlaceHolder1.FindControl("txtDate" + a) as TextBox;
        }
    }
    
    private void CreateTextBoxes(int total)
    {
        for (int a = 1; a <= total; a++)
        {
            // Make sure we do not add same ID again
            if (PlaceHolder1.FindControl("txtDate" + a) == null)
            {
                TextBox txtDate = new TextBox();
                Label lbl = new Label();
                lbl.Text = "
    "; txtDate.Width = 70; txtDate.CssClass = "tbl"; txtDate.ID = "txtDate" + a; PlaceHolder1.Controls.Add(txtDate); PlaceHolder1.Controls.Add(lbl); } } }

提交回复
热议问题