Getting Data out of your DataControlshttp://forums.asp.net/t/1362718.aspx/1?Getting+Data+out+of+your+DataControlsSun, 06 Dec 2009 08:01:52 -050013627182819081http://forums.asp.net/p/1362718/2819081.aspx/1?Getting+Data+out+of+your+DataControlsGetting Data out of your DataControls <p>Please read this blog post before you start using <strong>FindControl </strong> or go digging into the control tree.<br> <br> <a href="http://weblogs.asp.net/davidfowler/archive/2008/12/12/getting-your-data-out-of-the-data-controls.aspx">http://weblogs.asp.net/davidfowler/archive/2008/12/12/getting-your-data-out-of-the-data-controls.aspx</a></p> 2008-12-19T00:57:49-05:002831888http://forums.asp.net/p/1362718/2831888.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>&nbsp;Hi,</p> <p>Is this very helpful in&nbsp; handling user controls ?</p> <p>Please let me know the advantages of this.</p> <p>Thanks,</p> <p>Thani <br> </p> 2008-12-26T10:13:55-05:002833693http://forums.asp.net/p/1362718/2833693.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>it's the only way to get data from controls inside data controls</p> 2008-12-27T21:27:41-05:002833814http://forums.asp.net/p/1362718/2833814.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>what is?</p> 2008-12-28T01:09:33-05:002834176http://forums.asp.net/p/1362718/2834176.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls Nice article. But why does Microsoft gives us such a powerful/clean function called FindControl, while your method isn't embedded in the framework?<br> 2008-12-28T11:23:21-05:002834374http://forums.asp.net/p/1362718/2834374.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>If you read the article,&nbsp;you'll see that&nbsp;the method <strong>is</strong> embedded in the framework and on the <strong>ListView </strong>the method&nbsp;is <strong>public</strong>.I don't know exactly why we made the decisions we did my not exposing that method on the other controls, but FindControl is an abused function and people don't know how to use it properly. Wouldn't you rather have an api to get the <strong>values</strong> (because that is what you really want), rather than the thing that contains the values? Supposed your using bound fields and turned HtmlEncode and other formatting options&nbsp;on for your cell, do you want to use FindControl and have to figure out what sequence of methods to call to get&nbsp;the unformatted&nbsp;<strong>data</strong> back? Why not call <strong>ExtractValuesFromCell</strong> which knows how to do that?<br> <br> I'm not saying don't use FindControl&nbsp;its just&nbsp;not the silver bullet that fixes all problems.</p> <p>Hope this helps</p> 2008-12-28T15:34:46-05:002834682http://forums.asp.net/p/1362718/2834682.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>You are correct [:)] I think it's funny because it's the first time ever I see someone using this method. While everyone is using FindControl as a standard.</p> <p>I'm going to keep this in mind next time I need to get the value out of datacontrol. Thank you. <br> </p> 2008-12-28T22:53:32-05:002839824http://forums.asp.net/p/1362718/2839824.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>its like this</p> <p>&nbsp;<pre class="prettyprint">CType(yourDataControl.FindControl(&quot;yourControlID&quot;), yourControlType).putTheProperityHere</pre>&nbsp;</p> 2008-12-31T10:18:15-05:002840322http://forums.asp.net/p/1362718/2840322.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>Why use FindControl if you don't have to?</p> 2008-12-31T15:30:04-05:002840531http://forums.asp.net/p/1362718/2840531.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>david, why you say that.. can you explain your idea??</p> 2008-12-31T18:20:11-05:002840539http://forums.asp.net/p/1362718/2840539.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>Did you read the blog post?</p> 2008-12-31T18:28:31-05:002864688http://forums.asp.net/p/1362718/2864688.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>&nbsp;Hi davidfowl, </p> <p>Forgive me if this sounds too stupid. <br> </p> <p>Is it more simpler to use FindControl?<br> We do have methods to access rowIndex in every GridView event, right?( even if we have no direct methods in some cases)<br> </p> <p>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?<br> </p> 2009-01-13T15:08:26-05:002864965http://forums.asp.net/p/1362718/2864965.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>I would argue that is <strong>not</strong> the case. Most of the time you <strong> do</strong> 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: <br> <em><br> Supposed your using bound fields and turned HtmlEncode and other formatting options&nbsp;on for your cell, do you want to use FindControl and have to figure out what sequence of methods to call to get&nbsp;the unformatted&nbsp;<strong>data</strong> back? Why not call <strong>ExtractValuesFromCell</strong> which knows how to do that?</em><em><strong></strong></em></p> <p>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&nbsp;when using a datasource control.</p> 2009-01-13T17:14:50-05:002866098http://forums.asp.net/p/1362718/2866098.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>Hi davidfowl,</p> <p>And I wont argue with one who is atleast ten times smarter than me at a technology [:)]<br> I was merely enquiring if it was simpler the traditional way. <br> </p> 2009-01-14T06:05:28-05:002877258http://forums.asp.net/p/1362718/2877258.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p></p> <blockquote><span class="icon-blockquote"></span> <h4>davidfowl</h4> <p></p> <p>Please read this blog post before you start using <strong>FindControl </strong> or go digging into the control tree.<br> <br> <a href="http://weblogs.asp.net/davidfowler/archive/2008/12/12/getting-your-data-out-of-the-data-controls.aspx">http://weblogs.asp.net/davidfowler/archive/2008/12/12/getting-your-data-out-of-the-data-controls.aspx</a></p> <p></p> </blockquote> &nbsp; <p></p> <p>So let me see if I understand what this blog entry says:&nbsp; Data controls are designed stupidly and are not intuitive.&nbsp; The logical way almost everyone expects data controls to work fails and looping by row and/or cell returns a null reference.&nbsp; 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.&nbsp; The dictionaries have to be tied to specific events.&nbsp; If this isn't correct please explain where I'm off.<br> </p> <p>The &quot;helper&quot; 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.&nbsp; An actual real world example to put it in context would have made this quite a bit more useful.&nbsp; 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?</p> <p>I have a gridview with multiple rows and multiple columns.&nbsp; Each cell has a textbox.&nbsp; 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.&nbsp; How then would your helper help?&nbsp; 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?<br> </p> 2009-01-19T17:24:38-05:002877315http://forums.asp.net/p/1362718/2877315.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>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&nbsp;the POV of someone that works with/on these controls daily. Since the birth of <a class="" href="http://www.red-gate.com/products/reflector/">reflector</a>, developers have been able to &quot;spy&quot; 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.</p> <p>What&nbsp;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. <br> </p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>kraznodar</h4> <br> 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.&nbsp; The dictionaries have to be tied to specific events.&nbsp; If this isn't correct please explain where I'm off.<br> </blockquote> <p></p> <p>Thats what the GridView does internally when you use a data source control.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>kraznodar</h4> <br> The dictionaries have to be tied to specific events<br> </blockquote> <p></p> <p>Not sure what you mean by this.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>kraznodar</h4> <br> Do we need to add your helper to our code manually and if so where and how do we trigger it?<br> </blockquote> <p></p> <p>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). </p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>kraznodar</h4> &nbsp;<br> I have a gridview with multiple rows and multiple columns.&nbsp; Each cell has a textbox.&nbsp; 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.&nbsp; How then would your helper help?&nbsp; 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?<br> </blockquote> <br> <br> 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 <strong>ExtractValuesFromCell.</strong> For each row you should get the values that you expect. Are you using bound fields to do this btw? <p></p> <p>&nbsp;Hope this helps</p> <p>I'll post something in the future about DataControlField's and how they extract their data.</p> 2009-01-19T18:05:13-05:002877499http://forums.asp.net/p/1362718/2877499.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>David,</p> <p>Thank you very much for your answer.&nbsp; I apologize for the rather noxious choice of terminology.&nbsp; 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.&nbsp; 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.&nbsp; The null reference error was not amusing.&nbsp; 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.<br> </p> <p>Your response to my post answers all of my important questions.&nbsp; Only the first column is bound but your blog entry deals with manually binding so I know what to do.&nbsp; I need to manually make a dictionary for each set of textboxes and bind them.&nbsp; Not a big deal since you show how to do it step by step.&nbsp; <br> </p> <p>I do have a real complaint about Microsoft documentation.&nbsp; Clearly you aren't the person that needs to hear why my coworkers and I aren't overly impressed with Microsoft documentation.&nbsp; Some of it is great but for more advanced things like the gridview it tends to skip a lot of detail or be misleading.&nbsp; Either that or it is in engineer speak that is hard to decipher.&nbsp; 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.&nbsp; Never said outright but it was implied.&nbsp; Is there a contact that we can whine to about this and not waste any more of your time?</p> <p>It is always good to eliminate bad practices and I hope I wasn't discouraging towards your doing that.&nbsp; It is a big task though.&nbsp; I just read a CERT list of bad programming practices and about half the list is things I was taught in class.&nbsp; One other question though: Why do you get that cold shiver when people use the FindControl in a gridview?&nbsp; I accept that it isn't good but what is the risk?</p> <p>Thanks for your time! &nbsp;</p> <p>PS. You should get a raise for dealing with jerks like me. <br> </p> 2009-01-19T20:26:11-05:002877595http://forums.asp.net/p/1362718/2877595.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p></p> <blockquote><span class="icon-blockquote"></span> <h4>kraznodar</h4> <br> One other question though: Why do you get that cold shiver when people use the FindControl in a gridview?&nbsp; I accept that it isn't good but what is the risk?<br> </blockquote> <p></p> <p>It's not the use of FindControl that makes my spine tingle its the <strong>MIS</strong>use<strong> and AB</strong>use of it that does. FindControl is a good method and very useful, but look at the name of method, <strong>FindControl</strong>. 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 <strong>data</strong>, i.e <strong>ExtractRowValues</strong>.&nbsp;I think that there is alot of knowledge out there, good and bad,&nbsp;about using these data controls and I just want to try to set some things straight by shedding some more light on <a class="" href="http://www.howstuffworks.com/">how stuff works</a> (I love that website). <br> <br> Before I go lemme show you what I mean by picking the right API.&nbsp; Lets make a new control IntControl. IntControl has&nbsp;a method <strong>int GetValue(). </strong>Now OO principals tell&nbsp;us to not break the abstraction by letting consumers grab at the internals&nbsp;but IntControl derives from control, and by&nbsp;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&nbsp;could never call&nbsp;GetValue and instead write:<br> <br> int value;<br> Int32.TryParse(((TextBox)IntControl.Controls[0]).Text, out value);<br> <br> Everything will work fine until the author of IntControl decides to use a div instead of a textbox.</p> <p>&nbsp;The <strong>REAL</strong> lesson to take away from the blog post is to try your best to follow the contract provided by a class or an&nbsp;interface (in this case the DataControl).<br> </p> 2009-01-19T21:32:29-05:002881044http://forums.asp.net/p/1362718/2881044.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>&nbsp;</p> <p class="MsoNormal"><span lang="EN-US" style="">Hello </span></p> <p class="MsoNormal"><span lang="EN-US" style="">&nbsp;<br> 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 dont 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.</span></p> <p class="MsoNormal"><span lang="EN-US" style="">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="">&nbsp;</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'; color:blue">protected</span><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"> <span style="color:blue">void</span> DataList3_ItemCommand(<span style="color:blue">object</span> source, <span style="color:rgb(43,145,175)">DataListCommandEventArgs</span> e)</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>{</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">if</span> (<span style="color:rgb(43,145,175)">Convert</span>.ToInt32(e.CommandArgument) == 3)</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<span style="color:green"></span></span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">string</span> test;</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>test = ((<span style="color:rgb(43,145,175)">TextBox</span>)(DataList3.FindControl(<span style="color:rgb(163,21,21)">&quot;fullname&quot;</span>))).Text;</span></p> <p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10pt; font-family:'Courier New'">Response.Write(test);</span></p> <p class="MsoNormal" style="line-height:normal"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="">&nbsp;&nbsp;&nbsp;&nbsp;</span></span></p> <p class="MsoNormal" style="line-height:normal"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p> <p class="MsoNormal"><span style="font-size:10pt; line-height:115%; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>}</span><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'"></span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">But an exception rise :</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">NullReferenceException was unhandled by user code</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size:10pt; line-height:115%; font-family:'Courier New'">Thanks !!!</span></p> <p>&nbsp;</p> 2009-01-21T07:55:34-05:002881125http://forums.asp.net/p/1362718/2881125.aspx/1?Re+Getting+Data+out+of+your+DataControlsRe: Getting Data out of your DataControls <p>-I don't think it's able to find the Control fullname. Try stepping through your code. <br> </p> 2009-01-21T08:32:08-05:00