DataGrid

A

anonymous

I am creating a datagrid control at run time.
Me.Controls.Add(Answers)
With Answers
.AllowPaging = True
.PageSize = 1
.DataSource = Data()
.DataBind()
End With
But when I try to access PageIndexChanged event it is not
recognized. Any idea why?
here is my code for PageIndexChanged event
With Answers
.CurrentPageIndex = e.NewPageIndex
.SelectedIndex = -1
.DataSource = Data()
.DataBind()
End With
 
K

Ken Cox [Microsoft MVP]

You didn't show where you are adding the event and handler. Try this?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim Answers As New DataGrid
With Answers
.AllowPaging = True
.PageSize = 5
.DataSource = Data()
.DataBind()
End With
PlaceHolder1.Controls.Add(Answers)
AddHandler Answers.PageIndexChanged, _
AddressOf ChangePage
End Sub

Sub ChangePage _
(ByVal source As Object, _
ByVal e As DataGridPageChangedEventArgs)
' This procedure handles events raised by the object source.
Response.Write("Event fired. " & _
e.NewPageIndex.ToString) ' Handle the event.
End Sub

Function Data() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
A

anonymous

I am getting this error:
Control '_ctl0__ctl5__ctl1' of type 'DataGridLinkButton'
must be placed inside a form tag with runat=server.
Below is my code:
Public Class WebForm1
Inherits System.Web.UI.Page
Dim grdwaiversAnswers As New DataGrid....
....
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
With Answers
.AllowPaging = True
.PageSize = 1
.DataSource = GetData()
.DataBind()
End With
Me.Controls.Add(Answers)
AddHandlerAnswers.PageIndexChanged, AddressOf
ChangePage
End Sub
Sub ChangePage(ByVal source As Object, ByVal e As
DataGridPageChangedEventArgs)
' This procedure handles events raised by the
object source.
Response.Write("Event fired. " &
e.NewPageIndex.ToString) ' Handle the event.
End Sub

Function GetData() As DataSet
Dim storedParams(2) As SqlParameter
storedParams =
SqlHelperParameterCache.GetSpParameterSet
(ConfigurationSettings.AppSettings("DBConnInfo"), "sp")
storedParams(0).Value = "900005"
storedParams(1).Value = 1
Dim AnswersDataSet As DataSet =
SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings
("DBConnInfo"), CommandType.StoredProcedure, "sp",
storedParams)
If AnswersDataSet.Tables(0).Rows.Count = 0 Then
MsgBox(" No answers")
End If
GetData = AnswersDataSet
AnswersDataSet.Dispose()
End Function
 
K

Ken Cox [Microsoft MVP]

You probably need to add a Placeholder to your .aspx page. Add the datagrid
to the placeholder's Controls collection.
 
A

anonymous

BELOW IS MY CODE FROM HTML:
<body>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>
 
K

Ken Cox [Microsoft MVP]

Where did you add your datagrid to the placeholder's Controls collection?
See my code how I did it.
 
A

anonymous

I've created a new form and copy your code.
It will not compile because it is not recognizing the
following value:
.AllowPaging , .PageSize , .DataSource
Any idea why? in my form I have:
Imports System.Web
Imports System.Web.UI.WebControls
 
K

Ken Cox [Microsoft MVP]

Here's all the code I have.


<%@ Page Language="vb" AutoEventWireup="false" Codebehind="dgdyamic.aspx.vb"
Inherits="p4320work.dgdyamic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>dgdyamic</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:placeHolder id="PlaceHolder1" runat="server"></asp:placeHolder>
</form>
</body>
</HTML>


'----------------------------------------------------------
Public Class dgdyamic
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents PlaceHolder1 As
System.Web.UI.WebControls.PlaceHolder

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim Answers As New DataGrid
With Answers
.AllowPaging = True
.PageSize = 5
.DataSource = Data()
.DataBind()
End With
PlaceHolder1.Controls.Add(Answers)
AddHandler Answers.PageIndexChanged, _
AddressOf ChangePage
End Sub

Sub ChangePage _
(ByVal source As Object, _
ByVal e As DataGridPageChangedEventArgs)
' This procedure handles events raised by the object source.
Response.Write("Event fired. " & _
e.NewPageIndex.ToString) ' Handle the event.
End Sub

Function Data() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
End Class
 
A

anonymous

WHEN I CHANGE THE DECLARATION FROM
DIM ANSWERS AS NEW DATAGRID
TO
Dim ANSWERS As New System.Web.UI.WebControls.DataGrid
IT FIRE THE EVENT.
THANKS
 
A

anonymous

It started again and I didn't change any code.
I am getting this error:
Control '_ctl0__ctl5__ctl1' of type 'DataGridLinkButton'
must be placed inside a form tag with runat=server.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top