Is it more simpler to use FindControl?
We do have methods to access rowIndex in every GridView event, right?( even if we have no direct methods in some cases)
Also, Is there a need to get values of controls in all rows when almost always we want only the values of a particular row?
Regards,
Naveen Jose ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
I would argue that is not the case. Most of the time you
do want to get all of the values of the row to do an update, delete or some other function. And like I said earler in this post:
Supposed your using bound fields and turned HtmlEncode and other formatting options on for your cell, do you want to use FindControl and have to figure out what sequence of methods to call to get the unformatted data back? Why not call
ExtractValuesFromCell which knows how to do that?
This has nothing to do with extracting values from all of the rows and nowhere in this article does it say that. The blog post mimics what the controls does internally to extract values from a single row when using a datasource control.
So let me see if I understand what this blog entry says: Data controls are designed stupidly and are not intuitive. The logical way almost everyone expects data controls to work fails and looping by row and/or cell returns a null reference. The solution
you provide is essentially to manually build data dictionaries for each field(column) and then use the dictionaries to get the entered data. The dictionaries have to be tied to specific events. If this isn't correct please explain where I'm off.
The "helper" that you wrote is interesting but like most information from Microsoft or it's employees, assumes knowledge on the part of the reader that frequently does not exist. An actual real world example to put it in context would have made this quite
a bit more useful. Maybe a link to an explanation of how the data controls work and why the most intuitive way of getting the data simply doesn't work would help. Do we need to add your helper to our code manually and if so where and how do we trigger it?
I have a gridview with multiple rows and multiple columns. Each cell has a textbox. When the data has been entered for all of the rows there is a button to click to submit the data back to the server. How then would your helper help? How does this 1
button get all of that data into the data dictionaries and then extract that data from the dictionaries so it can be used?
In no way did I state that data controls are designed stupidly, I don't have all the answers but I am merely providing alternatives from the POV of someone that works with/on these controls daily. Since the birth of
reflector, developers have been able to "spy" on some of the internals, and what I am suggesting could have been done by anyone. Let me make it clear that I'm trying to show the difference when
you use data source controls vs binding directly to the DataSource property, and what happens internally with the data controls in each case.
What I see everyday on these forums are bad practices and I'm trying to rectify some of those by shedding a little light on how things work behind the scenes. If you would like me to post more on these I can if that helps you grasp my point.
kraznodar
The solution you provide is essentially to manually build data dictionaries for each field(column) and then use the dictionaries to get the entered data. The dictionaries have to be tied to specific events. If this isn't correct please explain where I'm off.
Thats what the GridView does internally when you use a data source control.
kraznodar
The dictionaries have to be tied to specific events
Not sure what you mean by this.
kraznodar
Do we need to add your helper to our code manually and if so where and how do we trigger it?
You'd have to manually add the helper. You'd use it one the gridview row when you want to perform an operation(i.e delete, update, insert or a custom command).
kraznodar
I have a gridview with multiple rows and multiple columns. Each cell has a textbox. When the data has been entered for all of the rows there is a button to click to submit the data back to the server. How then would your helper help? How does this 1 button
get all of that data into the data dictionaries and then extract that data from the dictionaries so it can be used?
You'd have to manually loop over all your rows and call the helper for each row. The helper method extracts values from each cell using a cleverly named method
ExtractValuesFromCell. For each row you should get the values that you expect. Are you using bound fields to do this btw?
Hope this helps
I'll post something in the future about DataControlField's and how they extract their data.
Thank you very much for your answer. I apologize for the rather noxious choice of terminology. I'm kind of disgruntled because I found a tutorial on a different site and used it with my gridview. Your post shows that exactly what they did is completely
wrong. I didn't find out it was wrong until after doing a couple of days of work to get to where I could do a preliminary test. The null reference error was not amusing. I did a search and until I found your post they all said to do the same wrong thing
so I was already pretty frustrated before I found your post.
Your response to my post answers all of my important questions. Only the first column is bound but your blog entry deals with manually binding so I know what to do. I need to manually make a dictionary for each set of textboxes and bind them. Not a big
deal since you show how to do it step by step.
I do have a real complaint about Microsoft documentation. Clearly you aren't the person that needs to hear why my coworkers and I aren't overly impressed with Microsoft documentation. Some of it is great but for more advanced things like the gridview it
tends to skip a lot of detail or be misleading. Either that or it is in engineer speak that is hard to decipher. I read quite a bit about how to use the gridview on MSDN and it was implied that we could use the gridview this way. Never said outright but
it was implied. Is there a contact that we can whine to about this and not waste any more of your time?
It is always good to eliminate bad practices and I hope I wasn't discouraging towards your doing that. It is a big task though. I just read a CERT list of bad programming practices and about half the list is things I was taught in class. One other question
though: Why do you get that cold shiver when people use the FindControl in a gridview? I accept that it isn't good but what is the risk?
Thanks for your time!
PS. You should get a raise for dealing with jerks like me.
One other question though: Why do you get that cold shiver when people use the FindControl in a gridview? I accept that it isn't good but what is the risk?
It's not the use of FindControl that makes my spine tingle its the MISuse and ABuse of it that does. FindControl is a good method and very useful, but look at the name of method,
FindControl. Everything has a purpose right? If I'm trying to find a control then i'll call find control. If I'm trying to get data out of a control I might call find control or some other API that is better at getting
data, i.e ExtractRowValues. I think that there is alot of knowledge out there, good and bad, about using these data controls and I just want to try to set some things straight by shedding some more light on
how stuff works (I love that website).
Before I go lemme show you what I mean by picking the right API. Lets make a new control IntControl. IntControl has a method
int GetValue(). Now OO principals tell us to not break the abstraction by letting consumers grab at the internals but IntControl derives from control, and by default, there is alot of other things exposed to the consumer that shouldn't be.
Now we know that IntControl uses a textbox internally so I could never call GetValue and instead write:
int value;
Int32.TryParse(((TextBox)IntControl.Controls[0]).Text, out value);
Everything will work fine until the author of IntControl decides to use a div instead of a textbox.
The REAL lesson to take away from the blog post is to try your best to follow the contract provided by a class or an interface (in this case the DataControl).
I am building a website that shows data from a database ! I use the datalist component so I can show the data In a formatted way. My problem is that in the datalist I don’t show only data but I have an image button that when you press it , it shows you a reservation
form using ajax (ModalpopupExtender). In the reservation form there are text components that I want to get there data.
davidfowl
Contributor
2683 Points
608 Posts
Microsoft
Re: Getting Data out of your DataControls
Dec 31, 2008 06:28 PM|LINK
Did you read the blog post?
Senior SDE, ASP.NET Team, Microsoft
naveenj
Contributor
6164 Points
1130 Posts
Re: Getting Data out of your DataControls
Jan 13, 2009 03:08 PM|LINK
Hi davidfowl,
Forgive me if this sounds too stupid.
Is it more simpler to use FindControl?
We do have methods to access rowIndex in every GridView event, right?( even if we have no direct methods in some cases)
Also, Is there a need to get values of controls in all rows when almost always we want only the values of a particular row?
Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
davidfowl
Contributor
2683 Points
608 Posts
Microsoft
Re: Getting Data out of your DataControls
Jan 13, 2009 05:14 PM|LINK
I would argue that is not the case. Most of the time you do want to get all of the values of the row to do an update, delete or some other function. And like I said earler in this post:
Supposed your using bound fields and turned HtmlEncode and other formatting options on for your cell, do you want to use FindControl and have to figure out what sequence of methods to call to get the unformatted data back? Why not call ExtractValuesFromCell which knows how to do that?
This has nothing to do with extracting values from all of the rows and nowhere in this article does it say that. The blog post mimics what the controls does internally to extract values from a single row when using a datasource control.
Senior SDE, ASP.NET Team, Microsoft
naveenj
Contributor
6164 Points
1130 Posts
Re: Getting Data out of your DataControls
Jan 14, 2009 06:05 AM|LINK
Hi davidfowl,
And I wont argue with one who is atleast ten times smarter than me at a technology [:)]
I was merely enquiring if it was simpler the traditional way.
Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
kraznodar
Contributor
3332 Points
881 Posts
Re: Getting Data out of your DataControls
Jan 19, 2009 05:24 PM|LINK
So let me see if I understand what this blog entry says: Data controls are designed stupidly and are not intuitive. The logical way almost everyone expects data controls to work fails and looping by row and/or cell returns a null reference. The solution you provide is essentially to manually build data dictionaries for each field(column) and then use the dictionaries to get the entered data. The dictionaries have to be tied to specific events. If this isn't correct please explain where I'm off.
The "helper" that you wrote is interesting but like most information from Microsoft or it's employees, assumes knowledge on the part of the reader that frequently does not exist. An actual real world example to put it in context would have made this quite a bit more useful. Maybe a link to an explanation of how the data controls work and why the most intuitive way of getting the data simply doesn't work would help. Do we need to add your helper to our code manually and if so where and how do we trigger it?
I have a gridview with multiple rows and multiple columns. Each cell has a textbox. When the data has been entered for all of the rows there is a button to click to submit the data back to the server. How then would your helper help? How does this 1 button get all of that data into the data dictionaries and then extract that data from the dictionaries so it can be used?
davidfowl
Contributor
2683 Points
608 Posts
Microsoft
Re: Getting Data out of your DataControls
Jan 19, 2009 06:05 PM|LINK
In no way did I state that data controls are designed stupidly, I don't have all the answers but I am merely providing alternatives from the POV of someone that works with/on these controls daily. Since the birth of reflector, developers have been able to "spy" on some of the internals, and what I am suggesting could have been done by anyone. Let me make it clear that I'm trying to show the difference when you use data source controls vs binding directly to the DataSource property, and what happens internally with the data controls in each case.
What I see everyday on these forums are bad practices and I'm trying to rectify some of those by shedding a little light on how things work behind the scenes. If you would like me to post more on these I can if that helps you grasp my point.
Thats what the GridView does internally when you use a data source control.
Not sure what you mean by this.
You'd have to manually add the helper. You'd use it one the gridview row when you want to perform an operation(i.e delete, update, insert or a custom command).
You'd have to manually loop over all your rows and call the helper for each row. The helper method extracts values from each cell using a cleverly named method ExtractValuesFromCell. For each row you should get the values that you expect. Are you using bound fields to do this btw?
Hope this helps
I'll post something in the future about DataControlField's and how they extract their data.
Senior SDE, ASP.NET Team, Microsoft
kraznodar
Contributor
3332 Points
881 Posts
Re: Getting Data out of your DataControls
Jan 19, 2009 08:26 PM|LINK
David,
Thank you very much for your answer. I apologize for the rather noxious choice of terminology. I'm kind of disgruntled because I found a tutorial on a different site and used it with my gridview. Your post shows that exactly what they did is completely wrong. I didn't find out it was wrong until after doing a couple of days of work to get to where I could do a preliminary test. The null reference error was not amusing. I did a search and until I found your post they all said to do the same wrong thing so I was already pretty frustrated before I found your post.
Your response to my post answers all of my important questions. Only the first column is bound but your blog entry deals with manually binding so I know what to do. I need to manually make a dictionary for each set of textboxes and bind them. Not a big deal since you show how to do it step by step.
I do have a real complaint about Microsoft documentation. Clearly you aren't the person that needs to hear why my coworkers and I aren't overly impressed with Microsoft documentation. Some of it is great but for more advanced things like the gridview it tends to skip a lot of detail or be misleading. Either that or it is in engineer speak that is hard to decipher. I read quite a bit about how to use the gridview on MSDN and it was implied that we could use the gridview this way. Never said outright but it was implied. Is there a contact that we can whine to about this and not waste any more of your time?
It is always good to eliminate bad practices and I hope I wasn't discouraging towards your doing that. It is a big task though. I just read a CERT list of bad programming practices and about half the list is things I was taught in class. One other question though: Why do you get that cold shiver when people use the FindControl in a gridview? I accept that it isn't good but what is the risk?
Thanks for your time!
PS. You should get a raise for dealing with jerks like me.
davidfowl
Contributor
2683 Points
608 Posts
Microsoft
Re: Getting Data out of your DataControls
Jan 19, 2009 09:32 PM|LINK
It's not the use of FindControl that makes my spine tingle its the MISuse and ABuse of it that does. FindControl is a good method and very useful, but look at the name of method, FindControl. Everything has a purpose right? If I'm trying to find a control then i'll call find control. If I'm trying to get data out of a control I might call find control or some other API that is better at getting data, i.e ExtractRowValues. I think that there is alot of knowledge out there, good and bad, about using these data controls and I just want to try to set some things straight by shedding some more light on how stuff works (I love that website).
Before I go lemme show you what I mean by picking the right API. Lets make a new control IntControl. IntControl has a method int GetValue(). Now OO principals tell us to not break the abstraction by letting consumers grab at the internals but IntControl derives from control, and by default, there is alot of other things exposed to the consumer that shouldn't be. Now we know that IntControl uses a textbox internally so I could never call GetValue and instead write:
int value;
Int32.TryParse(((TextBox)IntControl.Controls[0]).Text, out value);
Everything will work fine until the author of IntControl decides to use a div instead of a textbox.
The REAL lesson to take away from the blog post is to try your best to follow the contract provided by a class or an interface (in this case the DataControl).
Senior SDE, ASP.NET Team, Microsoft
snikos
Member
14 Points
8 Posts
Re: Getting Data out of your DataControls
Jan 21, 2009 07:55 AM|LINK
Hello
I am building a website that shows data from a database ! I use the datalist component so I can show the data In a formatted way. My problem is that in the datalist I don’t show only data but I have an image button that when you press it , it shows you a reservation form using ajax (ModalpopupExtender). In the reservation form there are text components that I want to get there data.
protected void DataList3_ItemCommand(object source, DataListCommandEventArgs e)
{
if (Convert.ToInt32(e.CommandArgument) == 3)
{
string test;
test = ((TextBox)(DataList3.FindControl("fullname"))).Text;
Response.Write(test);
}
}
But an exception rise :
NullReferenceException was unhandled by user code
Thanks !!!
Nemesis116
Contributor
4667 Points
927 Posts
Re: Getting Data out of your DataControls
Jan 21, 2009 08:32 AM|LINK
-I don't think it's able to find the Control fullname. Try stepping through your code.
Visit my blog