VS2005public class Label : WebControl, IPostBackDataHandler用户控件的TagKeys为spanLabel类实现了IPostBackDataHandler接口,当把用户控件的TagKey改为Input(Text)类型的时候在主Form回发后能接收到修改后的Text值.为用户控件(Span标记)添加自定义属性的方式:
1.用户控件内部通过Page.ClientScript.RegisterStartupScript(自身ID.自定义属性名=值)
2.Form另写好JS修改该用户控件的自定义属性值.
3.Form回发.
4.提取该自定义属性值赋值给用户控件的某个属性并将值保存至ViewState.
5.流程完毕.问题:TagKeys为span模式下通过js为服务段控件添加一属性(可复合型、数组,需考虑序列化)后,然后Form回发,用户控件如何提取该js生成的变量的值.
1.用户控件内部通过Page.ClientScript.RegisterStartupScript(自身ID.自定义属性名=值)
2.Form另写好JS修改该用户控件的自定义属性值.
3.Form回发.
4.提取该自定义属性值赋值给用户控件的某个属性并将值保存至ViewState.
5.流程完毕.问题:TagKeys为span模式下通过js为服务段控件添加一属性(可复合型、数组,需考虑序列化)后,然后Form回发,用户控件如何提取该js生成的变量的值.
数据体的过程就是相当于序列化.
========
1.hidden用服务器控件,直接取value就可以了.
2.如果手功生成html的hidden 用 Request.Form["hidden的name"]就可
注意这个name的命名不要重复,加上控件的ClientID
我先去试试,从来没用过hidden- -#
不要.后面再加点字符区别
string postedValue = postCollection[postDataKey];
Text = postedValue;
string presentValue = Text;
if (presentValue == null || postedValue != presentValue)
{
Text = postedValue;
return true;
}
return false;
}这里不需要做更改?
{
base.RenderContents(writer);
writer.Write(这里应该没办法输出服务段的hidden控件吧);
//有点不想手动输出HTML的hidden,想把获取值的地方集中在LoadPostData函数里面.
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;namespace BFY.WEB.Controls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Label runat=server></{0}:Label>")]
public class Label : WebControl,IPostBackDataHandler,IPostBackContainer
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)] private static readonly object EventTextChanged = new object();
protected override HtmlTextWriterTag TagKey
{
get
{
return base.TagKey;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
//writer.AddAttribute(HtmlTextWriterAttribute.Type, "Text");
writer.AddAttribute(HtmlTextWriterAttribute.Value, (String)ViewState["Text"]);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
}
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer); writer.Write((String)ViewState["Text"]);
}
public virtual string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
} set
{
ViewState["Text"] = value;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//this.Page.ClientScript.GetPostBackEventReference();
}
#region IPostBackDataHandler 成员 bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
//比较初始数据presentValue和回传数据postedValue
string postedValue = postCollection[postDataKey];
Text = postedValue;
string presentValue = Text;
if (presentValue == null || postedValue != presentValue)
{
Text = postedValue;
return true;
}
return false;
} void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
} private void OnTextChanged(EventArgs eventArgs)
{
EventHandler textChangedHandler = (EventHandler)Events[EventTextChanged];
if (textChangedHandler != null)
{
textChangedHandler(this, eventArgs);
}
}
// 为TextChanged实现事件属性结构
public event EventHandler TextChanged
{
add
{
Events.AddHandler(EventTextChanged, value);
}
remove
{
Events.RemoveHandler(EventTextChanged, value);
}
}
#endregion #region IPostBackContainer 成员 PostBackOptions IPostBackContainer.GetPostBackOptions(IButtonControl buttonControl)
{
throw new Exception("The method or operation is not implemented.");
} #endregion
}
}完整代码.js修改值的方式是document.getElementById(用户控件的名称).innerHTML=值;
是innerHTML属性,不是value- -# 获取不到.