0

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

1 Answer 1

0

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
4
  • 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, 2011 at 22:17
  • ah missing a "void"
    – djeeg
    Jan 29, 2011 at 2:04
  • Thanks... Now I've an error "no suitable method found to override" on the OnInit one...
    – Wim Hill
    Jan 29, 2011 at 19:19
  • remove sender arguement
    – djeeg
    Jan 30, 2011 at 4:13

Your Answer

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

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