Dynamisk textboxes HJÆLP!:-)
Hej alle sammen.. :-)Jeg har den her kode:
------------------------------------------------------------
protected void Tracks_SelectedIndexChanged(object sender, EventArgs e)
{
int numlabels = System.Convert.ToInt32(Tracks.SelectedItem.Text);
for (int i = 1; i <= numlabels; i++)
{
TextBox TrackBox = new TextBox();
TrackBox.ID = "TrackID" + i.ToString();
TextBox ArtistBox = new TextBox();
ArtistBox.ID = "ArtistID" + i.ToString();
PlaceHolder1.Controls.Add(new LiteralControl(i.ToString() + ". "));
PlaceHolder1.Controls.Add(ArtistBox);
PlaceHolder1.Controls.Add(new LiteralControl(" : "));
PlaceHolder1.Controls.Add(TrackBox);
PlaceHolder1.Controls.Add(new LiteralControl("<br>"));
}
}
protected void AlbumSubmit_Click(object sender, EventArgs e)
{
int numlabels = System.Convert.ToInt32(Tracks.SelectedItem.Text);
for (int i = 1; i <= numlabels; i++)
{
TextBox tBox = (TextBox)this.FindControl("TrackID" + i.ToString());
Response.Write("TrackID" + i.ToString() + " : " + tBox.Text + "<br>");
}
}
------------------------------------------------------------
Men når jeg prøver at køre mit script siger den:
------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
------------------------------------------------------------
Line 72: {
Line 73: TextBox tBox = (TextBox)this.FindControl("TrackID" + i.ToString());
Line 74: Response.Write("TrackID" + i.ToString() + " : " + tBox.Text + "<br>");
Line 75: }
Line 76: }