如何在代码后面的占位符中获取文本框值?

how to get textbox value in placeholder on code behind?(如何在代码后面的占位符中获取文本框值?)
本文介绍了如何在代码后面的占位符中获取文本框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些文本框,我想动态获取它们的值.简而言之,我解释一下我的页面:

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

我的下拉列表有数字 1 到 15.当用户选择数字时,我将文本框创建为所选数字.例如;用户选择 3,我创建 3 个文本框,用户在文本框中写一些东西.这是我的代码:aspx 侧:

I have dropDown list has number 1 to 15.When the user select number and I created textbox as selected number. for example; user select 3 and I create 3 text box and user write something in textbox. Here is My Code: aspx Side:

  <asp:DropDownList ID="ddlUserSelected" AutoPostBack="true" OnSelectedIndexChanged="ddlUserSelected_SelectedIndexChanged" runat="server">
 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
 <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>

代码背后:

    protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
    {
     for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
            {

                TextBox txtDate = new TextBox();
                Label lbl = new Label();          
                lbl.Text = "<br/>";        
                txtDate .Width = 70;
                txtDate .CssClass = "tbl";
                txtDate .ID = "txtDate" + a;                  
                PlaceHolder1.Controls.Add(txtDate);            
                PlaceHolder1.Controls.Add(lbl);

            }
}

我还有保存按钮.

protected void btnSave_Click(object sender, EventArgs e)
{

   for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
    {              
      //I want to get each textbox value               
    }

}

注意:for循环没关系(可以去掉)我的主要目的是获取文本框的值.

Note: for loop doesn't matter (can be removed) My main aim is get the text box value.

如何在 btnSave_Click 方法中获取文本框值?

How to get textbox(es) value in btnSave_Click method?

推荐答案

下面的代码会帮助你

 protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
  {
    for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
    {

        TextBox txtDate = new TextBox();
        Label lbl = new Label();
        lbl.Text = "<br/>";
        txtDate.Width = 70;
        txtDate.CssClass = "tbl";
        txtDate.ID = "txtDate" + a;
        PlaceHolder1.Controls.Add(txtDate);
        PlaceHolder1.Controls.Add(lbl);

    }
  }
  protected void btnSave_Click(object sender, EventArgs e)
  {

    for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
    {
        if(Request.Form.Get("txtDate" + a.ToString()) != null)
        {
            var str = Request.Form.Get("txtDate" + a.ToString());
        }        
    }
  }

如果您使用母版页,请使用以下代码

If you are used master page then use below code

        if (Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString()) != null)
        {
            var str = Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString());
        }

这篇关于如何在代码后面的占位符中获取文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)