?
Solved

microsoft, asp.net, 2, Gridview

Posted on 2007-08-09
8
Medium Priority
?
2,740 Views
Last Modified: 2013-11-05
How do I update a specific gridview row field with data from an external textbox. I am using google maps api to select and place a location's latitude and longitude values in a textbox. I then want to update a selected gridview database record's latitude and lingitude fields with these values.

0
Comment
Question by:Beish1
  • 5
  • 3
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19661071
Loop through each GridView row, and update the underlying data source, and then write the changes to the data source back to the database.

Bob
0
 

Author Comment

by:Beish1
ID: 19661111
Bob
I want to select a specific row and then click a button which will then trigger the update of the latitude and longitude values to the selected records.

Any code samples you have would be of great benefit as I am new to ASP.Net

Ken
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19670714
C# example:

GridViewRow row = this.gridView1.Rows[0];
TextBox txt = (TextBox)row.FindControl("TextBox1");

DataRow dr = DataSet1.Tables(0).Rows(0);
dr["Name"] = txt.Text;

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Beish1
ID: 19673088
Bob - Thanks - will give this a try. I'll need to translate to VB which may take a few attempts! Unless you have the VB equivalent?

Thanks

ken
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19673111
Dim row As GridViewRow = Me.GridView1.Rows(0)
Dim txt As TextBox = CType(row.FindControl("TextBox1"), TextBox)

Dim dr As DataRow = DataSet1.Tables(0).Rows(0)
dr("Name") = txt.Text

Ta-da!!

Bob
0
 

Author Comment

by:Beish1
ID: 19673126
Thanks!
0
 

Author Comment

by:Beish1
ID: 19680415
Bob - I've defined my database using SQLDataSource tool rather than a dataset (which have not tried before in VB.Net). I've tried to adapt your code for my SQLDataSource definition without success. I've included my code below. The update to the gridview and database code is under btnDelete_Click (name shouldn't really be "Delete" but used previous code etc for this new function).

In the Gridview's Item template I've defined a checkbox and a TextBox1 field bound to database field ClcnREasting for updating. There is a textbox field (external to gridview) called txtLat into which the value to be updated to the Gridview TextBox1(ClcnREasting field) would be keyed. On btnDelete_Click (ie user selecting the button) the database records for those rows for which checkboxes selected should all be updated with the txtLat keyed value.

I've also included the code behind page as well, although don't think affects the specific updating

Ken

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default900.aspx.vb" Inherits="Default900" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Gridview Update Page</title>
    <link href="~/Demos.css" rel="stylesheet" type="text/css"/>
 <script language="vb" runat="server">
     Protected Overrides Sub OnInit(ByVal e As EventArgs)
         MyBase.OnInit(e)
         AddHandler Page.PreRender, AddressOf Page_PreRender
     End Sub
     Private Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
   
         Dim cskey As String = "OnDeleteScript"
         Dim scriptText As String = "function ConfirmDelete(){return confirm('Are you sure you want to delete?');}"
         Dim cstype As Type = Me.GetType()
         Dim cs As ClientScriptManager = Page.ClientScript
         If Not (cs.IsClientScriptBlockRegistered(cstype, cskey)) Then
             cs.RegisterClientScriptBlock(cstype, cskey, scriptText, True)
         End If
     End Sub
   
    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim gr As GridViewRow
        For Each gr In GridView1.Rows
            Dim chk As CheckBox = CType(gr.FindControl("chkSelect"), CheckBox)
             If Not chk Is Nothing Then
                 If chk.Checked Then
         
                     '*******YOUR CODE HERE*******          
                     'Dim row As GridViewRow = Me.GridView1.Rows(0)
                     'Dim txt As TextBox = CType(row.FindControl("TextBox1"), TextBox)

                     'Dim dr As DataRow = DataSet1.Tables(0).Rows(0)
                     
                     'dr("Name") = txt.Text

                     '***********************'
             
                 End If
             End If
         Next
         GridView1.DataBind()
     End Sub
</script>
<script language="Javascript">
          function SelectRow()
          {
                var obj = window.event.srcElement;
                if(obj.tagName=="INPUT")    //this is a checkbox
                {
                    checkRowOfObject(obj);
                }
                else if (obj.tagName=="TD") //this a table cell
                {
                    //get a pointer to the tablerow
                    var row = obj.parentNode;
                    var chk = row.cells[0].firstChild;
                    chk.checked = !chk.checked;
                    if (chk.checked)
                    {
                        row.className="SelectedRow";
                       
                    }
                    else
                    {
                       row.className="";
                    }
                }
          }
          function checkRowOfObject(obj)
          {
              if (obj.checked)
                {
                    obj.parentNode.parentNode.className="SelectedRow";
                   
                }
                else
                {
                   obj.parentNode.parentNode.className="";
                }
          }
          function SelectAllRows()
          {
             var chkAll = window.event.srcElement;
             var tbl = chkAll.parentNode.parentNode.parentNode.parentNode;
            
             if (chkAll)
             {
                  for(var i=1;i<tbl.rows.length-1;i++)
                  {
                      var chk = tbl.rows[i].cells[0].firstChild;
                      chk.checked=chkAll.checked;
                      checkRowOfObject(chk);
           
                  }
             }
          }
    </script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HA4ConnectionString %>" SelectCommand="SELECT [ClcnRRecordID], [ClcnRContribID], [ClcnRTitle], [ClcnREasting], [ClcnRNorthing] FROM [CollectionRecords] ORDER BY [ClcnRTitle]" >
           
        </asp:SqlDataSource>
       
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="False" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CssClass="GridView1Style"  >
        <SelectedRowStyle CssClass="Green" />
        <HeaderStyle CssClass="Pink" />
            <Columns>
                <asp:TemplateField >
                    <HeaderStyle HorizontalAlign="Left" />
                    <HeaderTemplate>
                        <asp:CheckBox id="chkSelectAll" ToolTip="Click here to select/deselect all rows" runat="server"  />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox  ID="chkSelect" runat="server"  />
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ClcnREasting") %>' Width="71px"></asp:TextBox>


                    </ItemTemplate>
                   
                </asp:TemplateField>
               
                <asp:BoundField DataField="ClcnRTitle"   ReadOnly="True" HeaderText="Title"  />
                <asp:BoundField DataField="ClcnREasting"   HeaderText="Latitude"  />
                <asp:BoundField DataField="ClcnREasting"  HeaderText="Longitude" />
            </Columns>
           
        </asp:GridView>
        <asp:Button ID="btnDelete" runat="server" Text="Update"  OnClick="btnDelete_Click" OnClientClick="return ConfirmDelete();" />
        <asp:TextBox ID="txtLat" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtLong" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtrecordID" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>

Code behind:

Partial Class Default900
    Inherits System.Web.UI.Page

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.Pager Then
            e.Row.Cells(0).ColumnSpan -= 2
            Dim td As TableCell = New TableCell()
            td.CssClass = "Label1"
            td.Text = "Viewing page " & GridView1.PageIndex & " of " & GridView1.PageCount
            e.Row.Cells.Add(td)
            td = New TableCell()
            Dim hl As HyperLink = New HyperLink()
            hl.NavigateUrl = "default.aspx"
            hl.Text = "Reset"
            td.Controls.Add(hl)
            e.Row.Cells.Add(td)

        End If
        If e.Row.RowType = DataControlRowType.Header Then
            Dim chkAll As CheckBox = CType(e.Row.FindControl("chkSelectAll"), CheckBox)
            If Not chkAll Is Nothing Then
                chkAll.Attributes.Add("onClick", "SelectAllRows()")
            End If
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chk As CheckBox = CType(e.Row.FindControl("chkSelect"), CheckBox)
            If Not chk Is Nothing Then
                chk.Attributes.Add("onClick", "SelectRow()")
            End If
            e.Row.Attributes.Add("onclick", "SelectRow()")
            e.Row.Attributes.Add("title", "Click to toggle the selection of this row")




        End If
    End Sub



End Class
0
 

Author Comment

by:Beish1
ID: 19682254
Bob -

Worked it out! Thanks for help

Ken
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 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