开发者

How do I bind a dynamically created asp textbox to a dataset?

I have a textbox generated by parsing an xml file.

 TextBox tb = new TextBox;
 tb.ID = "MYDATA"
 Parent.Controls.Add(tb);

I then read another Xml file for the data to populate the created TextBox. I have been trying all sorts of databinging and setting the text property to a dataset, but ca开发者_开发百科nnot figure it out. If I set the text property at creation to say:

MyDataSet.Tables[0].rows[0].["MYDATA"].ToString();  

I get an error because the dataset hasn't been created and wont be until the form has been created. Am I going about this wrong? Can't I someway specify that the data to fill the textbox is coming from the dataset without already creating it?


If you're populating the DataSet in the same method that creates the TextBox, you should be able to do this:

tb.Text = MyDataSet.Tables[0].rows[0].["MYDATA"].ToString();

If you're populating the DataSet in another method after the TextBox has been created, you can use the FindControl method to search the parent container of the TextBox:

TextBox tb = Panel1.FindControl("MYDATA") as TextBox;
if (tb != null)
{
    tb.Text = MyDataSet.Tables[0].rows[0].["MYDATA"].ToString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜