c# - 在 asp .net 中从动态创建的控件标签中检索值

标签 c# asp.net

我在单击按钮时动态创建了标签控件:

protected void createDynamicLabels_Click(object sender, EventArgs e)
{
    int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label MyLabel = new Label();
        MyLabel.ID = "lb" + i.ToString();
        MyLabel.Text = "Labell: " + i.ToString();
        MyLabel.Style["Clear"] = "Both";
        MyLabel.Style["Float"] = "Left";
        MyLabel.Style["margin-left"] = "100px";

        Panel1.Controls.Add(MyLabel);
    }
}

当我试图从另一个按钮读回时,我看到 Label Control 返回 null

Label str = (Label)Panel1.FindControl("lb" + i.ToString());

不知道这里出了什么问题

protected void bReadDynValue_Click(object sender, EventArgs e)
{

  int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label str = (Label)Panel1.FindControl("lb" + i.ToString());
        lbGetText.Text = str.Text;
    }


}

最佳答案

这是每次页面加载事件的问题。每次单击任何按钮时,ASP.net 都会在每次页面加载事件时触发。

假设在这个例子中..

protected void Page_Load(object sender, EventArgs e)
{

    if(!IsPostBack)
        createDynamicLabels();
}


private void createDynamicLabels()
    {
        int n = 5;
        for (int i = 0; i < n; i++)
        {
            Label MyLabel = new Label();
            MyLabel.ID = "lb" + i.ToString();
            MyLabel.Text = "Labell: " + i.ToString();
            MyLabel.Style["Clear"] = "Both";
            MyLabel.Style["Float"] = "Left";
            MyLabel.Style["margin-left"] = "100px";

            Panel1.Controls.Add(MyLabel);


        }
    }

protected void bReadDynValue_Click(object sender, EventArgs e)
{

    int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label str = (Label)Panel1.FindControl("lb" + i.ToString());
        lbGetText.Text = str.Text;
    }


}

当 Button trigger Page 没有任何标签时,因为它是在运行时创建的。并且 Page 没有找到特定的标签。如果您尝试上面的代码,它会正常运行。

 protected void Page_Load(object sender, EventArgs e)
{

        createDynamicLabels();
}


private void createDynamicLabels()
    {
        int n = 5;
        for (int i = 0; i < n; i++)
        {
            Label MyLabel = new Label();
            MyLabel.ID = "lb" + i.ToString();
            MyLabel.Text = "Labell: " + i.ToString();
            MyLabel.Style["Clear"] = "Both";
            MyLabel.Style["Float"] = "Left";
            MyLabel.Style["margin-left"] = "100px";

            Panel1.Controls.Add(MyLabel);


        }
    }

protected void bReadDynValue_Click(object sender, EventArgs e)
{

    int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label str = (Label)Panel1.FindControl("lb" + i.ToString());
        lbGetText.Text = str.Text;
    }


}

在这个示例代码中每次都找到标签,因为每次它都可以为这个页面制作标签。

关于c# - 在 asp .net 中从动态创建的控件标签中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766007/

相关文章:

c# - Entity Framework 5 使用 SaveChanges 添加审计日志

asp.net - 在 Asp.net 页面中测试 UI?

c# - Linq 使用稍微复杂的选择列表

c# - WPF MVVM : how to bind GridViewColumn to ViewModel-Collection?

c# - Selenium 等待逻辑

c# - ASP.NET 中的静态方法

c# - Linq to Entities 按周数分组

c# - 如何进行无缝代码推送(为什么 Page._fPageLayoutChanged 很重要)?

c# - 选择特定行时禁用 gridview 刷新

ASP.NET TreeView 排序