The datalist multiplies the components that are inside of it , so if the table we want to show the data haves 10 rows then all the components in side of the datalist are multiplied by 10! The asp.net framework
creates a unique name for every component.
If you want to see whate is going on behind the scene , just enable trace = true to your page !
You will see that all of your components have a unique name
DataList3$ctl00$TextBox1
DataList3$ctl01$TextBox1
If u use this code will rise a null exception
String str = ((TextBox)(DataList3.FindControl(“TextBox1”))).Text;
But if u use this one , it works fine
String str = ((TextBox)(DataList3.FindControl(“ctl00$TextBox1”))).Text;
So …. You can’t know in what datalist item you are (ctl00, ctl01)
string info = e.Item.UniqueID.ToString();
string[] arInfo =
newstring[2];
char[] splitter = {
'$' };
arInfo = info.Split(splitter);
string DlUniqueName = arInfo[1];
//
I take the full unique name
DataList3$ctl00 and split it
string newTextBox1 = DlUniqueName+”$TextBox1”;
sting result =
((TextBox)(DataList3.FindControl(newTextBox1))).Text;
As somebody who is relatively new to databinding and its practices, I just want to put out there that info like this is great to have - and having it, it will be much less likely that I will develop
bad habits like misusing FindControl (which, incidentally, I learned about only a month or two ago). So thanks, and keep it up.
The datalist multiplies the components that are inside of it , so if the table we want to show the data haves 10 rows then all the components in side of the datalist are multiplied by 10! The asp.net framework creates a unique name
for every component.
If you want to see whate is going on behind the scene , just enable trace = true to your page !
You will see that all of your components have a unique name
DataList3$ctl00$TextBox1
DataList3$ctl01$TextBox1
If u use this code will rise a null exception
String str = ((TextBox)(DataList3.FindControl(“TextBox1”))).Text;
But if u use this one , it works fine
String str = ((TextBox)(DataList3.FindControl(“ctl00$TextBox1”))).Text;
So …. You can’t know in what datalist item you are (ctl00, ctl01)
string info = e.Item.UniqueID.ToString();
string[] arInfo =
newstring[2];
char[] splitter = {
'$' };
arInfo = info.Split(splitter);
string DlUniqueName = arInfo[1];
//
I take the full unique name
DataList3$ctl00 and split it
string newTextBox1 = DlUniqueName+”$TextBox1”;
sting result =
((TextBox)(DataList3.FindControl(newTextBox1))).Text;
Thanks
Nick Sp
Sorry for my english
This is the kind of code that should be discouraged. Supposed we changed the id separator in the next version, now your code blows up.
The correct way would be:
TextBox textBox = DataList3.Item.FindControl("TextBox1");
yes its correct tha if the id separator changes , but
sting result =
((TextBox)(DataList3.FindControl("TextBox1"))).Text; does not work it rise a null exception , because there is no such component named TextBox1 in the datalist, the textbox1 name is know
ctl00$TextBox1.
so maybe my code is not a good way to programm , yes im new in asp.net development !!! but really show me the right way!!!!!!!!!!!!!!!!!!!!!!!!!!!!! a lot of theorys but i see no code that realy works in your post!
yes its correct tha if the id separator changes , but
sting result =
((TextBox)(DataList3.FindControl("TextBox1"))).Text; does not work it rise a null exception , because there is no such component named TextBox1 in the datalist, the textbox1 name is know
ctl00$TextBox1.
so maybe my code is not a good way to programm , yes im new in asp.net development !!! but really show me the right way!!!!!!!!!!!!!!!!!!!!!!!!!!!!! a lot of theorys but i see no code that realy works in your post!
thanks
In my last post i wrote: TextBox textBox = (TextBox)DataList3.Item.FindControl("TextBox1");
If you really think logically about it (forget how find control works for now) the DataList will have a repeated TextBox1 for each row of data so what would you expect DataList.FindControl to return?
Sorry I didn’t read your post correctly! I didn’t see the item at your line of code!!!!
I will fix my code in soon of possible. For know I will leaved because I have a dead line for this project.
snikos
Member
14 Points
8 Posts
Re: Getting Data out of your DataControls
Jan 21, 2009 10:07 AM|LINK
i need more information about stepping through your code !
thanks
snikos
Member
14 Points
8 Posts
Re: Getting Data out of your DataControls
Jan 22, 2009 08:52 AM|LINK
The datalist multiplies the components that are inside of it , so if the table we want to show the data haves 10 rows then all the components in side of the datalist are multiplied by 10! The asp.net framework creates a unique name for every component.
If you want to see whate is going on behind the scene , just enable trace = true to your page !
You will see that all of your components have a unique name
DataList3$ctl00$TextBox1
DataList3$ctl01$TextBox1
If u use this code will rise a null exception
String str = ((TextBox)(DataList3.FindControl(“TextBox1”))).Text;
But if u use this one , it works fine
String str = ((TextBox)(DataList3.FindControl(“ctl00$TextBox1”))).Text;
So …. You can’t know in what datalist item you are (ctl00, ctl01)
string info = e.Item.UniqueID.ToString();
string[] arInfo = new string[2];
char[] splitter = { '$' };
arInfo = info.Split(splitter);
string DlUniqueName = arInfo[1];
// I take the full unique name DataList3$ctl00 and split it
string newTextBox1 = DlUniqueName+”$TextBox1”;
sting result = ((TextBox)(DataList3.FindControl(newTextBox1))).Text;
Thanks
Nick Sp
Sorry for my english
Chronofied
Member
22 Points
27 Posts
Re: Getting Data out of your DataControls
Jan 22, 2009 04:47 PM|LINK
As somebody who is relatively new to databinding and its practices, I just want to put out there that info like this is great to have - and having it, it will be much less likely that I will develop bad habits like misusing FindControl (which, incidentally, I learned about only a month or two ago). So thanks, and keep it up.
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Getting Data out of your DataControls
Jan 22, 2009 06:12 PM|LINK
This is the kind of code that should be discouraged. Supposed we changed the id separator in the next version, now your code blows up.
The correct way would be:
TextBox textBox = DataList3.Item.FindControl("TextBox1");
Senior SDE, ASP.NET Team, Microsoft
snikos
Member
14 Points
8 Posts
Re: Getting Data out of your DataControls
Jan 23, 2009 07:07 AM|LINK
yes its correct tha if the id separator changes , but
sting result = ((TextBox)(DataList3.FindControl("TextBox1"))).Text; does not work it rise a null exception , because there is no such component named TextBox1 in the datalist, the textbox1 name is know ctl00$TextBox1.
so maybe my code is not a good way to programm , yes im new in asp.net development !!! but really show me the right way!!!!!!!!!!!!!!!!!!!!!!!!!!!!! a lot of theorys but i see no code that realy works in your post!
thanks
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Getting Data out of your DataControls
Jan 23, 2009 03:27 PM|LINK
In my last post i wrote:
TextBox textBox = (TextBox)DataList3.Item.FindControl("TextBox1");
where as you have
TextBox textBox = ((TextBox)(DataList3.FindControl("TextBox1")));
If you really think logically about it (forget how find control works for now) the DataList will have a repeated TextBox1 for each row of data so what would you expect DataList.FindControl to return?
Senior SDE, ASP.NET Team, Microsoft
snikos
Member
14 Points
8 Posts
Re: Getting Data out of your DataControls
Jan 23, 2009 06:55 PM|LINK
I will fix my code in soon of possible. For know I will leaved because I have a dead line for this project.
Thanks!
engineerachu
Contributor
3077 Points
850 Posts
Re: Getting Data out of your DataControls
Feb 17, 2009 11:09 AM|LINK
Couldn't understand the topic. Can you post with a full example?
Achutha Krishnan
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Getting Data out of your DataControls
Feb 17, 2009 04:28 PM|LINK
Did you read the blog post?
Senior SDE, ASP.NET Team, Microsoft
engineerachu
Contributor
3077 Points
850 Posts
Re: Getting Data out of your DataControls
Feb 18, 2009 08:10 AM|LINK
Yeah, I read. How to can and where to call method you've written for IDictionary?
public static IDictionary GetValues(GridViewRow row) { IOrderedDictionary values = new OrderedDictionary(); foreach (DataControlFieldCell cell in row.Cells) { if (cell.Visible) { // Extract values from the cell cell.ContainingField.ExtractValuesFromCell(values, cell, row.RowState, true); } } return values; }Achutha Krishnan