Link to home
Start Free TrialLog in
Avatar of Beish1
Beish1

asked on

microsoft, asp.net, 2, Gridview

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.

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of Beish1
Beish1

ASKER

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
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
Avatar of Beish1

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Beish1

ASKER

Thanks!
Avatar of Beish1

ASKER

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
Avatar of Beish1

ASKER

Bob -

Worked it out! Thanks for help

Ken