我有一个组合控件,里面有一个textbox,一个image,有的时候,我需要将这个组合控件的enable属性设为false,可当我设为false后,textbox里面的值在提交到服务器后(textbox.text)就成空的了,我该怎么解决这个问题?
高手帮忙,非常感谢!!!

解决方案 »

  1.   

    false后本来就要掉换个思路看看
      

  2.   

    比如将该值存到另外textbox上然后又将她赋回来拉
      

  3.   

    可服务器控件中的文本框false之后就能保存值,我想加个viewstate,可不知道都该加到哪,高手帮忙,郁闷中......
      

  4.   

    实现 IPostBackDataHandler 接口了吗?
      

  5.   

    我的AspNetPager分页控件中用ViewState保存属性值的代码:public int PageSize
    {
    get
    {
    object obj=ViewState["PageSize"];
    return (obj==null)?10:(int)obj;
    }
    set
    {
    ViewState["PageSize"]=value;
    }
    }
      

  6.   

    我的代码:
    ————————————————————————————————————————
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Drawing;namespace vlj.zlk.webcontrol
    {
    /// <summary>
    /// LBInt 的摘要说明。
    /// </summary>
    [DefaultProperty("labelString"), 
    ToolboxData("<{0}:DateTimePiter runat=server></{0}:DateTimePiter>")]
    public class DateTimePiter : System.Web.UI.WebControls.WebControl,INamingContainer 
    {
    public DateTimePiter()
    {
    //this.label.Width=120;
    this.textBox.Width=150;
    image.ImageUrl="/cdswxxzlk/images/calendar_001.gif";
    image.ImageAlign=System.Web.UI.WebControls.ImageAlign.AbsMiddle;
    image.Height=20;
    image.Width=20;
    //this.textBox.MaxLength=20;
    //this.table.ID=this.table.UniqueID;
    } public TextBox textBox = new TextBox();
    private System.Web.UI.WebControls.Image image=new System.Web.UI.WebControls.Image(); //private Color _backgroundColor;//鼠标移入的背景颜色 //private const string setMOUSE_SCRIPT = "<script language=javascript>var OldColor;function ontextfocus(ctrl,color){OldColor = ctrl.style.backgroundColor;ctrl.style.backgroundColor = color;}function ontextblur(ctrl){ctrl.style.backgroundColor = OldColor;}</script>";//用来触发鼠标事件的脚本
    [DefaultValue("120px")] public int TBMaxLength
    {
    get
    {
    return textBox.MaxLength;
    }
    set
    {
    textBox.MaxLength=value;
    }
    }
    [DefaultValue("150px")]
    public int TextBoxWidth
    {
    get
    {
    return (int)textBox.Width.Value;
    }
    set
    {
    textBox.Width=value;
    }
    }
    public string textString
    {
    get
    {
    return textBox.Text;
    }
    set
    {
    textBox.Text = value;
    }
    }
    private string changecolor()
    {
    return "#9FBFE6";
    //以下将颜色值转化成十六进制表示
    // string R,G,B;
    // R = (Convert.ToInt32(this.backgroundColor.R)).ToString("X");
    // G = (Convert.ToInt32(this.backgroundColor.G)).ToString("X");
    // B = (Convert.ToInt32(this.backgroundColor.B)).ToString("X");
    // return "#" + R + G + B;
    }
    private void CreateControls()//创建控件以及设置控件的相关属性 {
    //添加一行
    Table tb=new Table();
    tb.CellPadding=0;
    tb.CellSpacing=0;
    tb.BorderStyle=System.Web.UI.WebControls.BorderStyle.None;
    TableRow tr = new TableRow();
    //tr.BorderStyle=System.Web.UI.WebControls.BorderStyle.None;
    //tr.Style.Add("cellPadding","0");
    //tr.Style.Add("cellSpacing","0");
    //tr.Width=0;

    //添加两个单元格
    TableCell tc1 = new TableCell();
    TableCell tc2 = new TableCell(); //tc1.Width=0;
    //tc2.Width=0;
    //将控件添加到Controls集中. tc1.Controls.Add(textBox);
    tc2.Controls.Add(image);
    tr.Controls.Add(tc1);
    tr.Controls.Add(tc2); tb.Rows.Add(tr); this.Controls.Add(tb); //table.Controls.Add(tr);
    //this.Controls.Add(table);   
    //this.Controls.Add(this.textBox);
    //this.Controls.Add(this.image);
    } protected override void Render(HtmlTextWriter writer)
    {
    //textBox.Attributes.Add("onpropertychange","if(this.value==this.value2){return;}if(this.value.search(/['\\\\\"]/)!=-1){this.value=(this.value2)?this.value2:'';}else{this.value2=this.value;}");
    textBox.Attributes.Add("onkeyup","changeDate(this)");
                textBox.Attributes.Add("onblur","dateJuge(this)");
    image.Attributes.Add("onclick","calendar(document.all('" + textBox.UniqueID + "'))"); textBox.Attributes.Add("onkeydown","if(event.keyCode==13) event.keyCode=9");
    //textBox.Attributes.Add("onfocus","ontextfocus(" + this.table.ClientID + ",'" + changecolor() + "')");
    //textBox.Attributes.Add("onblur", "ontextblur(" + this.table.ClientID  + ")");
    base.Render (writer);
    }
    protected override void CreateChildControls()
    {
    this.EnsureChildControls();//如果子控件没有创建,则创建
    base.CreateChildControls ();//调用方法
    CreateControls();
    }
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
    this.EnsureChildControls();
    base.AddAttributesToRender(writer);
    //writer.AddStyleAttribute("font-size","9pt");
    }
    protected override void OnPreRender(EventArgs e)
    {
    //将脚本输出到页面中.
    string str=@"<SCRIPT language='JavaScript' src='/cdswxxzlk/script/Calendar30.js'></SCRIPT>";
    if(!Page.IsClientScriptBlockRegistered("datetimescript")) //防止重复输出.
    {
    Page.RegisterClientScriptBlock("datetimescript",str);
    }
    base.OnPreRender (e);
    }
    }
    }____________________________________________________________________________高手帮忙啊,多谢!!!
      

  7.   

    我的代码:
    ————————————————————————————————————————
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Drawing;namespace vlj.zlk.webcontrol
    {
    /// <summary>
    /// LBInt 的摘要说明。
    /// </summary>
    [DefaultProperty("labelString"), 
    ToolboxData("<{0}:DateTimePiter runat=server></{0}:DateTimePiter>")]
    public class DateTimePiter : System.Web.UI.WebControls.WebControl,INamingContainer 
    {
    public DateTimePiter()
    {
    //this.label.Width=120;
    this.textBox.Width=150;
    image.ImageUrl="/cdswxxzlk/images/calendar_001.gif";
    image.ImageAlign=System.Web.UI.WebControls.ImageAlign.AbsMiddle;
    image.Height=20;
    image.Width=20;
    //this.textBox.MaxLength=20;
    //this.table.ID=this.table.UniqueID;
    } public TextBox textBox = new TextBox();
    private System.Web.UI.WebControls.Image image=new System.Web.UI.WebControls.Image(); //private Color _backgroundColor;//鼠标移入的背景颜色 //private const string setMOUSE_SCRIPT = "<script language=javascript>var OldColor;function ontextfocus(ctrl,color){OldColor = ctrl.style.backgroundColor;ctrl.style.backgroundColor = color;}function ontextblur(ctrl){ctrl.style.backgroundColor = OldColor;}</script>";//用来触发鼠标事件的脚本
    [DefaultValue("120px")] public int TBMaxLength
    {
    get
    {
    return textBox.MaxLength;
    }
    set
    {
    textBox.MaxLength=value;
    }
    }
    [DefaultValue("150px")]
    public int TextBoxWidth
    {
    get
    {
    return (int)textBox.Width.Value;
    }
    set
    {
    textBox.Width=value;
    }
    }
    public string textString
    {
    get
    {
    return textBox.Text;
    }
    set
    {
    textBox.Text = value;
    }
    }
    private string changecolor()
    {
    return "#9FBFE6";
    //以下将颜色值转化成十六进制表示
    // string R,G,B;
    // R = (Convert.ToInt32(this.backgroundColor.R)).ToString("X");
    // G = (Convert.ToInt32(this.backgroundColor.G)).ToString("X");
    // B = (Convert.ToInt32(this.backgroundColor.B)).ToString("X");
    // return "#" + R + G + B;
    }
    private void CreateControls()//创建控件以及设置控件的相关属性 {
    //添加一行
    Table tb=new Table();
    tb.CellPadding=0;
    tb.CellSpacing=0;
    tb.BorderStyle=System.Web.UI.WebControls.BorderStyle.None;
    TableRow tr = new TableRow();
    //tr.BorderStyle=System.Web.UI.WebControls.BorderStyle.None;
    //tr.Style.Add("cellPadding","0");
    //tr.Style.Add("cellSpacing","0");
    //tr.Width=0;

    //添加两个单元格
    TableCell tc1 = new TableCell();
    TableCell tc2 = new TableCell(); //tc1.Width=0;
    //tc2.Width=0;
    //将控件添加到Controls集中. tc1.Controls.Add(textBox);
    tc2.Controls.Add(image);
    tr.Controls.Add(tc1);
    tr.Controls.Add(tc2); tb.Rows.Add(tr); this.Controls.Add(tb); //table.Controls.Add(tr);
    //this.Controls.Add(table);   
    //this.Controls.Add(this.textBox);
    //this.Controls.Add(this.image);
    } protected override void Render(HtmlTextWriter writer)
    {
    //textBox.Attributes.Add("onpropertychange","if(this.value==this.value2){return;}if(this.value.search(/['\\\\\"]/)!=-1){this.value=(this.value2)?this.value2:'';}else{this.value2=this.value;}");
    textBox.Attributes.Add("onkeyup","changeDate(this)");
                textBox.Attributes.Add("onblur","dateJuge(this)");
    image.Attributes.Add("onclick","calendar(document.all('" + textBox.UniqueID + "'))"); textBox.Attributes.Add("onkeydown","if(event.keyCode==13) event.keyCode=9");
    //textBox.Attributes.Add("onfocus","ontextfocus(" + this.table.ClientID + ",'" + changecolor() + "')");
    //textBox.Attributes.Add("onblur", "ontextblur(" + this.table.ClientID  + ")");
    base.Render (writer);
    }
    protected override void CreateChildControls()
    {
    this.EnsureChildControls();//如果子控件没有创建,则创建
    base.CreateChildControls ();//调用方法
    CreateControls();
    }
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
    this.EnsureChildControls();
    base.AddAttributesToRender(writer);
    //writer.AddStyleAttribute("font-size","9pt");
    }
    protected override void OnPreRender(EventArgs e)
    {
    //将脚本输出到页面中.
    string str=@"<SCRIPT language='JavaScript' src='/cdswxxzlk/script/Calendar30.js'></SCRIPT>";
    if(!Page.IsClientScriptBlockRegistered("datetimescript")) //防止重复输出.
    {
    Page.RegisterClientScriptBlock("datetimescript",str);
    }
    base.OnPreRender (e);
    }
    }
    }____________________________________________________________________________高手帮忙啊,多谢!!!
      

  8.   

    don't disable a textbox, if you do it, the textbox's value is not returned by the browser, try to make it ReadOnly insteadtry to override the Enabled propertypublic override bool Enabled
    {
          get
          {
                return image.Enabled;
          }
          set
          {
      image.Enabled = value;
      textBox.ReadOnly = value;
          }
    }
      

  9.   

    sorrytextBox.ReadOnly = !value;
      

  10.   

    老大来了,看来我有希望了    :)我试了一下,image没有enabled的属性其实我这个控件是一个日期选择,前面一个文本框,后面一个图片按钮,可有的时候,需要给文本框赋一个值,然后将其enable=false或readonly=true,可这样做了这个,客户端可以显示值,但提交到服务器后,就变成空的了textbox.readonly=true我也试了,可还是不行,老大再邦我想想还有没有其他办法,多谢
      

  11.   

    我把public override bool Enabled去掉了,直接设置了组合控件.textbox.readonly=true;这样基本可以满足需要,但服务器提交后,并不保存readonly=true的状态,如果我要继续让它readonly,还要再设置一次,郁闷中......
      

  12.   

    >>>并不保存readonly=true的状态are you sure?
    <form runat="server">
    <asp:TextBox id="txt" runat="server"/>
    <input type="button" value="set value" onclick="document.getElementById('<%=txt.ClientID%>').value='xyz' + new Date();">
    <asp:Button id="btn" runat="server" Text="Submit" OnClick="DoSubmit" />
    </form><script language="C#" runat="server">
    void Page_Load(Object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
    txt.Text = "123";
    txt.ReadOnly = true;
      }
    }void DoSubmit(Object sender, EventArgs e)
    {
      Response.Write("current value:" + txt.Text);
    }</script>
      

  13.   

    感谢老大回复       :)这样没问题,即使把文本框enable=false,服务器端也能获得状态,可在我的组合控件中,就不会保存readonly=true,提交后,服务器现在能得到组合控件中文本框的值,但再返回给客户端后,readonly就自动变为false了,不知道是什么原因,5555555555555555555没办法,最后加了个ViewState,用来保存readonly状态,呵呵希望路过的高手能再帮我看看,多谢!!!