Solved

Update multiple rows at once

Posted on 2011-02-20
8
635 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now