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 created a custom app page with two user controls:
asp:TextBox ID="SiteTitle" runat="server" and
asp:Button ID="UpdateSubmit" runat="server" Text="Update"

The goal is to have the same behavior as the _layouts/prjsetng.aspx page
So checking the current Title, change it and update it.

For some reason the below code is not updating the title. (Rights are ok running this as site col admin)

Code behind:

protected void Page_Load(object sender, EventArgs e) {

       string Title = SPContext.Current.Web.Title;
       SiteTitle.Text = Title;

UpdateSubmit.Click += new EventHandler(UpdateSubmitButton_Click); }

private void UpdateSubmitButton_Click(object sender, EventArgs e) {
SPWeb oweb = SPContext.Current.Web;
oweb.Title = SiteTitle.Text.ToString();
oweb.Update();
}

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Page_Load fires before UpdateSubmitButton_Click

I would refactor your code to be:

protected override void OnInit(EventArgs e) {
    UpdateSubmit.Click += new EventHandler(UpdateSubmitButton_Click);
}

protected void Page_Load(object sender, EventArgs e) {
    if(!IsPostBack) {
        DataBind(SPContext.Current.Web):
    }
}

public void DataBind(SPWeb web) {
    SiteTitle.Text = web.Title;
}

private void UpdateSubmitButton_Click(object sender, EventArgs e) {
    SPWeb oweb = SPContext.Current.Web;
    oweb.Title = SiteTitle.Text.ToString();
    oweb.Update();
    DataBind(oweb);
}
  1. Always try to bind your event handlers as early as possible (OnInit)
  2. Bind control values in a databind event
  3. In OnLoad have a IsPostBack clause to prevent double DataBinds
  4. After events, databind the new values
share|improve this answer
    
Thanks for pushing me on the right track! - VS2010 throws an error on the OnInit: Method must have a return type –  Wim Hill Jan 28 '11 at 22:17
    
ah missing a "void" –  djeeg Jan 29 '11 at 2:04
    
Thanks... Now I've an error "no suitable method found to override" on the OnInit one... –  Wim Hill Jan 29 '11 at 19:19
    
remove sender arguement –  djeeg Jan 30 '11 at 4:13
    
Thanks again, code is running fine now! –  Wim Hill Jan 30 '11 at 16:17
add comment

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.