English 中文(简体)
创建动态文本框和格外值
原标题:Create Dynamic Textbox and Get values

I want to create dynamic text box when user click on Add more link button. For this I am using this code. And I have to mention that I am using master page.

  protected void lnkAddMore_Click(object sender, EventArgs e)
  {
        if (Request.Cookies["value"] != null)
        {
              i = Convert.ToInt32(Request.Cookies["value"].Value) + 1 ;
        }
        for (int k = 1; k <= i; k++)
        {
              LiteralControl literal = new LiteralControl();
              literal.Text = "<br /><br />";
              Label newLabel = new Label();
              newLabel.Text = "Choice" + " " + k.ToString();
              newLabel.ID = "lblChoice_" + k.ToString();
              newLabel.Attributes.Add("runat", "Server");
              this.panelLabel.Controls.Add(newLabel);
              this.panelLabel.Controls.Add(literal);

              LiteralControl literal1 = new LiteralControl();
              literal1.Text = "<br /><br />";
              TextBox nexText = new TextBox();
              nexText.ID = "txtChoice_" + k.ToString();
              nexText.Attributes.Add("TextMode", "MultiLine");
              nexText.Attributes.Add("runat", "Server");
              panelTextbox.Controls.Add(nexText);
              this.panelTextbox.Controls.Add(literal1);

              Response.Cookies["value"].Value = i.ToString();
              Session["Panel"] = panelTextbox;
        }
  }



  protected void Page_Load(object sender, EventArgs e)
  {
        if (!IsPostBack)
        {
           if (Session["Panel"] != null)
                    {
                          ContentPlaceHolder content=new ContentPlaceHolder();
                          content.Controls.Add(Session["Panel"] as Panel);
                    }
        }
  }

现在,在点击提交纽顿之后,我正面临如何检索这些文本箱数据的问题,以便我能够把文本箱的数值储存到数据库。

为点击 b而写的代码 Save

  protected void btnSave_Click(object sender, EventArgs e)
  {
     if (Session["Panel"] != null)
        {
              ContentPlaceHolder content_new = new ContentPlaceHolder();
              for (int i = 1; i <= count; i++)
              {
                    strControlName = "txtChoice_" + i.ToString();

                    TextBox objTextBox =              (TextBox)content_new.FindControl(strControlName);

                    strTextBoxValues[i] = objTextBox.Text;
                    string str3 = strTextBoxValues[2];
              }
        }
  }

这部法典显示了 ob粒的错误。 错误是NullReferenceException。

• 如何写存储程序以保存上述代码的数据?

主要问题是处理参数申报,如何宣布通过价值的动态参数,从而节省动态案文箱的价值?

感谢。

问题回答

You need to find the reference of your already created ContentPlaceHolder like-

ContentPlaceHolder cnt =(ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");

之后添加动态生成的<代码>Control,载于ContentPlaceHolder

cnt.Controls.Add(Session["Panel"] as Panel);

为什么要创建新的<条码>。 内容提要 每次都提到你正在使用主人 页: 1





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签