Solved

Can I set a DetaiView control's datasource at runtime?

Posted on 2011-09-30
5
372 Views
Last Modified: 2012-08-13
Hi, i'm using asp.net 3.5 and C#.
I have a web page where I display all items in a gridview control whose Auto-generate Select is enabled.  When a user selects a row(item), I save the item id in session using the Gridview control's Select event.  After the user selects an item and then select Detail in the menu bar, this will take them to the item's detail page where I have a DetailView control.  

I create a sqldatabase in the Page_load event when it's !IsPostBack.  Because I have to get the itemid from the session variable so i can only do this at runtime.  Is this the best way to do this?  I get a runtime error about I didn't set the datakey field.  but when I tried to do this in backend code I get tooltip that his field is read only and not for assignment.  What am I doing wrong here?  Thank you.
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
yogsoft earned 250 total points
ID: 36896869
I think what you are doing is correct, however there is another way of getting Previous page value using "PreviousPage" attribute to get selected itemid.

Regarding DataKey error. DataKey property has to be set at design time in aspx/ascx.

Hope this has helped you understanding DataKey error.
0
 
LVL 4

Assisted Solution

by:Friman001
Friman001 earned 250 total points
ID: 36896896
What you can do is pass the variable through the Query String.  When you redirect to the Item Detail's page, just add "...aspx?ItemId=19387459273" So:
string itemId = 19387459273;
Response.Redirect("~/Item/ItemDeatil.aspx?ItemId=19387459273");   
// or the following
this.HyperLink1.NavigateURL = "~/Item/ItemDeatil.aspx?ItemId=19387459273";

Open in new window

.  Once you are at the Item Detail's page, you can pull that variable out and use it how ever you want by using Request.QueryString["ItemId"].ToString().
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36896901
Opps.  I should be
"~/Item/ItemDetail.aspx?ItemId=" + itemId

Open in new window


Sorry
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36896913
I guess not getting the strong coffee this morning was a bad idea.  You want to save the itemId as an int probably and then convert it to a string when you add it to the end of the URL.  Then, when you pull it back out of the Query String, you can convert it back to an int.

Hopefully I am leading you where you were looking!!!
0
 

Author Comment

by:lapucca
ID: 36897574
It doesn't matter I put it in the url as parm or store in session my question here is setting the datasourde for the viewdetail control.  I created a sqldatasource control at page_load event.  Anyway, please read over again my question.  Thank you.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question