![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| Hey everyone, it peebman2000 i'm posting this question for a friend and co-worker who is currently working on a .Net application. And here is his question: I need help updating gridviews that are created dynamically in the AJAX TabContainer (in which the tabs are also created dynamically). (The code I have currently creates 9 tabs with 2 gridviews in each tab.) Everything works except I can't figure out how to handle the update. I thought about creating a generic RowEditing Sub and passing the gridview as a third parameter However, I'm not really sure how to do that. Any help or suggestions would be grateful! Thanks! ASPX page Code: <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" title="Title" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePartialRendering="true">
</cc1:ToolkitScriptManager>
<cc1:TabContainer ID="TabContainer1" runat="server" AutoPostBack="true">
</cc1:TabContainer>
</asp:Content>
ASPX.VB CODE: Code: Protected Sub Gridview_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs, ByVal grid As GridView)
grid.EditIndex = e.NewEditIndex
End Sub
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
Dim gv1, gv2 As New GridView()
MyBase.OnInit(e)
Dim sqlconn As New SqlConnection(WebConfigurationManager.ConnectionStrings("ConnectionString").ToString)
Dim cmdExpr As SqlCommand
Dim strSQL As String
Dim myReader As SqlDataReader
Dim id As Integer = 1
strSQL = "Select * from sometable"
cmdExpr = New SqlCommand(strSQL, sqlconn)
sqlconn.Open()
myReader = cmdExpr.ExecuteReader
While myReader.Read
' Create the tab panel's content container and creates the table inside the tab content
Dim tabContent, table As New Control()
Dim tab As New AjaxControlToolkit.TabPanel()
tab.ID = "tabPanel" + id
tab.HeaderText = "Text"
Dim tbl As New Table()
Dim label As New Label()
Dim gv1, gv2 As New GridView()
Dim sdsold, sdsnew As New SqlDataSource()
sdsold.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
sdsnew.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
sdsold.ID = "sdsOld" + id
sdsnew.ID = "sdsNew" + id
sdsnew.SelectCommand = "Select * from table1"
sdsold.SelectCommand = "Select * from table2"
gv1.EmptyDataText = "No record(s) exist"
gv1.ID = "GV1" + id
gv1.Attributes.Add("runat", "server")
'Add Values to the editRowCommand
gv1.Attributes.Add("onRowEditing", "Values go here")
gv1.DataSourceID = sdsnew.ID
gv1.Visible = True
gv1.AutoGenerateEditButton = True
gv2.ID = "GV2" + id
gv2.EmptyDataText = "No record(s) exist"
gv2.Attributes.Add("runat", "server")
'Add Values to the editRowCommand
gv2.Attributes.Add("onRowEditing", "Values go here")
gv2.DataSourceID = sdsold.ID
gv2.Visible = True
gv2.AutoGenerateEditButton = True
' Create Table
Dim tr As New TableRow()
' Create column 1
Dim td1 As New TableCell()
td1.Controls.Add(gv2)
' Create column 2
Dim td2 As New TableCell()
' Add control to the table cell
td2.Controls.Add(gv1)
' Add cell to the row
tr.Cells.Add(td1)
tr.Cells.Add(td2)
' Add row to the table.
tbl.Rows.Add(tr)
tabContent.Controls.Add(label)
table.Controls.Add(tbl)
table.Controls.Add(sdsnew)
table.Controls.Add(sdsold)
tab.Controls.Add(tabContent)
tab.Controls.Add(table)
Me.TabContainer1.Tabs.Add(tab)
gv1.DataBind()
gv2.DataBind()
id += 1
End While
sqlconn.Close()
'Set active tab index
Me.TabContainer1.ActiveTabIndex = 0
End Sub
|
| Sponsored Links |
|
#2
| ||||
| ||||
| What exactly do you mean by "Update Gridview"?
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.Join our Folding team: DeveloperBarn Folding |
|
#3
| |||
| |||
| Hey thanks jmurrayhead, he's trying to edit the rows in the gridviews that are created. He says this: Quote:
Thanks. |
|
#4
| ||||
| ||||
| When you create the GridVew, try setting the event to the sub you create: Code: Dim WithEvents gv1 As New GridView AddHandler gv1.RowEditing, AddressOf gv1_RowEditing |
|
#5
| |||
| |||
| Okay he's trying it, i'll let you know if it works for him. |
|
#6
| |||
| |||
| Hey jmurrayhead, its not working, here's his reply: The only way I can add that code is: Declaring the gridviews outside of Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs) : Dim WithEvents gv1, gv2 As New GridView() Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs) ...... End Sub Then I created the following subroutines: Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) gv1.EditIndex = e.NewEditIndex End Sub Protected Sub gv2_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) gv2.EditIndex = e.NewEditIndex End Sub But, because I created the Gridviews outside of the loop, the other tabs no longer have the gridviews inside them. It is still not updating the edits. Do I need to create the other methods (RowUpdating, RowUpdated ) to handle this? Thanks again |
|
#7
| ||||
| ||||
| What if you declared the GridViews within the class? Code: Partial Class _myClass
Inherits System.Web.UI.Page
Private WithEvents gv1 As New GridView
''' Ad sub routines and such
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
End Class
|
|
#8
| |||
| |||
| Okay, making some progress. My co-worker has changed some of the code a little and here is his reply: I added the following code: Code: Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'cast gridview to get values
Dim tempGrid As GridView = DirectCast(sender, GridView)
tempGrid.EditIndex = e.NewEditIndex
Label1.Text = tempGrid.ID
End Sub
Protected Sub gv2_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
Dim tempGrid As GridView = DirectCast(sender, GridView)
tempGrid.EditIndex = e.NewEditIndex
Label1.Text = tempGrid.ID
End Sub
Thanks for your help. |
|
#10
| |||
| |||
| Hey thanks, this is starting to become a really crazy challenge for him, bumping head against the wall fustration; any ideas or suggestions on getting the other events working. Here's his reply and code. I added gv1_RowUpating, gv2_RowUpdating, gv1_RowUpdated, and gv2_RowUpdated. However it still doesn't seem like the update is being triggered. When I click update the new value is replaced by the old value and the gridview stays in edit mode. Do I need to rebind the gridview or is there something else I'm missing? Thanks! Aspx.vb code: Code: Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
Dim sqlconn As New SqlConnection(WebConfigurationManager.ConnectionStrings("ConnectionString").ToString)
Dim cmdExpr As SqlCommand
Dim strSQL As String
Dim myReader As SqlDataReader
Dim id As Integer = 1
strSQL = "Select * from sometable"
cmdExpr = New SqlCommand(strSQL, sqlconn)
sqlconn.Open()
myReader = cmdExpr.ExecuteReader
While myReader.Read
' Create the tab panel's content container and creates the table inside the tab content
Dim tabContent, table As New Control()
Dim tab As New AjaxControlToolkit.TabPanel()
tab.ID = "tabPanel" + id
tab.HeaderText = "Text"
Dim tbl As New Table()
Dim label As New Label()
Dim gv1, gv2 As New GridView()
AddHandler gv1.RowEditing, AddressOf gv1_RowEditing
AddHandler gv1.RowUpdating, AddressOf gv1_RowUpdating
AddHandler gv1.RowUpdated, AddressOf gv1_RowUpdated
AddHandler gv2.RowEditing, AddressOf gv2_RowEditing
AddHandler gv2.RowUpdating, AddressOf gv2_RowUpdating
AddHandler gv2.RowUpdated, AddressOf gv2_RowUpdated
Dim sdsold, sdsnew As New SqlDataSource()
sdsold.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
sdsnew.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString
sdsold.ID = "sdsOld" + id
sdsnew.ID = "sdsNew" + id
sdsnew.SelectCommand = "Select * from table1"
sdsold.SelectCommand = "Select * from table2"
gv1.EmptyDataText = "No record(s) exist"
gv1.ID = "GV1" + id
gv1.Attributes.Add("runat", "server")
'Add Values to the editRowCommand
gv1.Attributes.Add("onRowEditing", "Values go here")
gv1.DataSourceID = sdsnew.ID
gv1.Visible = True
gv1.AutoGenerateEditButton = True
gv2.ID = "GV2" + id
gv2.EmptyDataText = "No record(s) exist"
gv2.Attributes.Add("runat", "server")
'Add Values to the editRowCommand
gv2.Attributes.Add("onRowEditing", "Values go here")
gv2.DataSourceID = sdsold.ID
gv2.Visible = True
gv2.AutoGenerateEditButton = True
' Create Table
Dim tr As New TableRow()
' Create column 1
Dim td1 As New TableCell()
td1.Controls.Add(gv2)
' Create column 2
Dim td2 As New TableCell()
' Add control to the table cell
td2.Controls.Add(gv1)
' Add cell to the row
tr.Cells.Add(td1)
tr.Cells.Add(td2)
' Add row to the table.
tbl.Rows.Add(tr)
tabContent.Controls.Add(label)
table.Controls.Add(tbl)
table.Controls.Add(sdsnew)
table.Controls.Add(sdsold)
tab.Controls.Add(tabContent)
tab.Controls.Add(table)
Me.TabContainer1.Tabs.Add(tab)
gv1.DataBind()
gv2.DataBind()
id += 1
End While
sqlconn.Close()
'Set active tab index
Me.TabContainer1.ActiveTabIndex = 0
End Sub
Protected Sub gv1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'cast gridview to get values
Dim tempGrid As GridView = DirectCast(sender, GridView)
tempGrid.EditIndex = e.NewEditIndex
End Sub
Protected Sub gv2_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'cast gridview to get values
Dim tempGrid As GridView = DirectCast(sender, GridView)
tempGrid.EditIndex = e.NewEditIndex
End Sub
Protected Sub gv1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim tempGrid As GridView = DirectCast(sender, GridView)
Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
End Sub
Protected Sub gv2_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim tempGrid As GridView = DirectCast(sender, GridView)
Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
End Sub
Protected Sub gv2_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
Dim tempGrid As GridView = DirectCast(sender, GridView)
Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
End Sub
Protected Sub gv1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
Dim tempGrid As GridView = DirectCast(sender, GridView)
Label1.Text = "NEW: " & e.NewValues.Item(1) & "OLD: " & e.OldValues.Item(1)
End Sub
|
![]() |
|
| Bookmarks |
| Tags |
| dynamic, gridview, rowediting, update |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| add additional footer to gridview | peebman2000 | .Net Development | 13 | May 2nd, 2008 01:50 PM |
| Update sql connection string in web config | peebman2000 | .Net Development | 5 | April 18th, 2008 12:23 PM |
| [ASP.Net/General] Create ToolTip for GridView Header | jmurrayhead | Code Samples | 0 | March 21st, 2008 02:26 PM |