扩展ASP.NET文本框和添加一个隐藏字段 [英] Extending ASP.NET Textbox and adding a hidden field

查看:88
本文介绍了扩展ASP.NET文本框和添加一个隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想延长ASP.NET的文本框控件,所以我可以把它变成使用TinyMCE的所见即所得编辑器。我也想一个隐藏字段添加到该TextBox控件和商店降价从客户端的浏览器这个隐藏值,当表单提交张贴回服务器。

问题是这样的隐藏字段是不是渲染它,即使我已经把它添加到的控件集合 - 只有文本渲染。如何获取文本框并隐藏字段来呈现?

以下code演示了我如何扩展TextBox控件。

 公共类TinyMCEEditor:System.Web.UI.WebControls.TextBox
    {
        私人只读HiddenField hf_MarkdownValue =新HiddenField();        公共字符串降价
        {
            {返回hf_MarkdownValue.Value; }
        }        保护覆盖无效的OnInit(EventArgs的发送)
        {
            //添加隐藏字段不会呈现
            this.Controls.Add(hf_MarkdownValue);
            base.OnInit(E);
        }    }


解决方案

隐藏字段没有得到在客户端渲染,因为你没有渲染它。下面这段code添加到您的类中,将很好地工作:

 保护覆盖无效渲染(HtmlTextWriter的W)
    {
        base.Render(重量);
        hf_MarkdownValue.RenderControl(重量);
    }

I would like to extend ASP.NET's Textbox control so I can make it into a WYSIWYG editor using TinyMCE. I would also like to add a hidden field to this Textbox control and store markdown in this hidden value from the client's browser and post it back to the server when the form submits.

The problem is this hidden field is not rendering it even though I have added it to collection of controls - only the textbox renders. How do I get the textbox AND hidden field to render?

The following code demonstrates how I'm extending the Textbox control.

 public class TinyMCEEditor : System.Web.UI.WebControls.TextBox
    {
        private readonly HiddenField hf_MarkdownValue = new HiddenField();

        public string MarkDown
        {
            get { return hf_MarkdownValue.Value; }
        }

        protected override void OnInit(EventArgs e)
        {
            //Adding the hidden field does not render
            this.Controls.Add(hf_MarkdownValue);  
            base.OnInit(e);                          
        }

    }

解决方案

The hidden field is not getting rendered at the client side since you are not rendering it. Add the following piece of code to your class an it would work fine:

    protected override void Render(HtmlTextWriter w)
    {
        base.Render(w);
        hf_MarkdownValue.RenderControl(w);
    }

这篇关于扩展ASP.NET文本框和添加一个隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆