Umbraco usercontrol: Get usercontrol.property.Id within the usercontrol itself?

余生长醉 提交于 2019-12-10 11:36:11

问题


i'm having some trouble with the umbraco usercontrol.. I'm trying to develop a Photo Gallery.. I have a usercontrol with a mediapicker on it, and on save i would like to generate thumbnails of all mediafiles in the picked folder. So far so good..

It is possible that 1 document contains more than one of the Photo Gallery properties, so to determine the path for storing the thumbnails i have to do something like this:

'PhotoGalleryStorageFolder/{DocumentID}/{UsercontrolPropertyId}'

Retrieving the document id was easy:

_currentNodeId = int.Parse(Request.QueryString["id"]);

The problem is that i do not know the alias and i dont want to hardcode it in my usercontrol as there can be more instances of it..

Code:

    private Int32 _currentNodeId = -1;
    private Int32 _currentPhotoGalleryId = -1;
    private String _value = ""; // Holds MediaPickerValue;

    protected void Page_Load(object sender, EventArgs e)
    {
        initialize();
    }

    #region Initialize
    private void initialize()
    {
        _currentNodeId = int.Parse(Request.QueryString["id"]);

        /////// PROBLEM \\\\\\\\\
        _currentPhotoGalleryId = -1; // How to retrieve this?!?!
        \\\\\\\ PROBLEM /////////

        Document d = new Document(_currentNodeId);

        if (!Page.IsPostBack)
        {
            this.setMediaPickerValue(_value);
            this.setPrevMediaPickerValue(_value);
        }
        else
            _value = this.getMediaPickerValue();

        Response.Write("_currentNodeId: " + _currentNodeId.ToString() + "<br />");
        Response.Write("_value: " + _value + "<br />");
    }

    void Document_AfterSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)
    {

    }

    #endregion

    #region MediaPickerValue
    private String getMediaPickerValue()
    {
        return mediaPicker.Value;
    }

    private void setMediaPickerValue(String value)
    {
        mediaPicker.Value = value;
    }

    private String getPrevMediaPickerValue()
    {
        return prevMediaPickerValue.Value;
    }

    private void setPrevMediaPickerValue(String value)
    {
        prevMediaPickerValue.Value = value;
    }
    #endregion

    #region OnSave
    private void onSave()
    {
        String currentValue = this.getMediaPickerValue();
        String prevValue = this.getPrevMediaPickerValue();

        if (currentValue != prevValue) HttpContext.Current.Response.Write("Hergenereer thumbs.<br />");
        else HttpContext.Current.Response.Write("Thumbs hoeven niet opnieuw te worden gegenereerd.<br />");

        this.setPrevMediaPickerValue(currentValue);
    }
    #endregion

    public object value
    {
        get
        {
            onSave();
            return this.getMediaPickerValue();
        }
        set
        {
            Response.Write("Set triggered<br />");
            String val = string.IsNullOrEmpty(value.ToString())  || value == null ? "" : value.ToString();
            _value = val;
        }
    }

Any help on this would be great.. Thnx in advance!


回答1:


Running round controls in a page or a usercontrol is simple enough (http://msdn.microsoft.com/en-us/library/20zys56y(v=vs.90).aspx)

As is getting the properties of an object ((http://msdn.microsoft.com/en-us/library/aky14axb.aspx))

EG Running round the properties of any text boxes:

ControlCollection controls = this.Controls;
foreach(Control control in controls)
{
  if (control.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
  {
    // if this is the control you are interested in you can loop round its properties
    // something like 
    foreach (var prop in control.GetProperties())
    {



回答2:


I ended up adding an extra textbox (only visible to admins) to the usercontrol, so the developer can now define a subfolder for each instance of the PhotoGallery. Thnx for all the help anyway.



来源:https://stackoverflow.com/questions/17504534/umbraco-usercontrol-get-usercontrol-property-id-within-the-usercontrol-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!