【问题标题】:Check the button event handler it is not performing the desired function?检查按钮事件处理程序是否没有执行所需的功能?
【发布时间】:2016-01-08 07:34:47
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class product_entry : System.Web.UI.Page
{
int count;
protected void Page_Load(object sender, EventArgs e)
{
    int i, j;
    for (i = 0; i < 1; i++)
    {
        TableRow tr = new TableRow();
        tr.Attributes.Add("class", "tabrow");

        for (j = 0; j <= 8; j++)
        {
            TableCell tc = new TableCell();

            if (j == 0)
            {
                tc.Controls.Add(new LiteralControl("<Button class=remove type=button>-</button>"));
            }
            if (j == 1)
            {
                tc.Attributes.Add("class", "sno");
            }
            if (j == 2 || j == 3 || j == 4 || j == 5 || j == 6 || j == 7 || j == 8)
            {
                TextBox tb = new TextBox();
                tb.Style["width"] = "98%";
                tc.Controls.Add(tb);
            }
            tr.Controls.Add(tc);
        }
        Table1.Controls.Add(tr);
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    Label2.Text = TextBox1.Text;
for (int i = 1; i <= count; i++)
{
    for (int j = 1; j <= 7; j++)
    {
        TextBox aa = (TextBox)Pnl.FindControl("textbox" + i + j);
        Response.Write(aa.Text);
    }
}
}
}

我正在尝试获取我在页面加载时动态生成并使用 jquery 克隆它们的文本框的值....

每个文本框都有一个矩阵形式的唯一 ID,例如,第一行的文本框的 ID 为 textbox11、textbox12、textbox13、textbox14 等,第二行的 textbox21、textbox22、textbox23 ......

有什么方法可以获取值.. 循环 int 按钮事件处理程序也没有执行其任务

protected void Button1_Click(object sender, EventArgs e) 
{ TextBox aa = (TextBox)Pnl.FindControl("textbox22"); 
Label2.Text = aa.Text;} 

我试过这个来检查结果,但它给出了错误

【问题讨论】:

  • 你没有在 page_load 中为文本框设置任何 id
  • 我使用 jquery 来查看文本框的 ID。每个文本框都有一个唯一 ID,例如 texttbox11、textbox12 等

标签: c#


【解决方案1】:

尝试在Page_PreInit 方法中创建动态文本框

protected void Page_PreInit(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        tb.ID = "textBox";
        tb.Text = "";
        Panel1.Controls.Add(tb);
    }

【讨论】:

  • 文本框是使用jquery一次又一次地克隆的。所以所有的文本框都不是在页面加载时创建的
  • 您可能需要为这些文本框命名并使用 Request["nameOftxtBox"] 检索其值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 2011-11-12
相关资源
最近更新 更多