?
Solved

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

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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