Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to bind dataset to textbox?
Message
De
22/10/2013 12:52:31
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01585915
Message ID:
01586135
Vues:
18
>>>>>>>>>>>>>>>>>On the same page I have a GridView bound to a dataset. Below the GridView there is a textbox (or several textboxes). As user navigates through the GridView (clicking on this or that row) I want the value/text in the textbox(s) to be updated according to new row in the dataset. My understanding is I need to bind the textbox(s) to the same dataset. How do you do it? An example, of one textbox binding to the dataset would be very helpful.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>If you don't mind round-tripping to the server you can wire up the GridView.SelectedIndexChanged event and do something like this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
>>>>>>>>>>>>>>>>        {
>>>>>>>>>>>>>>>>            TextBox1.Text = GridView1.SelectedRow.Cells[2].Text;
>>>>>>>>>>>>>>>>            //etc
>>>>>>>>>>>>>>>>        }
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>I prefer not to make the "trip" back to the server. I am working on having a button field on the GridView and wire it to a method of the page. The only thing I don't know - yet - if the page has to be reposted on the call to this method. If so I will look for way to do it with jQuery so there is no reposting of the page and no trip back to the server.
>>>>>>>>>>>>>>>Thank you.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>Not quite so easy. Wire up the GridView.RowDataBound event in c#:
       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
>>>>>>>>>>>>>>        {
>>>>>>>>>>>>>>            if (e.Row.RowType == DataControlRowType.DataRow)
>>>>>>>>>>>>>>            {
>>>>>>>>>>>>>>                e.Row.Attributes.Add("onClick", "javascript:void selectRow(this);");
>>>>>>>>>>>>>>            }
>>>>>>>>>>>>>>        }
then script on the page:
 function selectRow(i) {
>>>>>>>>>>>>>>            document.getElementById("TextBox1").value = i.cells[5].innerText;
>>>>>>>>>>>>>>        }
>>>>>>>>>>>>>
>>>>>>>>>>>>>Thank you very much. I will try to "translate" your code into jQuery code. I have been studying jQuery over the weekend and am anxious to try it.
>>>>>>>>>>>>jQuery would just be :
function selectRow(i) {
>>>>>>>>>>>>               $('#TextBox1').val(i.cells[5].innerText);
>>>>>>>>>>>>        }
TBH, when I need this type of functionality now I use WebAPI and angular. angular gives you a nice MVVM two-way binding on the client.
>>>>>>>>>>>
>>>>>>>>>>>Thank you for the example of using jQuery.
>>>>>>>>>>>As to WebAPI and angular, this is the first time I hear about these technologies. You mentioned MVVM but will WebAPI and angular work on ASP.NET Web Forms, as far as you know?
>>>>>>>>>>
>>>>>>>>>>You can use it in a Web Forms application - but where it *is* being used a simple HTML page is probably easier to work with than using a Web Form (aspx) page.
>>>>>>>>>
>>>>>>>>>Thank you. A lot to learn. And I found a couple of tutorials that I will go through at some point.
>>>>>>>>>
>>>>>>>>>Right now I am trying to figure how to pass to a jQuery function the row object values of GridView.
>>>>>>>>
>>>>>>>>You can't really do that directly. jQuery only sees what's on the client and at that point your datarow values are just tr's in a td...
>>>>>>>>Using the WebApi approach you would simply pass the data as a collection of JSON objects and use angular (or similar) to bind the elements of the page to those objects. As a side effect this can greatly reduce the workload on the server and the volume of server/client traffic.
>>>>>>>>
>>>>>>>>> I think I need to learn JSON for that or maybe something else. My head is spinning.
>>>>>>>
>>>>>>>You are correct (as I am learning) that jQuery will only see the tr's and td's. But if we skip angular for now since it is too much for me to take at this point, I found the following code online (that I am trying to adopt to my case):
>>>>>>>
>>>>>>>
>>>>>>>var rowData = {
>>>>>>>                "EquipmentID": $(this).closest('tr').find('#id_number').text(),
>>>>>>>                  "Company": $(this).closest('tr').find('.eq_descr').text(),
>>>>>>>                  "Model": $(this).closest('tr').find('.model').text()
>>>>>>>
>>>>>>>
>>>>>>>I understand that 'this' is the object passed to the jQuery function() but I can't seem to figure how to use what is inside the Find() function. So far all I get is a bunch of empty strings (for each value in the pair). And I am not sure if the use of 'closest' is correct.
>>>>>>
>>>>>>Can you post the link for that code so that I can see it in context. Ta
>>>>>
>>>>>http://stackoverflow.com/questions/17869837/passing-row-data-to-jquery-function
>>>>>
>>>>>Thank you.
>>>>
>>>>Hmm. The code doesn't reveal where the button in question is in relation to the table so the behaviour of 'closest()' is moot. closest() is the same as parent() except that it includes itself in the search. Also your table probably doesn't have the Ids/classes expected in that snippet.
>>>>
>>>>Maybe you could extend the example I gave using the tablerow click:
 var rowData = {
>>>>                "EquipmentID": i.cells[1].innerText,
>>>>                "Company": i.cells[2].innerText,
>>>>                "Model": i.cells[3].innerText
>>>>            };
>>>
>>>Thank you for your suggestion.
>>>
>>>I also found a page/post that describes almost exactly what I am trying to do:
>>>
>>>http://adsanti.wordpress.com/2012/08/03/asp-net-gridview-select-row-with-jquery/
>>>
>>>I am going through the post trying to understand that code.
>>
>>Haven't looked at it that closely. I can see that he's binding the click() on the client side rather than using code on the server as I was doing - but apart from that I'm not sure how much more it brings to the table......
>
>But as often is the case in our business, after spending a lot of time on this issue, I realized that the approach of "retrieving" values of columns in the jQuery will not work for me :(. The GridView columns are only a few columns of the dataset that I will need to work with. So I will need to re-think my approach. I need to learn to convert the dataset into the JSON and then pass the JSON object to the jQuery function. Does this sound like possible or doable?

That's why I suggested WebApi - although a DataSet object would probably be too bulky/complex to convert to JSON. Better to define a class with properties to match a datarow and build up a collection of these from the DataTable (Of course that brings us back to EF :-} ). I'll try to post a simple example - but it won't be until tomorrow now.....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform