Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formview with links containing the ID

Status
Not open for further replies.

kylua

Technical User
Sep 30, 2002
199
GB
This HAS to be dead simple. But nearly two hours of googling hasn't found the answer.
I want to show a formview containing part of an employee's data. And I want to provide links to pages that display other parts of the data for the displayed record.
So the question is very simple:
How do I add the value of the EmployeeID field from the formview to the end of the hyperlink as a parameter?
Simple <%# Bind("EmployeeID") %> slotted into an HTML link didn't work.
<asp:HyperLink ID="HyperLink1" runat="server" >NavigateUrl="~/Level1/salary.aspx?EmpID="<%# Bind("EmployeeID") %>">Salary Details</asp:HyperLink>
Didn't work. (Bind not declared)
I tried binding a hidden field to the value and then referencing that, it didn't work.
<%# DataBinder.Eval(Container.DataItem,"filename").ToString()%> (Container not part of UI)

This must surely be something used all the time but I just cannot find the syntax to do it!
Everything else in the formview is working fine.
 
OK, problem solved:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "salary.aspx?EmpID=" & DataBinder.Eval(Container.DataItem,"EmployeeID").ToString()%>'>Salary Details</asp:HyperLink>
But it has to be inside one of the templates.
(Oh, I also had to use single quotes, double quotes didn't work)

Now maybe I can get some work done!!!
 
I would be easier and better to set the NavigateURL in the codebehind in the DataBound event.
 
[highlight #FF99FF]It would be easier and better to set the NavigateURL in the codebehind in the DataBound event. [/highlight]
Sounds better, but how do I access the data retrieved by the formview in the code behind?

And this is probably worth a new thread but it is blindingly obvious to me that the Formview is screaming out for a search function within it.
I've managed to rig one with codebehind but it instantly forgets the one found when the user clicks edit, or having searched in edit mode, forgets the record they were on when they click update.
There MUST be a simple control to add to be able to search for a record in 'view', then click on 'eidt' and it 'edits' that record, then click on update and it updates it, reverting to viewing that record.
If there isn't, why not?
 
Got a workaround after mucking about with the formview's Pageindex property and indexchanged event to no avail.
Simply put this into the Loadcomplete event for the page, (doesn't work on load):
If txt1.Text <> "" Then
SqlDS1.SelectCommand += " where lastname = '" & txt1.Text & "'"
SqlDS1.DataBind()
End If
But this seems to me be binding to the database twice, surely there must be a more efficient way?
 
Code:
Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
Dim s As String = rowView.Item("ColumnName").ToString
 
Thanks JB, once I worked out which question you were answering that was very helpful :)
It gives a bit more of an insight into how microsoft handle the way formviews work.
Personally, I've been very tempted to dump the entire lot and go back to asp today. Especially as changes in one page change another, suddenly the edit stops working so you have to reconfigure the connection and redo the schema which means re-editing the templates back to english labels instead of column names.
BUT!
I think I about have it cracked, so should get this largely polished off tomorrow.
But then, I said that when I went home last night.
 
Code:
Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
Dim s As String = rowView.Item("ColumnName").ToString
OK, using this just to store the name in a session variable as it references a table without a name field.
Or trying to.
Keeps coming up:
Object reference not set to an instance of an object.
Made sure I have bound the formview first.
Changed column name to an integer reference as it expects an integer
Removed 'tostring'
But when I am debugging 'rowview' is still 'nothing', becos formView1.dataitem is 'nothing'.
FormView1 is not nothing, but DataItemCount = 0.
The impression I get is that I am trying to obtain the data before it's arrived.
I'm doing this in load_complete and I have done a databind just before hand.
So I'm just lost!
Code:
SqlDS1.DataBind()
        Dim rowView As DataRowView = CType(FormView1.DataItem, DataRowView)
        Session("FN") = rowView.Row("FirstName").ToString


 
I see 2 problems with your code:
Code:
Dim rowView As DataRowView = CType([red]FormView1[/red].DataItem, DataRowView)
''Did you change the name of the formview to your formview name in the code above.  Formview1 was the name in my test page.

Session("FN") = rowView.Row[red].Item[/red]("FirstName").ToString 
''You are missing the .Item in the line above
 
First error, not the problem. I kept the default name as well.
Second error, it was the last in a long list of changes that I had made in the hope of finding the problem. And changing it to row.item made no difference.
The problem that I see here is not the second line, it's the first. Becos I put a breakpoint on the second line and the rowview is nothing.
This is becos the Formview1.dataitem is nothing at the point of setting rowview to it.
Formview1 itself is not nothing, it has a pageindex of 0 for example. And a DataItemCount of 0 as well.
 
Problem solved!
I was databinding the connection, not the formview. I just added formview1.databind in the line before the code and it worked fine.
 
Great! I'm glad you found the issue.
If you are using the datasource controls, I suggest you get rid of them, and write the code yourself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top