ISettingControl interface with databinding

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
1/14/2014 6:19:51 AM
Gravatar
Total Posts 17

ISettingControl interface with databinding

I need to insert a ISettingControl (dropdownlist or ListBox) whose elements are loaded from a database.

The loading is successful, but when retrieving the selected value that is null.
Below is the code implemented.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ListPagesSetting.ascx.cs" Inherits="srdE20.Web.UI.ListPagesSetting" %>


<asp:ListBox ID="ddPages" runat="server" SelectionMode="Single" >


</asp:ListBox>

 

         private void PopulateControls()
        {
            DataTable dt = srdE20_CalendarEvent.GetPages();
            this.ddPages.DataSource = dt;
            ddPages.DataTextField = "PageName";
            ddPages.DataValueField = "PageId";
            ddPages.DataBind();
        }

 

      #region ISettingControl

            
            public string GetValue()
            {
                return ddPages.SelectedValue;
            }

            public void SetValue(string val)
            {
                if (ddPages.Items.Count == 0)
                {
                    PopulateControls();
                }
                
                ListItem item = ddPages.Items.FindByValue(val);
                if (item != null)
                {
                    ddPages.ClearSelection();
                    item.Selected = true;
                    //txtValue.Text = val;
                }
            }

        #endregion

 

when I access the settings page, the control is properly populated. But when I saving the data no value is selected, indeed it would seem that the control is no longer populated

Can anyone suggest me where am I wrong

I apologize for my bad english :)

1/14/2014 9:06:45 AM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: ISettingControl interface with databinding

You only want to bind the list if it's not a postback, such as this:

private void PopulateControls()
{
           if (!Page.IsPostBack)  {

            DataTable dt = srdE20_CalendarEvent.GetPages();
            this.ddPages.DataSource = dt;
            ddPages.DataTextField = "PageName";
            ddPages.DataValueField = "PageId";
            ddPages.DataBind();
        }
}

 

1/14/2014 9:45:12 AM
Gravatar
Total Posts 18439

Re: ISettingControl interface with databinding

You can also study an existing ISettingControl that is populated from the database and model your code in a similar way. For example our CountryStateSetting which uses 2 dropdown lists, one for country and one for state but since it is a single setting the get and set use a pipe separated country and state. It also uses an updatepanel since it needs to update the state list if the country is changed.

1/16/2014 2:38:32 AM
Gravatar
Total Posts 17

Re: ISettingControl interface with databinding

Thank you very much. With the example "CountryStateSetting" I solved my problem. I offered you a well-deserved beer.  :)

1/16/2014 6:17:17 AM
Gravatar
Total Posts 18439

Re: ISettingControl interface with databinding

Thanks for the beer! Much appreciated.

Cheers,

Joe

You must sign in to post in the forums. This thread is closed to new posts.