• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1113
  • Last Modified:

Need to have HyperLink column in Datagrid link to another page and pass info to it.

I have a Datagrid called classGrid set up on a page called Classes.aspx.  I added a HyperLink column to the Datagrid using Property Builder.  I want to be able to click this hyperlink and it open another page called Edit_Classes.aspx and pass the info that was in the Datagrid for that specific record.  This Edit_Classes.aspx page is essentially the 'details' page for my Classes.aspx page.
I am not sure how to accomplish what I want.
I am very unfamilliar with ASP.NET so please bear with me.

Here is the code behind that I have for my Classes.aspx.  (At the moment I have a Edit, Update, Cancel Column but I think I will get rid of this.)


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        classDataAdapter.Fill(classDataSet)
        If Not IsPostBack Then
            classGrid.DataSource = classDataSet.Classes.DefaultView
            classGrid.DataKeyField = "class_name"
            classGrid.DataBind()
        End If
    End Sub

    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles classGrid.SelectedIndexChanged

    End Sub

    Private Sub classGrid_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles classGrid.SortCommand
        Dim SortView As DataView = classDataSet.Classes.DefaultView
        Dim CurrentSort As String = ""
        If Not ViewState("Sort") Is Nothing Then
            CurrentSort = ViewState("Sort")
        End If
        If CurrentSort.StartsWith(e.SortExpression) Then
            If CurrentSort.EndsWith("DESC") Then
                SortView.Sort = e.SortExpression
            Else
                SortView.Sort = e.SortExpression & " DESC"
            End If
        Else
            SortView.Sort = e.SortExpression
        End If
        ViewState("Sort") = SortView.Sort
        classGrid.DataSource = SortView
        classGrid.DataBind()
    End Sub

    Private Sub classGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles classGrid.EditCommand
        classGrid.DataSource = classDataSet.Classes.DefaultView
        classGrid.DataKeyField = "class_name"
        classGrid.EditItemIndex = e.Item.ItemIndex
        classGrid.DataBind()
    End Sub

    Private Sub classGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles classGrid.CancelCommand
        classGrid.DataSource = classDataSet.Classes.DefaultView
        classGrid.DataKeyField = "class_name"
        classGrid.EditItemIndex = -1
        classGrid.DataBind()
    End Sub

    Private Sub classGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles classGrid.UpdateCommand
        Dim classTable As DataSet2.ClassesDataTable
        classTable = classDataSet.Classes
        Dim rowToUpdate As DataSet2.ClassesRow
        rowToUpdate = classTable.Rows(e.Item.ItemIndex)
        rowToUpdate.professor = CType(e.Item.Cells(3).Controls(0), _
                                     TextBox).Text
        rowToUpdate.time_start = CType(e.Item.Cells(4).Controls(0), _
                                 TextBox).Text
        rowToUpdate.time_end = CType(e.Item.Cells(5).Controls(0), _
                                  TextBox).Text
        rowToUpdate.room = CType(e.Item.Cells(6).Controls(0), _
                                    TextBox).Text
        rowToUpdate.days = CType(e.Item.Cells(7).Controls(0), _
                                    TextBox).Text

        classDataAdapter.Update(classDataSet)
        classGrid.DataSource = classDataSet.Classes.DefaultView
        classGrid.EditItemIndex = -1
        classGrid.DataBind()
    End Sub

    Private Sub classGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles classGrid.DeleteCommand
        classDataSet.Classes.Rows.Item(e.Item.ItemIndex).Delete()
        classDataAdapter.Update(classDataSet)
        classGrid.DataSource = classDataSet.Classes.DefaultView
        classGrid.EditItemIndex = -1
        classGrid.DataBind()
    End Sub

    Private Sub classGrid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles classGrid.SelectedIndexChanged

    End Sub
0
jettman26
Asked:
jettman26
  • 11
  • 7
  • 7
2 Solutions
 
ayha1999Commented:
Hi,

First make template colum where you want hyperlink.

<Columns>
<asp:TemplateColumn HeaderText="Your title">
      <ItemTemplate>
            <asp:HyperLink ID="Yourid" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"YourFiedToDisplayInTheGrid") %>' NavigateUrl='<%# String.Format("YourPageToTransfer.aspx?VarName={0}",DataBinder.Eval(Container.DataItem, "YourFieldName"))%>'>
            </asp:HyperLink>
      </ItemTemplate>
</asp:TemplateColumn>
...

In the other page you can retrieve it like

Dim txtVar as string = Request.QueryString("VarName")

hope this helps.

ayha
0
 
jettman26Author Commented:
I went into the Property Builder for the DataGrid and added a Template column.  I see no where to add code for it.  Should I be doing this programatically?  I put all the other columns in the DataGrid w/the Property Builder.  Not sure what to do.
0
 
ayha1999Commented:
HI,

After adding template column just close build properties then go html view of the datagrid, locate your template containing you datafield which you want to make hyperlink then select the tempalte and paste my code there and change teh field name you want.

ayha
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tusharashahCommented:
If you go to Property Builder there's a different Option for HyperLink Column. It'll be straight fwd then using Template Column and your ASPX code will look something like following:

<asp:HyperLinkColumn
                HeaderText="Details"
                DataNavigateUrlField="IntegerValue"
                DataNavigateUrlFormatString="detailspage.aspx?id={0}"
                DataTextField="StringValue"
                Target="_new"
            />

Sample: http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/VB/datagrid3.aspx
Source: http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/datagrid3.src

-tushar
0
 
jettman26Author Commented:
ayha1999,
please explain to me what the following do:
HyperLink ID="Yourid"
(Container.DataItem,"YourFiedToDisplayInTheGrid")
("YourPageToTransfer.aspx?VarName={0}
Container.DataItem, "YourFieldName"

tusharashah,
please explain to me what the following do:
DataNavigateUrlField="IntegerValue"
DataTextField="StringValue"
0
 
tusharashahCommented:
Idieally what you should do is, Pass information of Unique field to the new page and retrieve value base on that unique field in new Page from Database. & you can use QueryString for that. I believe in your case ClassName is Unique field since you have it as DataKeyField. so you can modify the HyperLink code that I have given you like following:

<asp:HyperLinkColumn
                HeaderText="Class Name"
                DataNavigateUrlField="class_name"
                DataNavigateUrlFormatString="Edit_Classes.aspx?class_name={0}"
                DataTextField="class_name"
            />

So, at run time your HyperLink column will look something like following (lets say couple of your classname is ClassA, ClassB)

<a id='dg_HyperLink1_1' href='Edit_Classes.aspx?class_name=ClassA'> ClassA </a>
<a id='dg_HyperLink1_2' href='Edit_Classes.aspx?class_name=ClassB'> ClassB </a>

So, arrangment of Fields are like following:
   <a id='dg_HyperLink..' href='Edit_Classes.aspx?class_name=DataNavigateUrlField'> DataTextField </a>

Now, on Edit_Classes.aspx page retrieve value of QueryString in Page_Load event like following, and run query on your unique field:

   Request.QueryString("class_name")

Hope this will provide you with clearer picutre..

-tushar
0
 
jettman26Author Commented:
Thanks tusharashah,
I think it will work.  As far as Edit_classes.aspx, I put in Request.QueryString("class_name") in the page_Load event and VS.NET has a problem with it.  It is underlined and when I put my mouse over it, it says:
'Property access must assign to the property or use its value.'  Not sure why or what to do.
0
 
tusharashahCommented:
Retrive your value in some variable:

Dim strClassName as string = Request.QueryString("class_name")

-tushar
0
 
tusharashahCommented:
Also, it's good idea to check if the QueryString exist everytime you try to use it, something like following:

Dim strClassName As String
If Not (Request.QueryString("class_name") Is Nothing) Then
 strClassName = Request.QueryString("class_name").ToString
End If

-tushar
0
 
jettman26Author Commented:
Sorry tusharashah, I still don't understand what to do on the Edit_classes.aspx page.  I pasted the following code into the page_load.

Dim strClassName As String
If Not (Request.QueryString("class_name") Is Nothing) Then
 strClassName = Request.QueryString("class_name").ToString
End If

I don't understand how to get my fields in Edit_classes.aspx populated with the data.  What exactly do I need to add to Edit_Classes.aspx.

Let me give you a little more info.  On my Classes.aspx page I added a button called New that will open the Edit_classes.aspx page and all the fields will be blank and you can create a new class.  (I have not added the code for this 'New' button in Classes.aspx yet.)  I believe I will have to pass a parameter with this, something like the word New so that Edit_classes.aspx knows that is for a new record instead of Editing an existing record.

If I click the Hyperlink in the Hyperlink Column that you had me create it will pass the class_name parameter and somehow then Edit_classes.aspx will have the fields filled.

So in the end, this is what I need to accomplish.
I will gladly open a new question or two if you would rather have me do that.  

Here is what I have so far for code in Edit_classes.aspx.  The code for the button click is for if this would be a NEW record.


#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 Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents Label7 As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents startText As System.Web.UI.WebControls.TextBox
    Protected WithEvents endText As System.Web.UI.WebControls.TextBox
    Protected WithEvents classText As System.Web.UI.WebControls.TextBox
    Protected WithEvents professorText As System.Web.UI.WebControls.TextBox
    Protected WithEvents roomText As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    Protected WithEvents daysListBox As System.Web.UI.WebControls.ListBox

    '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 strClassName As String
        If Not (Request.QueryString("class_name") Is Nothing) Then
            strClassName = Request.QueryString("class_name").ToString
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myConn As New SqlClient.SqlConnection
        Dim myCommand As New SqlClient.SqlCommand
        Dim myAdapter As New SqlClient.SqlDataAdapter
        Dim CS As String

        CS = "Server=DELL;initial catalog=classes;integrated security=true"
        'uid=USER_NAME;pwd=PASSWORD;"
        myConn.ConnectionString = CS
        myCommand.Connection = myConn
        myAdapter.SelectCommand = myCommand

        myCommand.CommandText = "INSERT INTO CLASSES VALUES ('" & classText.Text & "', '" & professorText.Text & "', '" & startText.Text & "', '" & endText.Text & "', '" & roomText.Text & "', '" & daysListBox.SelectedItem.Text & "')"

        myConn.Open()
        Try
            myCommand.ExecuteNonQuery()
            lblMessage.Text = "Record Successfully Added"
        Catch
            lblMessage.Text = "ERROR: " & Err.Description      '<----- Add a label called lblMessage to your form.
        End Try
        myConn.Close()                  '<------------------------Make sure to close your connection.
    End Sub
0
 
jettman26Author Commented:
I added the following to the Page_Load and it still won't read in.

Dim strClassName As String
        If Not (Request.QueryString("class_name") Is Nothing) Then
            strClassName = Request.QueryString("class_name").ToString
        End If
        Dim myConn As New SqlClient.SqlConnection
        Dim myCommand As New SqlClient.SqlCommand
        Dim myAdapter As New SqlClient.SqlDataAdapter
        Dim CS As String

        CS = "Server=DELL;initial catalog=classes;integrated security=true"
        'uid=USER_NAME;pwd=PASSWORD;"
        myConn.ConnectionString = CS
        myCommand.Connection = myConn
        myAdapter.SelectCommand = myCommand
        myCommand.CommandText = "SELECT * FROM CLASSES " & _
        "WHERE class_name = strClassName"
        myConn.Open()
        Try
            myCommand.ExecuteReader()
            lblMessage.Text = "Record Successfully Read"
        Catch
            lblMessage.Text = "ERROR: " & Err.Description      '<----- Add a label called lblMessage to your form.
        End Try
        myConn.Close()
0
 
ayha1999Commented:
Hi,

If   strClassName is numeric then use

myCommand.CommandText = "SELECT * FROM CLASSES " & _
        "WHERE class_name = " & strClassName  & " "

If strClassName is string then use

strClassName

myCommand.CommandText = "SELECT * FROM CLASSES " & _
        "WHERE class_name = '" & strClassName & "'"

If Insert is not working then change to;

myCommand.CommandText = "INSERT INTO CLASSES(FeldName1,FeldName2,FeldName3,FeldName4,FeldName5) VALUES ('" & classText.Text & "', '" & professorText.Text & "', '" & startText.Text & "', '" & endText.Text & "', '" & roomText.Text & "', '" & daysListBox.SelectedItem.Text & "')"

Put some break point and see you are getting something in strClassName.

hope this helps.

ayha
0
 
jettman26Author Commented:
Thanks.

I put a break point in the line, myCommand.CommandText = "SELECT * FROM CLASSES " & _
        "WHERE class_name = '" & strClassName & "'"

strClassName was set to 'Test Class1' which it should have been.

How do I get my fields to be populated with the data?

As far as the Insert INTO CLASSES, that is working.  Although I will probably need to use an Update for when the record is passed in as opposed to an Insert when the record will be new.

0
 
ayha1999Commented:
-->How do I get my fields to be populated with the data?--<

could u pls. explain, you want to populate datagrid?

do you want string for update?

sorry It is not clear?

ayha
0
 
jettman26Author Commented:
When I click the hyperlink for a specific record on my Classes.aspx page, I want it to populate the text boxes and listBox that I have on my Edit_classes.aspx page.  The data that is populated into the Edit_classes.aspx page is determined by the value that is passed via the hyperlink from the Classes.aspx page to the Edit_classes.aspx page.

The value that is getting passed from Classes to Edit_classes is the 'class_name'.  A query needs to be done to get all the info from that particalar class name record.  
Example:  Lets say that the class name passed from Classes.aspx to Edit_Classes.aspx is Class A.
The query myCommand.CommandText = "SELECT * FROM CLASSES " & _
        "WHERE class_name = '" & strClassName & "'"
is done to get the info for the class name Class A.  i.e., professor, start_time, end_time, room, days.

Now I need to have the data for the record Class A populated into my textBoxes and listBox that are in Edit_classes.aspx.
class_name, professor, start_time, end_time and room are textBoxes.  days is a listBox.
This is the part that I don't know how to do.

If you need any more info let me know.
0
 
jettman26Author Commented:
Also, after the data for the record is populated into my textBoxes and listBox in Edit_classes.aspx, I need to be able to make changes to all of the fields except for the class_name (since class_name is the primary key) and then click the Save button and it will save the changes back to the database.  

On Classes.aspx if I click on the New button on the bottom of the page, it will take me to Edit_classes.aspx where I can fill in the textBoxes and listBox and Save this NEW record to the database.  When I press the New button I will need to pass a parameter to Edit_classes.aspx telling it that this is for a New record, so not to query the database for an existing record and when I click the Save button on Edit_classes.aspx it will insert a new record instead of trying to update a current record.

Hope this helps
0
 
ayha1999Commented:
Hi,

Why don't you prefer to use Inline datagrid editing instead of editing in form. Datagrid editing is easier.

You can populate like this

 dim sqlstr as string = "SELECT * FROM CLASSES " & _
        "WHERE class_name = '" & strClassName & "'"

dim cmd as SqlCOmmand = new sqlCommand(sqlstr,Youconnection)

try
dim rdr as sqlDataReader = cmd.ExecuteNoneQuery

if rdr.Read() Then
txtbox1.Text = rdr.Item("YourFieldName")
txtBox2.Text = rdr.Item("youfield2")
...

End If
rdr.close

ayha
0
 
tusharashahCommented:
You are doing it right, use SqlDataReader to retrieve information in your page:

Dim strClassName As String
        If Not (Request.QueryString("class_name") Is Nothing) Then
            strClassName = Request.QueryString("class_name").ToString
        End If
        Dim myConn As New SqlClient.SqlConnection
        Dim myCommand As New SqlClient.SqlCommand
        Dim myAdapter As New SqlClient.SqlDataAdapter
        Dim myReader as SqlCleint.SqlDataReader
        Dim CS As String

        CS = "Server=DELL;initial catalog=classes;integrated security=true"
        'uid=USER_NAME;pwd=PASSWORD;"
        myConn.ConnectionString = CS
        myCommand.Connection = myConn
        myAdapter.SelectCommand = myCommand
        myCommand.CommandText = "SELECT * FROM CLASSES " & _
        "WHERE class_name = strClassName"
        myConn.Open()

        Try
            myReader = myCommand.ExecuteReader()
            While myReader.Read
                  lblProfessor.Text = dr["Professor"].ToString
                  ...
                  ...
            End While
            lblMessage.Text = "Record Successfully Read"
        Catch
            lblMessage.Text = "ERROR: " & Err.Description      '<----- Add a label called lblMessage to your form.
        End Try
        myConn.Close()
0
 
jettman26Author Commented:
tusharashah,
VS.NET doesn't like the following statement.
Dim myReader As SqlCleint.SqlDataReader
VS.NET says that "Type 'SqlClient.SqlDataReader' is not defined."

ayha1999,
is Inline datagrid editing when you put in a Edit, Update and Cancel column in the DataGrid so that you can edit it?
0
 
tusharashahCommented:
Try with SqlDataReader:

Dim myReader As SqlDataReader

-tushar

0
 
ayha1999Commented:
Hi,

Of course but you have write code as you do for normal form to insert and update. Retriving data from datagrid is different that you retrive from normal form.

You mispelled client in "dim myReader As SqlCleint.SqlDataReader"

ayha
0
 
jettman26Author Commented:
I spelled SqlClient correctly and VS.NET likes it better.
In the line:
classText.Text = dr["class_name"].ToString

what is dr.=?  VS.NET says that "Name 'dr' is not declared".
I tried Dim dr As DataRow but it still didn't like it.
0
 
tusharashahCommented:
dr is myReader,

since you have Class_name already with you no need to get it from DataReader again.. thou the syntaxt would be something like following:

classText.Text = myReader("class_name").ToString

-tushar
0
 
ayha1999Commented:
If you don't have class then use

Dim MyReader as SqlDataReader

classText.Text = MyReader.Item("Class_Reader").ToString

ayha
0
 
jettman26Author Commented:
That worked.  Thanks you guys.  Not sure how I should split up the points.  I am going to open up another question that builds off of this.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 11
  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now