-
Dec 6th, 2010, 11:32 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
i'm using VB \ ASP.NET.
i'm trying to display some values in Gridview. but i don't know how many columns i need to display. at run time only i will come to know how many columns i need to display. it depends up on the values from Database. so i'm creating columns dynamically and adding to gridview. till that it works fine. but i want some columns value to be hyperlink. so i need to create some columns hyperlink dynamically.
Database table looks like this
Index-------Name--------------Date-----------------Description
1------------John----------------12/1/2010---------- Work
2------------Peter---------------12/2/2010---------- Out
3------------John----------------12/3/2010---------- Off
depends upon the "Distinct Date", the number of columns in gridview is created dynamically.
Gridview looks like this
Index-----Name----------12/1/2010-------12/2/2010----------12/3/2010
1----------John-----------Work--------------ADD NEW----------Off
2----------Peter----------ADD NEW---------Out-----------------ADD NEW
for all the date columns i want to create dynamic hyperlink.
for example : if i need to make any chnage for 12/1/2010 john, i can click hyperlink "Work" and update the information. and if i need to add some description for 12/2/2010 John, i can click hyperlink "Add New" and add description for that date.
Codes
Code:
Sub Display_Table
Dim Table_MAINPAGE As DataTable
Dim Row As DataRow
Dim dcol As DataColumn
Dim myConnection As SqlConnection
Dim MySQL As String
Dim myCommand As SqlCommand
Dim myreader As SqlDataReader
Dim pFirst As Boolean = True
Dim Name_NotExist, Group_NotExist As Boolean
aryDate.Clear()
aryName.Clear()
aryDate_Desc.Clear()
Table_MAINPAGE = New DataTable()
dcol = New DataColumn(" # ")
Table_MAINPAGE.Columns.Add(dcol)
dcol = New DataColumn("Name")
Table_MAINPAGE.Columns.Add(dcol)
myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
myConnection.Open()
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
'Get distinct Date from database
aryDate.Add(myreader(0))
dcol = New DataColumn(Trim(myreader(0)))
Table_MAINPAGE.Columns.Add(dcol)
End While
myreader.Close()
pFirst = True
MySQL = "Select fname, lname from Users ORDER BY fname"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
If pFirst = True Then
pFirst = False
'Get distinct Name from database
aryName.Add(myreader(0) & " " & myreader(1))
GoTo NextValue
End If
Name_NotExist = False
Group_NotExist = False
For q As Integer = 0 To aryName.Count - 1
If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
Name_NotExist = True
Else
Name_NotExist = False
GoTo NextValue
End If
Next
If Name_NotExist = True Then
aryName.Add(myreader(0) & " " & myreader(1))
End If
NextValue:
End While
myreader.Close()
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
'As first column is increment, as number of data in database
'Let's add some data to the other columns
For k As Integer = 0 To aryName.Count - 1
aryDate_Desc.Clear()
MySQL = "select ondate, description from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
Row(" # ") = h
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
End While
For i As Integer = 0 To aryDate.Count - 1
gBool = False
For j As Integer = 0 To aryDate_Desc.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
gBool = True
GoTo NextArrayValue
End If
Next
NextArrayValue:
If gBool <> True Then
Row(Trim(aryDate.Item(i))) = "Add New"
End If
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
Next
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
How can i create dynamic hyperlinks for the date columns.....
if you have any idea, how to do this, please help me. if you can some example, that's will be great.
Thanks in advance.
Last edited by remya1000; Dec 6th, 2010 at 11:36 AM.
-
Dec 6th, 2010, 03:47 PM
#2
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hey,
If I were you, I would be setting the AutoGenerateColumns property of the GridView to true, and then, within the RowDataBound event, take the appropriate action to add a Hyperlink to the GridView cell.
Gary
-
Dec 6th, 2010, 04:06 PM
#3
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply.
AutoGenerateColumns property of the GridView is already set as true.
But i don't know how to dynamically add hyperlink in RowDataBound event. i will search for that.
Is it possible to create dynamic hyperlink for each column like this inside RowDataBound event...
Example 1 : if i need to make any change for 12/1/2010 john, i can click hyperlink ("Update.aspx?id=1") "Work" and update the information.
Example 2 : if i need to make any change for 12/3/2010 john, i can click hyperlink ("Update.aspx?id=3") "Off" and update the information.
Example 3 : if i need to add some description for 12/2/2010 John, i can click hyperlink ("AddNew.aspx") "Add New" and add description for that date.
If you have any example, that's will be great.
Thanks in advance.
-
Dec 6th, 2010, 04:12 PM
#4
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hey,
Here is the documentation for the event:
http://msdn.microsoft.com/en-us/libr...databound.aspx
Assuming you know which columns you want to make hyperlinks, you can take action to here add a Hyperlink control into the required cell, you are can alter the contents of that cell to make it into a hyperlink.
Gary
-
Dec 7th, 2010, 12:58 PM
#5
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks for the reply...
i searched and found this code for dynamically creating Hyperlink in gridview.
Code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.DataItem
link.NavigateUrl = "Update.aspx"
e.Row.Cells(ColumnIndex.Column1).Controls.Add(link)
End If
End Sub
But when i use this code it says "name ColumnIndex not declared"...
is that how i need to do? is this the correct code i'm using for creating dynamic hyperlink?
Thanks in advance.
-
Dec 7th, 2010, 03:50 PM
#6
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hey,
Where did you pull that code from?
It should show you where ColumnIndex was declared. Do you have a link?
Gary
-
Dec 7th, 2010, 03:56 PM
#7
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply...
Below is the link from which i got that code...
http://stackoverflow.com/questions/1...ridview-column
is this the correct code i'm using for creating dynamic hyperlink?
Thanks in advance.
-
Dec 7th, 2010, 04:05 PM
#8
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hey,
That code isn't "complete", it is pseudo code. The expectation is that you replace ColumnIndex.Column1 with the actual cell index that you want to put the HyperLink into.
Gary
-
Dec 7th, 2010, 04:21 PM
#9
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for the reply....
in my GridView, i want 12/1/2010, 12/2/1010 and 12/3/2010 columns need to be Hyperlink.
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
so in that code, if i change it like this
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.DataItem
link.NavigateUrl = "Update.aspx"
e.Row.Cells(2).Controls.Add(link)
End If
End Sub
instead of 2, i change that to 3, 4, ....10
Code:
e.Row.Cells(3).Controls.Add(link)
and gridview display like this
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
i don't have 10 coumns in my gridview, but still its showing just 1st row...
is this the correct code i'm using for creating dynamic hyperlink?
Thanks in advance.
-
Dec 9th, 2010, 08:12 AM
#10
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hello,
Is it possible that you can create a small sample application of what you currently have, and I can take a look? You can upload it to this thread, or PM me details of where I can get it.
Gary
-
Dec 9th, 2010, 11:01 AM
#11
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for the reply....
created a small sample application of what i'm doing and i added that code here...
at run time only i will come to know how many columns i need to display. it depends up on the values from Database. so i'm creating columns dynamically and adding to gridview. till that it works fine. but i want some columns value to be hyperlink. so i need to create some columns hyperlink dynamically.
Code:
<%@ Page Language="VB" MasterPageFile="~/Master/Clearview.master" AutoEventWireup="false" CodeFile="SList.aspx.vb" Inherits="Issues" title="List - Home Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style ="width:800px; overflow-x:scroll;">
<asp:GridView ID="GridView1" runat="server" Width="800px" CellPadding="4" EnableTheming="True" ForeColor="#333333" GridLines="None" >
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</asp:Content>
Code:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class Issues
Inherits System.Web.UI.Page
Dim aryDate, aryName, aryDate_Desc, aryDesc_Color_Date As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Table_MAINPAGE As DataTable
Dim Row As DataRow
Dim dcol As DataColumn
Dim myConnection As SqlConnection
Dim MySQL As String
Dim myCommand As SqlCommand
Dim myreader As SqlDataReader
Dim pFirst As Boolean = True
Dim Name_NotExist, Group_NotExist As Boolean
aryDate.Clear()
aryName.Clear()
aryDate_Desc.Clear()
aryDesc_Color_Date.Clear()
Table_MAINPAGE = New DataTable()
dcol = New DataColumn(" # ")
Table_MAINPAGE.Columns.Add(dcol)
dcol = New DataColumn("Name")
Table_MAINPAGE.Columns.Add(dcol)
myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
myConnection.Open()
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
aryDate.Add(myreader(0))
dcol = New DataColumn(Trim(myreader(0)))
Table_MAINPAGE.Columns.Add(dcol)
End While
myreader.Close()
pFirst = True
MySQL = "Select fname, lname from Users ORDER BY fname"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
If pFirst = True Then
pFirst = False
'Get distinct Name from database
aryName.Add(myreader(0) & " " & myreader(1))
GoTo NextValue
End If
Name_NotExist = False
Group_NotExist = False
For q As Integer = 0 To aryName.Count - 1
If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
Name_NotExist = True
Else
Name_NotExist = False
GoTo NextValue
End If
Next
If Name_NotExist = True Then
aryName.Add(myreader(0) & " " & myreader(1))
End If
NextValue:
End While
myreader.Close()
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
'As first column is increment, as number of data in database
'Let's add some data to the other columns
For k As Integer = 0 To aryName.Count - 1
If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
aryDate_Desc.Clear()
MySQL = "select ondate, description, color from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
Row(" # ") = h
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
aryDesc_Color_Date.Add(myreader(0) & " -" & myreader(1) & " - " & myreader(3))
End While
For i As Integer = 0 To aryDate.Count - 1
gBool = False
For j As Integer = 0 To aryDate_Desc.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
gBool = True
GoTo NextArrayValue
End If
Next
NextArrayValue:
If gBool <> True Then
Row(Trim(aryDate.Item(i))) = "Add New"
End If
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
End If
Next
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim tCell As TableCell
Dim drv As DataRowView
Dim ob As Object
Dim j As Integer = 1
For i As Integer = 0 To aryDate.Count - 1
j = j + 1
If e.Row.RowType = DataControlRowType.DataRow Then
drv = e.Row.DataItem
ob = drv(aryDate.Item(i))
If ob.ToString.Contains("Work") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Blue
ElseIf ob.ToString.Contains("Off") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.LightBlue
ElseIf ob.ToString.Contains("Out") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Gray
ElseIf ob.ToString.Contains("Add New") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.White
End If
End If
Next
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'If (e.Row.RowType = DataControlRowType.DataRow) Then
' Dim link As HyperLink = New HyperLink()
' link.Text = e.Row.DataItem
' link.NavigateUrl = "Update.aspx" + e.Row.DataItem
' ' link.NavigateUrl = "Navigate somewhere based on data: " + e.Row.DataItem
' e.Row.Cells(2).Controls.Add(link)
'End If
End Sub
End Class
my Database table looks like this
Index-------Name--------------Date-----------------Description
1------------John----------------12/1/2010---------- Work
2------------Peter---------------12/2/2010---------- Out
3------------John----------------12/3/2010---------- Off
when this code runs, gridview display like this
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
that's the way i want it to be displayed....
but the next thing i want is to make all the Date coumns to displayed as hyperlink.
for example :
for Work (12/1/2010) - Update.aspx?id=1
for Off (12/3/2010) - Update.aspx?id=3
for Out (12/2/2010) - Update.aspx?id=2
How can i create dynamic hyperlinks for the date columns.....
if you have any idea, how to do this, please please please help me. if you have some example, that's will be great.
Thanks in advance.
Last edited by remya1000; Dec 9th, 2010 at 11:04 AM.
-
Dec 10th, 2010, 07:58 AM
#12
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hey,
I haven't forgotten about you...
I just haven't had a minute to sit down and take a look at this properly, but I will.
Gary
-
Dec 13th, 2010, 09:58 AM
#13
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply.
did you had a chance to check this codes?
-
Dec 13th, 2010, 04:36 PM
#14
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hello,
If I am honest, no
Had a really busy time at work the last couple days. I will really try and have a look at this tomorrow, but if you don't hear from me, either PM me, or post back into this thread.
Gary
-
Dec 13th, 2010, 04:39 PM
#15
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
-
Dec 14th, 2010, 03:46 PM
#16
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hello,
Ok, I have had a quick play with this, and this will do what you want, to an extent....
Code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim row As DataRowView = e.Row.DataItem
Dim newLink = String.Format("<a href='Update.aspx?id={1}'>{0}</a>", row("Date"), row("Index"))
e.Row.Cells(2).Text = newLink
End If
End Sub
The problem with this is that is requires knowledge of the column index for which the date column appears. If this isn't the same each time, then there is going to be a problem.
As yet, I haven't found a way to get the knowledge of the Column Index, so I will need to keep looking into this.
Have a look at the above, and let me know what you think.
Gary
-
Dec 14th, 2010, 04:09 PM
#17
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks gary for spending some time for checking my code and for sending a sample code.
let me try your code and i will let you know about that.
Thanks....
-
Dec 14th, 2010, 04:25 PM
#18
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Sounds like a plan.
Let me know how you get on.
Gary
-
Dec 14th, 2010, 04:27 PM
#19
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
-
Dec 15th, 2010, 12:06 PM
#20
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply..
This is the code where i used to add text for Date columns...
Code:
Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
As you said, instead of the above codes i tried this codes...
Code:
Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>")
then the GridView text display as <a href='Update.aspx?id=81'>-</a>
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
Instead of Work, OFF and OUT, it display this text <a href='Update.aspx?id=81'>-</a>
i tried these code just to see how it display and its display as <a href='Update.aspx?id=81'>New Link</a>
Label1.Text = String.Format("<a href='Update.aspx?id=81'>" & "New Link" & "</a>")
Textbox1.Text = String.Format("<a href='Update.aspx?id=81'>" & "New Link" & "</a>")
so now my entire code looks like this...
Code:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class Issues
Inherits System.Web.UI.Page
Dim aryDate, aryName, aryDate_Desc, aryDesc_Color_Date As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Table_MAINPAGE As DataTable
Dim Row As DataRow
Dim dcol As DataColumn
Dim myConnection As SqlConnection
Dim MySQL As String
Dim myCommand As SqlCommand
Dim myreader As SqlDataReader
Dim pFirst As Boolean = True
Dim Name_NotExist, Group_NotExist As Boolean
aryDate.Clear()
aryName.Clear()
aryDate_Desc.Clear()
aryDesc_Color_Date.Clear()
Table_MAINPAGE = New DataTable()
dcol = New DataColumn(" # ")
Table_MAINPAGE.Columns.Add(dcol)
dcol = New DataColumn("Name")
Table_MAINPAGE.Columns.Add(dcol)
myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
myConnection.Open()
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
aryDate.Add(myreader(0))
dcol = New DataColumn(Trim(myreader(0)))
Table_MAINPAGE.Columns.Add(dcol)
End While
myreader.Close()
pFirst = True
MySQL = "Select fname, lname from Users ORDER BY fname"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
If pFirst = True Then
pFirst = False
'Get distinct Name from database
aryName.Add(myreader(0) & " " & myreader(1))
GoTo NextValue
End If
Name_NotExist = False
Group_NotExist = False
For q As Integer = 0 To aryName.Count - 1
If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
Name_NotExist = True
Else
Name_NotExist = False
GoTo NextValue
End If
Next
If Name_NotExist = True Then
aryName.Add(myreader(0) & " " & myreader(1))
End If
NextValue:
End While
myreader.Close()
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
'As first column is increment, as number of data in database
'Let's add some data to the other columns
For k As Integer = 0 To aryName.Count - 1
If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
aryDate_Desc.Clear()
MySQL = "select ondate, description, color from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
Row(" # ") = h
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
aryDesc_Color_Date.Add(myreader(0) & " -" & myreader(1) & " - " & myreader(3))
End While
For i As Integer = 0 To aryDate.Count - 1
gBool = False
For j As Integer = 0 To aryDate_Desc.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>")
gBool = True
GoTo NextArrayValue
End If
Next
NextArrayValue:
If gBool <> True Then
Row(Trim(aryDate.Item(i))) = "Add New"
End If
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
End If
Next
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim tCell As TableCell
Dim drv As DataRowView
Dim ob As Object
Dim j As Integer = 1
For i As Integer = 0 To aryDate.Count - 1
j = j + 1
If e.Row.RowType = DataControlRowType.DataRow Then
drv = e.Row.DataItem
ob = drv(aryDate.Item(i))
If ob.ToString.Contains("Work") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Blue
ElseIf ob.ToString.Contains("Off") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.LightBlue
ElseIf ob.ToString.Contains("Out") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Gray
ElseIf ob.ToString.Contains("Add New") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.White
End If
End If
Next
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'If (e.Row.RowType = DataControlRowType.DataRow) Then
' Dim row As DataRowView = e.Row.DataItem
' 'Dim newLink = String.Format("<a href='Update.aspx?id={1}'>{0}</a>", row("Date"), row("Index"))
' e.Row.Cells(2).Text = newLink
'End If
End Sub
End Class
any this wrong in that code?
Thanks in advance...
-
Dec 15th, 2010, 03:40 PM
#21
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
i was tried your code and
when i try this code to display in Label, it created a HyperLink in Label1
Label1.Text = String.Format("<a href=Update.aspx?id=81>" & "My Link Text" & "</a>")
when i try this code to display in TextBox, it's displaying this text inside textbox <a href=Update.aspx?id=81>" & "My Link Text" & "</a>
TextBox1.Text = String.Format("<a href=Update.aspx?id=81>" & "My Link Text" & "</a>")
when i try this code to display in DataTable Row, it's displaying this text inside textbox <a href=Update.aspx?id=81>" & "My Link Text" & "</a>
Row("Date") = String.Format("<a href=Update.aspx?id=81>" & "My Link Text" & "</a>")
Then i tried this code using TemplateFiled,
Code:
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports system.IO
Public Class TemplateHandler
Implements ITemplate
Private columnID, columnText As String
Public Sub New(ByVal colText As String)
columnText = colText
End Sub
Private Sub ITemplate_InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim lbl As New Label()
lbl.Text = columnText
container.Controls.Add(lbl)
End Sub
End Class
Code:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class Issues
Inherits System.Web.UI.Page
Dim aryDate, aryName, aryDate_Desc, aryDesc_Color_Date As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Table_MAINPAGE As DataTable
Dim Row As DataRow
Dim dcol As DataColumn
Dim myConnection As SqlConnection
Dim MySQL As String
Dim myCommand As SqlCommand
Dim myreader As SqlDataReader
Dim pFirst As Boolean = True
Dim Name_NotExist, Group_NotExist As Boolean
aryDate.Clear()
aryName.Clear()
aryDate_Desc.Clear()
aryDesc_Color_Date.Clear()
Table_MAINPAGE = New DataTable()
dcol = New DataColumn(" # ")
Table_MAINPAGE.Columns.Add(dcol)
dcol = New DataColumn("Name")
Table_MAINPAGE.Columns.Add(dcol)
myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
myConnection.Open()
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
aryDate.Add(myreader(0))
End While
myreader.Close()
pFirst = True
MySQL = "Select fname, lname from Users ORDER BY fname"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
If pFirst = True Then
pFirst = False
'Get distinct Name from database
aryName.Add(myreader(0) & " " & myreader(1))
GoTo NextValue
End If
Name_NotExist = False
Group_NotExist = False
For q As Integer = 0 To aryName.Count - 1
If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
Name_NotExist = True
Else
Name_NotExist = False
GoTo NextValue
End If
Next
If Name_NotExist = True Then
aryName.Add(myreader(0) & " " & myreader(1))
End If
NextValue:
End While
myreader.Close()
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
'As first column is increment, as number of data in database
'Let's add some data to the other columns
For k As Integer = 0 To aryName.Count - 1
If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
aryDate_Desc.Clear()
MySQL = "select ondate, description, color from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
Row(" # ") = h
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
aryDesc_Color_Date.Add(myreader(0) & " -" & myreader(1) & " - " & myreader(3))
End While
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
End If
Next
GridView1.AutoGenerateColumns = False
Dim NameColumn As New BoundField()
NameColumn.DataField = " # "
NameColumn.HeaderText = " # "
GridView1.Columns.Add(NameColumn)
NameColumn = New BoundField()
NameColumn.DataField = "Name"
NameColumn.HeaderText = "Name"
GridView1.Columns.Add(NameColumn)
' Here is template column portion
Dim TmpCol As New TemplateField()
TmpCol.HeaderText = aryDate.Item(0).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "work" & "</a>"))
TmpCol = New TemplateField()
TmpCol.HeaderText = aryDate.Item(1).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "Off" & "</a>"))
TmpCol = New TemplateField()
TmpCol.HeaderText = aryDate.Item(2).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "Out" & "</a>"))
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim tCell As TableCell
Dim drv As DataRowView
Dim ob As Object
Dim j As Integer = 1
For i As Integer = 0 To aryDate.Count - 1
j = j + 1
If e.Row.RowType = DataControlRowType.DataRow Then
drv = e.Row.DataItem
ob = drv(aryDate.Item(i))
If ob.ToString.Contains("Work") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Blue
ElseIf ob.ToString.Contains("Off") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.LightBlue
ElseIf ob.ToString.Contains("Out") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Gray
ElseIf ob.ToString.Contains("Add New") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.White
End If
End If
Next
End Sub
End Class
when i use the above code, its creating the hyperlink in label's inside gridview. but the gridview looks like this... its adds same text for the entire columns.
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work-------------------Off----------------------Out
2----------Peter----------Work-------------------Off----------------------Out
But this is the format i need to display the gridview.
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
is it possible to display it like that?
i tried this code too.. but same result
Code:
Dim myLink As New HyperLink
myLink.Text = columnText
myLink.NavigateUrl = "Update.aspx?id=" & columnID
container.Controls.Add(myLink)
Code:
Dim TmpCol As New TemplateField()
TmpCol.HeaderText = aryDate.Item(0).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(81,"my Link Text")
If you have any idea, how to do this, please let me know.
Thanks in advance....
Last edited by remya1000; Dec 15th, 2010 at 03:53 PM.
-
Dec 16th, 2010, 06:12 AM
#22
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hey,
I am heading away for a couple days, but I will take a look at this when I get back, as you seem to be jumping around quite a bit.
Send me a PM on Saturday to remind me to take a look at this thread.
Gary
-
Dec 19th, 2010, 03:55 PM
#23
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hello,
Ok, let's see if we can break this down for you...
From what you have described, you have an unknown number of columns at runtime, which means that you can't turn off AutoGenerateColumns, which means that you can't use TemplateField's, so I am surprised that you mentioned that you had tried that.
Also, what are you trying to set the .Text property of a label? The code that I gave you will work for a GridView that has AutoGenerateColumns set to true.
Can you confirm exactly what you are using and how you are creating your GridView?
Gary
-
Dec 20th, 2010, 11:22 AM
#24
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply.
double clicked GridView from ToolBox. AutoGenerateColumns = True. this is the source code,
Code:
<asp:GridView ID="GridView1" runat="server" Width="800px" CellPadding="4" EnableTheming="True" ForeColor="#333333" GridLines="None" >
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
when i try this code (add to datarow)
Code:
Row(Trim(aryDate.Item(i))) = String.Format("<a href=Update.aspx?id=" & "81" & ">" & "work" & "</a>")
in each Gridview column, it display the text "<a href=Update.aspx?id=" & "81" & ">" & "work" & "</a>". not showing hyperlink.
i don't know why it's not showing hyperlink. any thing wrong in that code.
Always "#" and "Name" column will be there. i get remaining column names only during the runtime. Always i need to dispaly 7 date columns or 1 date coulmn.
so i have two "SUB" functions.
if i selected 1 date, then it calls the first SUB function and display 3 coulmns. "#, Name, date1".
if i just selected 1 date. it will display the following columns
#, Name, date1
if i selected 1 week, then it calls the second SUB function and display 9 coulmns. "#, Name, date1, date2, date3, date4, date5, date6, date7"
if i select a week, it will display the following columns
#, Name, date1, date2, date3, date4, date5, date6, date7
but i get column name at runtime
i think you get some idea of what i'm trying to do.
if you have any questions, please let me know.
Thanks in Advance...
Last edited by remya1000; Dec 20th, 2010 at 11:25 AM.
-
Dec 20th, 2010, 01:03 PM
#25
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Originally Posted by remya1000
i don't know why it's not showing hyperlink. any thing wrong in that code.
Yes. As I mentioned, that code that I gave you was specifically for when you are NOT using AutoGenerateColumns, which is what you are doing. When you are NOT using AutoGenerateColumns, you can alter the "text" that is rendered directly to the client browser, hence you can write out your own HTML, which is what I am doing with the above code. However, when you are using AutoGenerateColumns, you have to use a different approach.
Originally Posted by remya1000
Always "#" and "Name" column will be there. i get remaining column names only during the runtime. Always i need to dispaly 7 date columns or 1 date coulmn.
so i have two "SUB" functions.
if i selected 1 date, then it calls the first SUB function and display 3 coulmns. "#, Name, date1".
if i selected 1 week, then it calls the second SUB function and display 9 coulmns. "#, Name, date1, date2, date3, date4, date5, date6, date7"
but i get column name at runtime
i think you get some idea of what i'm trying to do.
if you have any questions, please let me know.
Thanks in Advance...
Ah, ok, I see where you are coming from now. Let me have a play, and I will try and knock you up a sample.
Gary
-
Dec 22nd, 2010, 03:42 AM
#26
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Okay, hold on a second here. With all the backward and forward, I think I have got myself confused. You are using AutoGenerateColumns = true, which is what I was using as well, so the code that I provided should work for you. Let's try a fresh sample.
Create a new Web Application, and add a new WebForm called SList.aspx. Use the following code:
ASPX:
Code:
<%@ Page MasterPageFile="~/ClearView.Master" Language="vb" AutoEventWireup="false" CodeBehind="SList.aspx.vb" Inherits="remya1000.SList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style ="width:800px; overflow-x:scroll;">
<asp:GridView ID="GridView1" runat="server" Width="800px" CellPadding="4" EnableTheming="True" ForeColor="#333333" GridLines="None" >
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</asp:Content>
Code Behind:
Code:
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Public Class SList
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Create the temporary DataTable that we will use to populate the GridView
' Replace this with your own DataTable
Dim dt As New DataTable()
Dim indexColumn As New DataColumn("Index")
Dim nameColumn As New DataColumn("Name")
Dim dateColumn As New DataColumn("Date")
Dim descriptionColumn As New DataColumn("Description")
dt.Columns.Add(indexColumn)
dt.Columns.Add(nameColumn)
dt.Columns.Add(dateColumn)
dt.Columns.Add(descriptionColumn)
Dim firstDataRow As DataRow = dt.NewRow()
firstDataRow.ItemArray = New Object() {"1", "John", "12/1/2010", "Work"}
Dim secondDataRow As DataRow = dt.NewRow()
secondDataRow.ItemArray = New Object() {"2", "Peter", "12/2/2010", "Out"}
Dim thirdDataRow As DataRow = dt.NewRow()
thirdDataRow.ItemArray = New Object() {"3", "John", "12/3/2010", "Off"}
dt.Rows.Add(firstDataRow)
dt.Rows.Add(secondDataRow)
dt.Rows.Add(thirdDataRow)
'Initialize the DataSource
GridView1.DataSource = dt
'Bind the datatable with the GridView
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
For i As Integer = 0 To e.Row.Cells.Count - 1
' This is used to find the Column Header Name
' This is being used instead of hard coding the index of the column with the date in it
Dim h As DataControlFieldHeaderCell = CType(Me.GridView1.HeaderRow.Cells(i), DataControlFieldHeaderCell)
If h.Text = "Date" Then
Dim row As DataRowView = e.Row.DataItem
Dim newLink = String.Format("<a href='Update.aspx?id={1}'>{0}</a>", row("Date"), row("Index"))
e.Row.Cells(i).Text = newLink
End If
Next
End If
End Sub
End Class
Make sure that that works for you, and then you can start to port this over into your existing application.
Gary
-
Jan 3rd, 2011, 02:22 PM
#27
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply and for the sample example.
Sorry for this late reply... actually i was trying your codes and made some changes and it start displaying the fields in gridview. but hyperlink is not working correctly...
The code used...
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
'As first column is increment, as number of data in database
'Let's add some data to the other columns
For k As Integer = 0 To aryName.Count - 1
If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
aryDate_Desc.Clear()
MySQL = "select ondate, description, color, recno from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
Row(" # ") = h
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
aryDesc_Color_Date.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(2))
aryHyperLinkIndex.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(3) & " - " & Trim(aryName.Item(k).ToString))
End While
For i As Integer = 0 To aryDate.Count - 1
gBool = False
For j As Integer = 0 To aryDate_Desc.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>")
gBool = True
GoTo NextArrayValue
End If
Next
NextArrayValue:
If gBool <> True Then
Row(Trim(aryDate.Item(i))) = "Add New"
aryHyperLinkIndex.Add(Trim(aryDate.Item(i)) & " - " & "Add New" & " - " & "0" & " - " & Trim(aryName.Item(k).split("-")(0)).ToString)
End If
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
End If
Next
'Create a new row
Row = Table_MAINPAGE.NewRow()
Row(" # ") = ""
Row("Name") = ""
For i As Integer = 0 To aryDate.Count - 1
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = ""
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
Next
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim tCell As TableCell
Dim drv As DataRowView
Dim ob As Object
Dim j As Integer = 1
For i As Integer = 0 To aryDate.Count - 1
j = j + 1
If e.Row.RowType = DataControlRowType.DataRow Then
drv = e.Row.DataItem
ob = drv(aryDate.Item(i))
If ob.ToString.Contains("Work") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Blue
ElseIf ob.ToString.Contains("Off") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.LightBlue
ElseIf ob.ToString.Contains("Out") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Gray
ElseIf ob.ToString.Contains("Add New") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.White
End If
End If
Next
End Sub
End Class
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To aryDate.Count - 1
For k As Integer = 0 To aryHyperLinkIndex.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
If Trim(e.Row.Cells(2).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
e.Row.Cells(2).Text = e.Row.Cells(2).Text
If Trim(e.Row.Cells(2).Text) = "Add New" Then
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(2).Text
link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & m(aryHyperLinkIndex.Item(k).split("-")(3))
e.Row.Cells(2).Controls.Add(link)
Else
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(2).Text
link.NavigateUrl = "ViewSchedule.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
e.Row.Cells(2).Controls.Add(link)
End If
Exit For
End If
End If
End If
Next
Next
For i As Integer = 0 To aryDate.Count - 1
For k As Integer = 0 To aryHyperLinkIndex.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
If Trim(e.Row.Cells(3).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
e.Row.Cells(3).Text = e.Row.Cells(3).Text
If Trim(e.Row.Cells(3).Text) = "Add New" Then
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(3).Text
link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & Trim(aryHyperLinkIndex.Item(k).split("-")(3))
e.Row.Cells(3).Controls.Add(link)
Else
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(3).Text
link.NavigateUrl = "ViewSchedule.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
e.Row.Cells(3).Controls.Add(link)
End If
Exit For
End If
End If
End If
Next
Next
End If
myWrite.Close()
End Sub
for "Add New", when i try to hit the hyperlink, its taking all the "Add New" hyperlinks to a single index values. its not taking to corresponding index value.
for example: First Add New - index 10
Second Add New - index 12
Third Add New - index 14
while creating hyperlink its assigning correct index number to each hyperlink. but when i hit hyperlink, it always take index 14. always the last index is assigning to all the Add New's.
is there anything wrong in this code? if you have any idea, why hyperlink is not getting the correct index, then can you please help me to figure this out...
Thanks in advance...
-
Jan 4th, 2011, 03:19 AM
#28
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hello,
Just to confirm something, once the page is rendered in the browser, can you right click and view source on the page. Then do a text search for Add New, can you confirm that each href, has the same index value?
Is it possible that you can provide some explanation has to what you are doing in each of the above loops, and why? It is a little hard to follow.
Also, the only place that I can see you creating an href is here:
Code:
Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>")
But this would appear to be hardcoded to always go to index 81.
Gary
-
Jan 4th, 2011, 11:14 AM
#29
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Sorry.... that was a mistake i did while typing...
Actually instead of this codes
Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>")
i'm using this
Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
while typing in this forum, i made that mistake...
i will give some idea what i'm trying to so with this code...
inside database the data is saved like this
Index-------Name--------------Date-----------------Description
1------------John----------------12/1/2010---------- Work
2------------Peter---------------12/2/2010---------- Out
3------------John----------------12/3/2010---------- Off
and i want the above details to display like this in web page
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
For k As Integer = 0 To aryName.Count - 1
If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
aryDate_Desc.Clear()
'for each name, need to get date, description, color and recno
MySQL = "select ondate, description, color, recno from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
'adding values to "#" row.
Row(" # ") = h
'adding values to "Name" row.
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
'adding date, description, index, name to an array. so while creating hyperlink, we can pass the index values
aryHyperLinkIndex.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(3) & " - " & Trim(aryName.Item(k).ToString))
End While
For i As Integer = 0 To aryDate.Count - 1
gBool = False
For j As Integer = 0 To aryDate_Desc.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
'adding value to "Date" row.
Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
gBool = True
GoTo NextArrayValue
End If
Next
NextArrayValue:
If gBool <> True Then
'adding value as "Add New" to "Date" row, if no value is set.
Row(Trim(aryDate.Item(i))) = "Add New"
'adding date, description, index, name to an array. so while creating hyperlink, we can pass the index values
aryHyperLinkIndex.Add(Trim(aryDate.Item(i)) & " - " & "Add New" & " - " & "0" & " - " & Trim(aryName.Item(k).split("-")(0)).ToString)
End If
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
End If
Next
'for new group, we need to create empty row (empty row will seperate each group)
'Create a new row
Row = Table_MAINPAGE.NewRow()
Row(" # ") = ""
Row("Name") = ""
For i As Integer = 0 To aryDate.Count - 1
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = ""
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
Next
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
'checking each Date
For i As Integer = 0 To aryDate.Count - 1
For k As Integer = 0 To aryHyperLinkIndex.Count - 1
'if Date and aryHyperLinkIndex are equal
If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
'if current cell2 (first Date) text is equal to aryHyperLinkIndex description
If Trim(e.Row.Cells(2).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
'if current cell1 (Name) text is equal to aryHyperLinkIndex name
If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
e.Row.Cells(2).Text = e.Row.Cells(2).Text
'if current cell2 (first date) text is equal to "Add New", then create Hyperlink
If Trim(e.Row.Cells(2).Text) = "Add New" Then
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(2).Text
'Passing the date and name
link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & m(aryHyperLinkIndex.Item(k).split("-")(3))
e.Row.Cells(2).Controls.Add(link)
Else
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(2).Text
'Passing the index
link.NavigateUrl = "ViewUpdate.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
e.Row.Cells(2).Controls.Add(link)
End If
Exit For
End If
End If
End If
Next
Next
End If
End Sub
GridView1_RowDataBound
aryHyperLinkIndex contain - date, description, index, name of all items in each row.
if Date and aryHyperLinkIndex are equal
if current cell2 (first Date) text is equal to aryHyperLinkIndex description
if current cell1 (Name) text is equal to aryHyperLinkIndex name
if current cell2 (first date) text is equal to "Add New"
then create Hyperlink for Add New and Passing the date and name
else
create HyperLink for Text and Passing the index
end if
for HyperLink Text, its taking the correct index and it works fine.
But for Add New HyperlLink, i'm passing date and name. its getting the correct name. but date is set to the last date.
i check the view source and for Add New, its taking the last Date for all.
<td>1</td><td>John </td><td style="background-color arkOrange;"><a href="ViewUpdate.aspx?ViewUpdateValue=102">Work</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=John">Add New</a></td>
<td>2</td><td>Peter </td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=Peter">Add New</a></td><td style="background-color:OliveDrab;"><a href="ViewUpdate.aspx?ViewUpdateValue=103">Off</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&NameValue=Peter">Add New</a></td>
is there anything wrong in this code? if you have any idea, why hyperlink is not getting the correct date, then can you please help me to figure this out...
Thanks in advance...
-
Jan 4th, 2011, 03:50 PM
#30
Thread Starter
Addicted Member
Re: Create Dynamic Hyperlink to GridView VB \ ASP.NET
Gary, Thank you so much for helping me to solve this problem..... you did a good job helping me to fix this and to direct me in rightway.... Thanks you so much....
it start working....
As in your code, i used DataControlFieldHeaderCell to get the Header, so that i can check that date inside the aryHyperLinkIndex array.
adding code here. so later if someone have this problem, they can check this.
Code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
'gets first date cell's header
Dim Header_Date1 As DataControlFieldHeaderCell = CType(Me.GridView1.HeaderRow.Cells(2), DataControlFieldHeaderCell)
For k As Integer = 0 To aryHyperLinkIndex.Count - 1
'checks if first date cell's header and AryHyperLinkIndex date are equal
If Trim(Header_Date1.Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
'if Current cell2 (first Date) text is equal to aryHyperLinkIndex description
If Trim(e.Row.Cells(2).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
'if current cell1 (Name) text is equal to aryHyperLinkIndex name
If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
e.Row.Cells(2).Text = e.Row.Cells(2).Text
'if current cell2 (first date) text is equal to "Add New", then create Hyperlink
If Trim(e.Row.Cells(2).Text) = "Add New" Then
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(2).Text
'Passing the date and name
link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(Header_Date1.Text) & "&NameValue=" & Trim(aryHyperLinkIndex.Item(k).split("-")(3))
e.Row.Cells(2).Controls.Add(link)
GoTo SecondCell
Else
Dim link As HyperLink = New HyperLink()
link.Text = e.Row.Cells(2).Text
'Passing index
link.NavigateUrl = "ViewUpdates.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
e.Row.Cells(2).Controls.Add(link)
GoTo SecondCell
End If
End If
End If
End If
Next
SecondCell:
End If
End Sub
Once again Thanks you so much for your help....
-
Jan 5th, 2011, 04:29 AM
#31
Re: [RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
Hello there,
Glad to hear that you got it working!!
I was happy to help you!
Gary
-
Jan 5th, 2011, 04:30 AM
#32
Re: [RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
Ooooo, I have just seen that you use GoTo.
Although this isn't in itself a bad thing, it has been known to lead to problems, and as a general rule, you shouldn't really be using it in your code.
Gary
-
Jan 6th, 2011, 02:20 PM
#33
Thread Starter
Addicted Member
Re: [RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
Thanks Gary for your reply...
What's the alternative for GoTo. instead of GoTo SecondCell, i tried Exit For. but that's not working. i don't know why. but while doing Exit For its not exiting the for loop completly and moving to the next one.
Thanks in Advance...
-
Jan 7th, 2011, 02:39 AM
#34
Re: [RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
Yes, Exit For is definitely the correct approach.
Sometimes, when you have nested For loops, that approach doesn't work, as you can't "break" out of the parent loop, however, there are way around this.
For instance, you can use a condition. i.e. create a boolean, which is initially false. Then, when you "find" the thing you are looking for, set this boolean to true. This is then your trigger to exit all the loops. So, as the last line in your loop, check the value of this boolean and if it is true, Exit For.
Gary
-
Jan 12th, 2011, 01:37 PM
#35
Thread Starter
Addicted Member
Re: [RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
Once again thanks Gary for your help....
you are right. when i try to use "Exit For", it didn't work. But this method of creating boolean, worked....
Thanks a lot for your Help....
-
Jan 12th, 2011, 09:15 PM
#36
Re: [RESOLVED] Create Dynamic Hyperlink to GridView VB \ ASP.NET
Not a problem at all.
Happy to help
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|