Solved

Update multiple rows at once

Posted on 2011-02-20
8
643 Views
Last Modified: 2012-05-11
Asp.net 4

Hi

We currently have a web page with a listview control which is bound to a linq 2 sql datasource.

On each row of the list view there are textboxes and an update button which users must click to update one row at a time.

How could we could create a page on which there was multiple rows but only one update button on the page which would process all rows at once.

I guess we would use a repeater but has anybody done this?  Is linq 2 sql appropriate or is it better to use some other way to connect to sql server?

Wing
0
Comment
Question by:WingYip
  • 6
  • 2
8 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34940006
Hi,

try looking at this:

http://www.codeproject.com/KB/webforms/EditGridviewCells.aspx

its not quite what your after, but the edits are commited as you leave the cell, so NO submit button at all!

An alternate method would be to force the grid to display the data in textboxes, then parse the grid when submit is clicked to do all the updates. I prefer the editable grids, but let me know if you want me to explain further.

GMM
0
 
LVL 1

Author Comment

by:WingYip
ID: 34942091
Reason for the question is to get more work done on each round trip.
What is the alternate method you mention?
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34942141
Hi,

Ok posting from my mobile so can't be detaied but in essence what you'd need to do is

On the aspx page
Edit the gridview so that each column is a textbox (off the top of my head in vwd you click on the little tag to goto design modeand you can set it there)

 
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 5

Expert Comment

by:gingermoleman
ID: 34942208
Sorry, hit post without finishing!
In the code behind you'll need to parse your grid on the submit button

Something like
If gridview1.hasRows
For each row in gridview
Update table
Next
Endif
I'll be able to write out the actual code in about 4hrs if you need it

Gmm
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 500 total points
ID: 34946697
Hi,

the linq took a bit longer than expected but here you go.

On the gridview you get it to display in Textboxes by using item templates. the linq update seems to want the old and new value so we keep this in a label that we hide.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="LinqDataSource1">
<Columns>
<asp:TemplateField HeaderText="id">
<ItemTemplate>
<asp:label ID="lbid" runat="server" Width="25px"
 text='<%# Bind("id") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Textbox ID="tbFirstName" runat="server" Width="100px"
 text='<%# Bind("firstName") %>'/>
<asp:label ID="hiddenFirstName" runat="server" Width="100px"
 text='<%# Bind("firstName") %>' Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Textbox ID="tbLastName" runat="server" Width="100px"
 text='<%# Bind("lastName") %>'/>
<asp:label ID="hiddenlastName" runat="server" Width="100px"
 text='<%# Bind("lastName") %>' Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
       ContextTypeName="updateAllDataContext" EnableUpdate="True" TableName="tests">
</asp:LinqDataSource>


in the code behind for a submit button, you need to parse the table one row at a time and update the record based on the ID


Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click

        Dim intRowIndex As Integer = 0
        Dim RowGrid As GridViewRow = GridView1.Rows(intRowIndex)
         For index As Integer = 0 To GridView1.Rows.Count - 1

            Dim keyValues As New ListDictionary()
            Dim newValues As New ListDictionary()
            Dim oldValues As New ListDictionary()
            keyValues.Add("id", Int32.Parse(CType(GridView1.Rows(index).FindControl("lbid"), Label).Text))

            oldValues.Add("firstname", CType(GridView1.Rows(index).FindControl("hiddenfirstname"), Label).Text)
            oldValues.Add("lastname", CType(GridView1.Rows(index).FindControl("hiddenlastname"), Label).Text)

            newValues.Add("firstname", CType(GridView1.Rows(index).FindControl("tbfirstname"), TextBox).Text)
            newValues.Add("lastname", CType(GridView1.Rows(index).FindControl("tblastname"), TextBox).Text)


            LinqDataSource1.Update(keyValues, newValues, oldValues)

            GridView1.DataBind()


        Next


    End Sub

Hope this helps

GMM
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34946714
hi again!

you dont need these 2 lines at all:

        Dim intRowIndex As Integer = 0
        Dim RowGrid As GridViewRow = GridView1.Rows(intRowIndex)


GMM
0
 
LVL 1

Author Comment

by:WingYip
ID: 35072630
Hi GMM

Sorry been away for a couple weeks and getting back to this now.

Interesting - need to mull this one over.

Thanks for the input.

REgards

Wing
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 35073391
Hi Wing,

No probs, let me know if theres any more questions

GMM
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question