By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,783 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,783 IT Pros & Developers. It's quick & easy.

problem with accessing control created dynamically

P: 29
Dear All,

I am using asp.net2.0, C#, sql2005 using Visual studio 2005

Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created dropdownlistbox and label dynamically through loop and assign id as dropdownlist + no of iteration. Now my question is how I access these control as when I am accessing one of the control I am getting nullobjectreference error stating “Object reference not set to an instance of an object.”
I created control as

Foreach(listitem)
{

//…….
Label label = new Label();
DropDownList dropdownlist = new DropDownList();
//assinging id
label.ID = "label" + i.ToString() ;
dropdownlist.ID = "dropdownlist" + i.ToString() ;

}
And I am accessing it through

cmd.Parameters.Add("@para1", SqlDbType.VarChar).Value = Page.FindControl("label1").ToString() ;
cmd.Parameters.Add("@value1", SqlDbType.VarChar).Value = Page.FindControl("dropdownlist1").ToString();

and my second question is how do i access selected value in dropdownlist1(dynamically created control) Please guide me

Thanks
Jul 26 '07 #1
Share this Question
Share on Google+
4 Replies


P: 10
Dear All,

I am using asp.net2.0, C#, sql2005 using Visual studio 2005

Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created dropdownlistbox and label dynamically through loop and assign id as dropdownlist + no of iteration. Now my question is how I access these control as when I am accessing one of the control I am getting nullobjectreference error stating “Object reference not set to an instance of an object.”
I created control as

Foreach(listitem)
{

//…….
Label label = new Label();
DropDownList dropdownlist = new DropDownList();
//assinging id
label.ID = "label" + i.ToString() ;
dropdownlist.ID = "dropdownlist" + i.ToString() ;

}
And I am accessing it through

cmd.Parameters.Add("@para1", SqlDbType.VarChar).Value = Page.FindControl("label1").ToString() ;
cmd.Parameters.Add("@value1", SqlDbType.VarChar).Value = Page.FindControl("dropdownlist1").ToString();

and my second question is how do i access selected value in dropdownlist1(dynamically created control) Please guide me

Thanks

Hi
u r declare the label and Drop down in loop so when loop is over object scop is also over. if u declare label and dropdown out of loop with array then it will help u.
Jul 26 '07 #2

P: 50
Hello sir ! can u provide me with the code for creating a text box control array when a button is clicked ?
Aug 10 '07 #3

TRScheel
Expert 100+
P: 638
Hello sir ! can u provide me with the code for creating a text box control array when a button is clicked ?
What solutions have you tried? Have you considered a hidden frame that is visible once the button is clicked, then disposed of when no longer needed?
Aug 10 '07 #4

TRScheel
Expert 100+
P: 638
my second question is how do i access selected value in dropdownlist1(dynamically created control) [/b] Please guide me

Thanks

You need to cast the found control to the proper type.

Expand|Select|Wrap|Line Numbers
  1. ((DropDownList)Page.FindControl("MyDropDownList"))
Aug 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.