| |

VerySource

 Forgot password?
 Register
Search
View: 933|Reply: 5

About dynamically generating textbox and dynamically obtaining testbox values

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-15 22:30:01
| Show all posts |Read mode
I use a dynamic way to automatically generate a lot of text boxes on the page
  for(int i = 1;i<100;i++)
  {
    TextBox t = new TextBox()
    t.ID = "a"+i.ToString();
    this.Page.Controls.Add(t);
  }

Use when getting
string b;
for (in i=1;i<=100;i++)
{
TextBox ck= (TextBox)this.FindControl("a"+i.ToString());
b=ck.text
}

There is an error when acquiring, and the value can be obtained when i=1 during the loop, and an error will occur when the loop continues
Unable to cast object of type'System.Web.UI.WebControls.DropDownList' to type'System.Web.UI.WebControls.TextBox'.
Reply

Use magic Report

1

Threads

60

Posts

37.00

Credits

Newbie

Rank: 1

Credits
37.00

 China

Post time: 2020-12-15 22:45:01
| Show all posts
for(int i = 1;i<100;i++)

for (in i=1;i<=100;i++)
Are these two the same? Just check it carefully
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-15 23:15:01
| Show all posts
This is the wrong number when I came up
Specifically not because of this
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-16 00:00:01
| Show all posts
Control tmp=this.FindControl("a"+i.ToString();
TextBox tmpTextBox=tmp is TextBox?(TextBox)tmp:null;
if (tmpTextBox != null)
{
...
}
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-16 05:45:01
| Show all posts
Thank you! Passed, can you explain? Why did mine just go wrong?
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-16 08:15:01
| Show all posts
Unable to cast object of type'System.Web.UI.WebControls.DropDownList' to type'System.Web.UI.WebControls.TextBox'.
Isn't it said here?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list