【问题标题】:Create Dynamic Textbox and Get values创建动态文本框并获取值
【发布时间】:2013-07-09 13:41:00
【问题描述】:

我想在用户点击添加更多链接按钮时创建动态文本框。 为此,我正在使用此代码。我不得不提到我正在使用母版页。

  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);
                    }
        }
  }

现在我遇到了如何在单击提交按钮后检索这些文本框的数据以便我可以将那里的文本框的值存储到数据库的问题。

btnSave的点击事件会写什么代码

  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];
              }
        }
  }

此代码显示 objTextBox 错误。错误是 NullReferenceException。

如何编写存储过程来保存上述代码的数据?

主要问题是处理参数声明,如何声明传递值的动态参数,以便为动态文本框保存值?

谢谢。

【问题讨论】:

    标签: asp.net c#-4.0 c#-3.0


    【解决方案1】:

    我已经在这里回答过了。

    Lost dynamically created text box values

    你可以试试这个。

    private string GetValue(string ControlID)
    {
       string[] keys = Request.Form.AllKeys;
       string value = string.Empty;
       foreach (string key in keys)
       {
         if (key.IndexOf(ControlID) >= 0)
         {
             value = Request.Form[key].ToString();
             break;
         }
       }
    
       return value;
    }
    

    然后获取值

    string txtChoice1value = GetValue("txtChoice1");
    

    【讨论】:

      【解决方案2】:

      首先,当您动态创建控件时,不需要设置“runat = sever”。 问题出在 `ContentPlaceHolder content_new = new ContentPlaceHolder();` 这一行,你创建了一个新的 ContentPlaceHolder,这意味着它没有任何控件可以找到。

      检查此页面。 How To Create TextBox Control Dynamically at Runtime

      【讨论】:

        【解决方案3】:

        你需要找到你已经创建的ContentPlaceHolderlike-的引用-

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

        然后在那个ContentPlaceHolder中添加动态创建的Control as-

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

        为什么你每次都创建一个新的ContentPlaceHolder,即使你提到你正在使用masterPage,所以必须存在一个ContentPlaceHolder..

        【讨论】:

          【解决方案4】:

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-14
            • 2012-12-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多