在自定义控件的构造函数中保持ViewState状态,(注意:如果不在构造函数中使用ViewState,它是可以保持的,可能是页面的处理问题,待解决)

public class FileUploadControl : System.Web.UI.Control


{

public FileUploadControl()


{

if(m_FileUploadWebService == null)


{

m_FileUploadWebService = new FileUploadWebService.FileUpload();

m_FileUploadWebService.Url = System.Configuration.ConfigurationSettings.AppSettings["UploadWebPage"].ToLower().Replace("default.aspx", "FileUpload.asmx");

}

if(this.UploadID == string.Empty)


{

this.UploadID = Guid.NewGuid().ToString("N");

m_FileUploadWebService.Initialize(this.UploadID);

}

if(ViewState["Borderss"] == null)


{

this.Borderss = !this.Borderss;

}

}

private string UploadID


{

set


{

ViewState["UploadID"] = value;

}

get


{

if(ViewState["UploadID"] != null)


{

return ViewState["UploadID"].ToString();

}

else


{

return string.Empty;

}

}

}


-- 重写父类的方法 --#region -- 重写父类的方法 --

protected override void Render(HtmlTextWriter writer)


{

base.Render (writer);

writer.WriteBeginTag("iframe");

writer.WriteAttribute("id", this.ClientID);

writer.WriteAttribute("name", this.UniqueID);

writer.WriteAttribute("width", "400");

writer.WriteAttribute("frameborder", "0");

if(this.Borderss)


{

this.m_Style = this.m_Style.Replace("#000000", this.BorderColor);

writer.WriteAttribute("style", this.m_Style);

}

writer.WriteAttribute("src", System.Configuration.ConfigurationSettings.AppSettings["UploadWebPage"].Trim() + "?id=" + this.UploadID);

writer.WriteAttribute("uploadid", this.UploadID);

writer.Write(">");

writer.WriteEndTag("iframe");

}



protected override object SaveViewState()


{

// Save State as a cumulative array of objects.

object BaseViewState = base.SaveViewState();

object[] AllStates = new object[2];

AllStates[0] = BaseViewState;

AllStates[1] = this.UploadID;

return AllStates;

}


protected override void LoadViewState(object savedState)


{

if (savedState != null)


{

// Load State from the array of objects that was saved at ;

// SavedViewState.

object[] MyState = (object[])savedState;

if (MyState[0] != null)


{

base.LoadViewState(MyState[0]);

}

if (MyState[1] != null)


{

this.UploadID = (string)MyState[1];

}

}

}

#endregion

}
