Solved

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

Posted on 2011-09-30
5
368 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquey and footrable 2 34
Authentication of Web Services 3 46
Not showing page correctly 3 29
Gridview alignment 1 0
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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now