Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-30
5
Medium Priority
?
375 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 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