Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a weather UserControl (defaulting to the headquarters location) . I have removed the ability to "Edit Web Part" because of branding and not wanting to have to train users on how to work the "Edit Web Part" box of goodies.

Instead of them using that, I have put a textbox in the webpart for them to enter in the zip code.

For some reason I am having a hard time grasping how to set the value of the zip code to the appropriate value in the code behind. I have posted my code below (for everything incase I have other areas I am messing things up).

weather.cs

    protected override void CreateChildControls()
    {
       //base.CreateChildControls();
       weatherUserControl control = (weatherUserControl)Page.LoadControl(_ascxPath);
       control.ParentWebPart = this; 
       Controls.Add(control);
    }

    [Category("Configuration"),
    Personalizable(PersonalizationScope.User),
    WebBrowsable(true),
    DefaultValue("<valid zip code here>"),
    WebDisplayName("Zip Code"),
    WebDescription("Please enter zip code")]
    public string pZipCode
    {
        get;
        set;
    }

weatherUserControl.ascx.cs

    public weather ParentWebPart
    {
        get;
        set;
    }

    protected void btnSubmitZipCode_Click(object sender, EventArgs e)
    {
        ParentWebPart.pZipCode = txtZipCode.Text; // Not positive about this section
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string setZipCode = "valid zip code here"; // because the default value was not working
        //if (ParentWebPart.pZipCode.ToString() != "")
        //{
        //   setZipCode = ParentWebPart.pZipCode.ToString(); 
            // the default value is not working
        //}

        XmlDocument myXml = new XmlDocument();
        myXml.Load("http://weather.yahooapis.com/forecastrss?p=" + setZipCode);
    }

weatherUserControl.ascx

<asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmitZipCode" CssClass="button" runat="server" 
            Text="Set" onclick="btnSubmitZipCode_Click" />

At present the above code doesn't throw an error, but it doesn't work. Any thoughts?

share|improve this question

2 Answers 2

I think you have complicated your solution :)

Firstly, btnSubmitZipCode_Click will get executed after Page_Load.

there is no need to use the "ParentWebPart" property. If you want remove the ability to change the option in "Edit Webpart" you don't need the property pZipCode.

Simple solution would be :

  1. Seperate the Webservice call which might include render(html) method ( I would even seperate them too).
  2. On the button click just set the zipcode or pass the value to the method created above.

Few points :

  • Make sure not to call the service on every Page Load (only on GET not on POST).
  • Persist the zip code value is required as HTTP is stateless

Give the same requirement to me, I would avoid server side code; just use JQuery with Content Editor webpart and render the weather :)

on the button click

share|improve this answer
    
So, I remove the ParentWebPart function. Then the btnSubmitZipCode_Click would be changed to ParentWebPart.pZipCode = txtZipCode.Text; to be pZipCode = txtZipCode.Text; ?? Also if I don't have the pZipCode how will I retrieve the value from the personalizable variable per user? –  ZombieCode Sep 20 '11 at 21:15
    
this.pZipCode = txtZipCode.Text; this.SaveProperties = true; –  Vamsi Sep 21 '11 at 20:52

I see where you are going. Try this:

protected void btnSubmitZipCode_Click(object sender, EventArgs e)     
{         
    UpdateZip(txtZipCode.Text);
    //refresh the view of the data
} 
private void UpdateZip(string zip)
{
  if(zip != ParentWebPart.pZipCode) // don't bother updating if the value didn't change
  {
    SPLimitedWebPartManager wpm = SPContext.Current.Web.GetLimitedWebPartManager(Page.Request.Path, PersonalizationScope.User);
    try
    {   
      System.Web.UI.WebControls.WebParts.WebPart wp = wpm.WebParts[ParentWebPart.ID];
      wpm.Web.AllowUnsafeUpdates = true;
      weather weatherPart = wp as weather;
      weatherPart.pZipCode = zip;
      wpm.SaveChanges(wp);
    }
    finally
    {
       wpm.Web.AllowUnsafeUpdates = false;
    }
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.