逮到mono asp.net的bug——多行文本框呈现内容前面有空格

作者:V君 发布于:2015-3-12 23:34 Thursday 分类:折腾手记

这个问题只有用它的人才会遇到, 也不多扯了, 直接贴代码!

[ValidationProperty("Text")]
[ControlValueProperty("Text")]
[DefaultProperty("Text")]
public class TextArea: WebControl, IPostBackDataHandler, IEditableTextControl
{
    protected override HtmlTextWriterTag TagKey
    {
        get { return HtmlTextWriterTag.Textarea; }
    }

    public bool CausesValidation
    {
        get
        {
            if (ViewState["CausesValidation"] == null)
                return false;
            return (bool)ViewState["CausesValidation"];
        }
        set
        {
            ViewState["CausesValidation"] = value;
        }
    }

    public string ValidationGroup
    {
        get
        {
            return (string)ViewState["ValidationGroup"] ?? "";
        }
        set
        {
            ViewState["ValidationGroup"] = value;
        }
    }

    public string Text
    {
        get
        {
            return (string)ViewState["Text"] ?? "";
        }
        set
        {
            ViewState["Text"] = value;
        }
    }

    public bool ReadOnly
    {
        get
        {
            if (ViewState["Readonly"] == null)
                return false;

            return (bool)ViewState["Readonly"];
        }
        set
        {
            ViewState["Readonly"] = value;
        }
    }

    public int Rows
    {
        get
        {
            if (ViewState["Rows"] == null)
                return 0;
            return (int)ViewState["Rows"];
        }
        set
        {
            ViewState["Rows"] = value;
        }
    }

    public int Columns
    {
        get
        {
            if (ViewState["Columns"] == null)
                return 0;
            return (int)ViewState["Columns"];
        }
        set
        {
            ViewState["Columns"] = value;
        }
    }

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);

        if (Enabled && !IsEnabled)
            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
        if (ReadOnly)
            writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");

        if (Rows != 0)
            writer.AddAttribute(HtmlTextWriterAttribute.Rows, Rows.ToString());
        if (Columns != 0)
            writer.AddAttribute(HtmlTextWriterAttribute.Cols, Columns.ToString());

        base.AddAttributesToRender(writer);
    }

    public override void RenderControl(HtmlTextWriter writer)
    {
        RenderBeginTag(writer);
        writer.WriteEncodedText(Text);
        RenderEndTag(writer);
    }

    public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
    {
        var strPostBack = postCollection[postDataKey];
        if (ReadOnly || Text.Equals(strPostBack, StringComparison.Ordinal))
            return false;
        Text = strPostBack;
        return true;
    }

    public void RaisePostDataChangedEvent()
    {
        if (!Page.IsPostBackEventControlRegistered)
        {
            Page.AutoPostBackControl = this;
            if (CausesValidation)
                Page.Validate(ValidationGroup);
        }
        TextChanged(this, EventArgs.Empty);
    }

    public event EventHandler TextChanged = delegate { };
}

标签: 软件开发 C# bug ASP.NET WebForm mono

评论(0) 引用(0) 浏览(2008)

ASP.NET WebForm 纯代码呈现页面, 不带aspx或ashx

作者:V君 发布于:2015-3-2 22:54 Monday 分类:折腾手记

这些天继续折腾相对MVC老掉牙的WebForm,做个自己的数据管理前端.

泥萌就用连表单验证都要自己加脚本的MVC吧,还是觉得高度集成的WebForm好使.

目前几乎没用到ASPX文件了, 全靠代码创建表单控件树. 

谁说我大ASP.NET只是拖拖控件而已的! 

由于已经把基础页面类封装得近乎完整(当然还不完美).每添加一个数据库表管理页面只需要继承一下页面基类就能完整实现列表查询/输入表单以及回传事件处理.


那么问题来了: 能不能把ASPX扔掉? 因为现在它只充当个一般处理程序一样的存在,但是一般处理程序似乎不能处理控件树.


答案是可以!

阅读全文>>

标签: 软件开发 C# ASP.NET WebForm

评论(0) 引用(0) 浏览(2233)

Powered by emlog 去你妹的备案 sitemap