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.
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
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
GridViewRow row = this.gridView1.Rows[0];
TextBox txt = (TextBox)row.FindControl("
DataRow dr = DataSet1.Tables(0).Rows(0)
dr["Name"] = txt.Text;
Bob
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
Thanks
ken
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
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.IsClientScriptBlockReg istered(cs type, cskey)) Then
cs.RegisterClientScriptBlo ck(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("chkS elect"), 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("Tex tBox1"), 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= "SelectedR ow";
}
else
{
obj.parentNode.parentNode. className= "";
}
}
function SelectAllRows()
{
var chkAll = window.event.srcElement;
var tbl = chkAll.parentNode.parentNo de.parentN ode.parent Node;
if (chkAll)
{
for(var i=1;i<tbl.rows.length-1;i+ +)
{
var chk = tbl.rows[i].cells[0].first Child;
chk.checked=chkAll.checked ;
checkRowOfObject(chk);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HA4Conne ctionStrin g %>" 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="SqlDataSourc e1" 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:TextB ox>
<asp:TextBox ID="txtLong" runat="server"></asp:TextB ox>
<asp:TextBox ID="txtrecordID" runat="server"></asp:TextB ox></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. GridViewRo wEventArgs ) 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("c hkSelectAl l"), CheckBox)
If Not chkAll Is Nothing Then
chkAll.Attributes.Add("onC lick", "SelectAllRows()")
End If
End If
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = CType(e.Row.FindControl("c hkSelect") , CheckBox)
If Not chk Is Nothing Then
chk.Attributes.Add("onClic k", "SelectRow()")
End If
e.Row.Attributes.Add("oncl ick", "SelectRow()")
e.Row.Attributes.Add("titl e", "Click to toggle the selection of this row")
End If
End Sub
End Class
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.
<%@ 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.IsClientScriptBlockReg
cs.RegisterClientScriptBlo
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("chkS
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("Tex
'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.
}
else
{
obj.parentNode.parentNode.
}
}
function SelectAllRows()
{
var chkAll = window.event.srcElement;
var tbl = chkAll.parentNode.parentNo
if (chkAll)
{
for(var i=1;i<tbl.rows.length-1;i+
{
var chk = tbl.rows[i].cells[0].first
chk.checked=chkAll.checked
checkRowOfObject(chk);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HA4Conne
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="False" DataSourceID="SqlDataSourc
<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:TextB
<asp:TextBox ID="txtLong" runat="server"></asp:TextB
<asp:TextBox ID="txtrecordID" runat="server"></asp:TextB
</form>
</body>
</html>
Code behind:
Partial Class Default900
Inherits System.Web.UI.Page
Protected Sub GridView1_RowCreated(ByVal
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("c
If Not chkAll Is Nothing Then
chkAll.Attributes.Add("onC
End If
End If
If e.Row.RowType = DataControlRowType.DataRow
Dim chk As CheckBox = CType(e.Row.FindControl("c
If Not chk Is Nothing Then
chk.Attributes.Add("onClic
End If
e.Row.Attributes.Add("oncl
e.Row.Attributes.Add("titl
End If
End Sub
End Class
ASKER
Bob -
Worked it out! Thanks for help
Ken
Worked it out! Thanks for help
Ken
Bob