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).


    protected override void CreateChildControls()
       weatherUserControl control = (weatherUserControl)Page.LoadControl(_ascxPath);
       control.ParentWebPart = this; 

    DefaultValue("<valid zip code here>"),
    WebDisplayName("Zip Code"),
    WebDescription("Please enter zip code")]
    public string pZipCode


    public weather ParentWebPart

    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);


<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?

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

  • 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, 2011 at 21:15
  • this.pZipCode = txtZipCode.Text; this.SaveProperties = true;
    – Vamsi
    Sep 21, 2011 at 20:52

I see where you are going. Try this:

protected void btnSubmitZipCode_Click(object sender, EventArgs e)     
    //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);
      System.Web.UI.WebControls.WebParts.WebPart wp = wpm.WebParts[ParentWebPart.ID];
      wpm.Web.AllowUnsafeUpdates = true;
      weather weatherPart = wp as weather;
      weatherPart.pZipCode = zip;
       wpm.Web.AllowUnsafeUpdates = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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