Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

22 Experts available now in Live!

Get 1:1 Help Now