Probably best to ignore all things outside of the web user control!
I have a web user control which contains a <asp:image/>, three
<asp:TextBox/> controls and a PhotoId property.
What I would like to do is instantiate a new instance of this control
using something similar to: -
<mc:PhotoThumb runat="server" ID="photoThumbnail" PhotoId="1"/>
which when loaded goes to the DB and gets the image location string and
sets the ImageURL and gets each of the three meta data elements and sets
the TextBoxes Text value.
My question is when should I call my function
private void LoadFromDB()
I have called it from the image's DataBinding event, and it works.
However this doesn't seem like the correct place.
Sorry I can't copy the code I do not have access to it from this terminal.
TIA
MC
[quoted text, click to view] Walter Wang [MSFT] wrote:
> Hi,
>
> Without your actual code, I'm not sure I fully understand how currently
> you're doing.
>
> Is the image data stored in db or just the path of the image stored in db?
> When you mean "from the Image DataBinding event handler", do you mean the
> Repeater's ItemDataBound event? If this is the case, the PhotoID is
> property assigned and you could safely use that to query the data from db
> and set other properties accordingly.
>
> Sincerely,
> Walter Wang (wawang@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
>
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif > ications. If you are using Outlook Express, please make sure you clear the
> check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
> promptly.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
>
http://msdn.microsoft.com/subscriptions/support/default.aspx. > ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
If I call the method from the controls Page_Load the Property has not
been initialized yet!
currently I've moved my call to LoadFromDB to the PreRender Event
handler, by which time the Property passed in the markup has been
initialized?
My question still stands, I can make it work by putting it in all sorts
of places, but where is the correct one.
At the Point that Page_Init and Page_Load fire the property has a value
of -1 (the default). Is there an attribute I should be applying to the
property? or any other way to get the property initialized sooner?
Is there a more appropriate group for this question?
[quoted text, click to view] Walter Wang [MSFT] wrote:
> Hi,
>
> The image's DataBinding event will only be fired when your usercontrol is
> putting inside a control that is binding to data. You should put the call
> to LoadFromDB inside your usercontrol's Load event:
>
> <%@ Control Language="C#" AutoEventWireup="true" ...
>
>
> protected void Page_Load(object sender, EventArgs e)
> {
> if (!Page.IsPostBack)
> {
> LoadFromDB();
> }
> }
>
> This is assuming your image and TextBox have enabled ViewState; if not
> enabled, you should not check Page.IsPostBack and call LoadFromDB() in
> every postback.
>
> Regards,
> Walter Wang (wawang@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
Hi,
The Page_Load should be fine if the property is assigned in declarative
statement just as you described in your previous example code. However, for
data binding scenario, you're right that in this time the property is not
assigned yet. I'm sorry that I didn't mentioned that previously.
I understand your original question is how to find the correct time to do
the db query. To answer it, we need to know more about the Page's Life
Cycle:
#ASP.NET Page Life Cycle Overview
http://msdn2.microsoft.com/en-us/library/ms178472.aspx From above article, you will see PreRender is the correct place to do this:
<quote>
Before this event occurs:
The Page object calls EnsureChildControls for each control and for the
page.
Each data bound control whose DataSourceID property is set calls its
DataBind method. For more information, see Data Binding Events for
Data-Bound Controls below.
The PreRender event occurs for each control on the page. Use the event to
make final changes to the contents of the page or its controls.
</quote>
Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Don't see what you're looking for? Try a search.