自定义的BoundField做回发之间添加了LiteralControl没有preserve数据 [英] Custom BoundField doesn't preserve data between PostBacks with added LiteralControl

查看:306
本文介绍了自定义的BoundField做回发之间添加了LiteralControl没有preserve数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的的BoundField 与使用 DetailsView控件中的修改的和插入的模式来使用Twitter的引导。

控制工作正常,但我在额外添加 LiteralControl s的过程中一些HTML包围文本框 InitializeDataCell(...)的。 code与此相关的走向结束。

生成的HTML低于似乎等同于与模板列生成的内容。但是,当我火了的更新的,任何时候它增加了额外的 DIV / 范围的在这里看到,在文本框中的值为空时的回传

 < D​​IV CLASS =输入 -  prePEND>
    <跨度类=附加> $< / SPAN>
    <输入名称=$ ctl00 $ ctl00 $日程地址搜索Maincontent $日程地址搜索Maincontent PI $ dvPackage $ tbPrice类型=文本值=0大小=5ID =MainContent_MainContent_pi_dvPackage_tbPrice称号=价格>
< / DIV>

下面是会产生什么我前面ASP.NET code范围内做什么HTML的一些例子。

不行的 - 自定义字段
文本框内的价值通过的更新的命令,只有当我补充提交后取消设置的输入追加的和的附加的一部分。

字段中的DetailsView

 <我:ValidationField数据字段=价格的HeaderText =价格文本prePEND =$/>

HTML生成

 < TD>
    < D​​IV CLASS =输入 - prePEND>
        <跨度类=附加> $< / SPAN>
        <输入名称=$ ctl00 $ ctl00 $日程地址搜索Maincontent $日程地址搜索Maincontent PI $ dvPackage $ tbPrice类型=文本值=0大小=5ID =MainContent_MainContent_pi_dvPackage_tbPrice称号=价格>
    < / DIV>
< / TD>

不工作 - 正常的TemplateField
正常工作

字段中的DetailsView

 < ASP:的TemplateField>
    <&EditItemTemplate的GT;
        < D​​IV CLASS =输入 - prePEND>
            <跨度类=附加> $< / SPAN>
            < ASP:文本框ID =tbCost=服务器文本='<%#绑定(费用)%GT;'>< / ASP:文本框>
        < / DIV>
    < / EditItemTemplate中>
< / ASP:的TemplateField>

HTML生成 - 与上面是什么

 < TD>
    < D​​IV CLASS =输入 - prePEND>
        <跨度类=附加> $< / SPAN>
        <输入名称=$ ctl00 $ ctl00 $日程地址搜索Maincontent $日程地址搜索Maincontent PI $ dvPackage $ tbPrice类型=文本值=0大小=5ID =MainContent_MainContent_pi_dvPackage_tbPrice称号=价格>
    < / DIV>
< / TD>

不工作 - HTML与自定义字段生成
文本框中的值是通过正确提交的更新的命令时,我不添加的正文prePEND 的领域。

字段中的DetailsView

 <我:ValidationField数据字段=价格的HeaderText =价格/>

HTML无需额外的跨度产生/ DIV

 < TD>
    <输入名称=$ ctl00 $ ctl00 $日程地址搜索Maincontent $日程地址搜索Maincontent PI $ dvPackage $ tbPrice类型=文本值=0大小=5ID =MainContent_MainContent_pi_dvPackage_tbPrice称号=价格>
< / TD>

InitializeDataCell 与此相关的code创建零件

我相信这是由于一些与的 InitializeDataCell(...)的实施。

 保护覆盖无效InitializeDataCell(DataControlFieldCell细胞,DataControlRowState的RowState)
{
    base.InitializeDataCell(细胞的RowState);    //查找文本框验证
    文本框的文本= FindTextBox(单元);    如果(文字!= NULL)
    {
        text.ID =TB+数据字段;
        text.MaxLength = MAXLENGTH;
        text.TextMode =的TextMode;
        text.Text =数据字段;        字符串cellCss =的String.Empty;
        布尔prePEND = string.IsNullOrEmpty(this.Text prePEND)!;
        布尔追加= string.IsNullOrEmpty(this.TextAppend)!;
        布尔插件= prePEND ||附加;        如果(prePEND == true)而
            cellCss = this.ConcatenateCss(cellCss,输入 - prePEND);        如果(追加==真)
            cellCss = this.ConcatenateCss(cellCss,投入追加);        如果(插件==真)
        {
            INT textIndex = cell.Controls.IndexOf(文本);            文字集装箱=新的文字();
            container.Text =< D​​IV CLASS = \\+ cellCss +\\>中;
            cell.Controls.AddAt(textIndex,集装箱);
        }        如果(prePEND == true)而
        {
            INT textIndex = cell.Controls.IndexOf(文本);            字面单位=新的文字();
            units.Text =<跨度类= \\附加\\>中这+ prePEND()+< / SPAN>中;
            cell.Controls.AddAt(textIndex,单位);
        }        如果(追加==真)
        {
            字面单位=新的文字();
            units.Text =<跨度类= \\附加\\>中+ this.Append()+&下; /跨度>中;
            cell.Controls.Add(单位);
        }        如果(插件==真)
        {
            文字集装箱=新的文字();
            container.Text =< / DIV>中;
            cell.Controls.Add(容器);
        }
    }
}

整个code 的情况下,这是任何人试图使用Twitter的引导非常有用,或者如果我的code是错误的其他地方。

 公共类ValidationField:绑定列
{
    #区域属性
    公共虚拟字符串EditTextCssClass
    {
        得到
        {
            回报(字符串)(的ViewState [EditTextCssClass] ??的S​​tring.Empty);
        }        组
        {
            的ViewState [EditTextCssClass] =值;
            OnFieldChanged();
        }
    }    公共虚拟ValidatorDisplay ErrorDisplay
    {
        得到
        {
            回报(ValidatorDisplay)(的ViewState [ErrorDisplay] ?? ValidatorDisplay.Dynamic);
        }        组
        {
            的ViewState [ErrorDisplay] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串的ErrorMessage
    {
        得到
        {
            回报(字符串)(的ViewState [的ErrorMessage] ??无效的输入值);
        }        组
        {
            的ViewState [的ErrorMessage] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串帮助文件
    {
        得到
        {
            回报(字符串)(的ViewState [帮助文件] ??的S​​tring.Empty);
        }        组
        {
            的ViewState [帮助文件] =值;
            OnFieldChanged();
        }
    }    公共虚拟TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode HelpDisplay
    {
        得到
        {
            回报(TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode)(的ViewState [HelpDisplay] ?? TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode.Block);
        }        组
        {
            的ViewState [HelpDisplay] =值;
            OnFieldChanged();
        }
    }    公共虚拟INT最大长度
    {
        得到
        {
            回报(INT)(的ViewState [最大长度] ?? 0);
        }        组
        {
            的ViewState [最大长度] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串的占位符
    {
        得到
        {
            回报(字符串)(的ViewState [占位符] ??的S​​tring.Empty);
        }        组
        {
            的ViewState [占位符] =值;
            OnFieldChanged();
        }
    }    公共虚拟BOOL必填
    {
        得到
        {
            回报(布尔)(的ViewState [必需] ??假);
        }        组
        {
            的ViewState [必需] =值;
            OnFieldChanged();
        }
    }    公共虚拟TextBoxMode的TextMode
    {
        得到
        {
            回报(TextBoxMode)(的ViewState [的TextMode] ?? TextBoxMode.SingleLine);
        }        组
        {
            的ViewState [的TextMode] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串ValidationEx pression
    {
        得到
        {
            回报(字符串)(的ViewState [ValidationEx pression] ??的S​​tring.Empty);
        }        组
        {
            的ViewState [ValidationEx pression] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串的ValidationGroup
    {
        得到
        {
            回报(字符串)(的ViewState [的ValidationGroup] ??的S​​tring.Empty);
        }        组
        {
            的ViewState [的ValidationGroup] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串TextAppend
    {
        得到
        {
            对象值=的ViewState [TextAppend];
            如果(值!= NULL)
                返回value.ToString();            返回的String.Empty;
        }        组
        {
            的ViewState [TextAppend] =值;
            OnFieldChanged();
        }
    }    公共虚拟字符串文本prePEND
    {
        得到
        {
            对象值=的ViewState [文本prePEND];
            如果(值!= NULL)
                返回value.ToString();            返回的String.Empty;
        }        组
        {
            的ViewState [文本prePEND] =值;
            OnFieldChanged();
        }
    }    #endregion    公共ValidationField()
    {    }    保护覆盖的DataControlField CreateField()
    {
        返回新ValidationField();
    }    保护覆盖无效InitializeDataCell(DataControlFieldCell细胞,DataControlRowState的RowState)
    {
        base.InitializeDataCell(细胞的RowState);        //查找文本框验证
        文本框的文本= FindTextBox(单元);        如果(文字!= NULL)
        {
            text.ID =TB+数据字段;
            text.MaxLength = MAXLENGTH;
            text.TextMode =的TextMode;
            text.CssClass = EditTextCssClass;
            text.Text =数据字段;            如果(占位符!=的String.Empty)
                text.Attributes.Add(占位符占位符);            字符串cellCss =的String.Empty;
            布尔prePEND = string.IsNullOrEmpty(this.Text prePEND)!;
            布尔追加= string.IsNullOrEmpty(this.TextAppend)!;
            布尔插件= prePEND ||附加;            如果(prePEND == true)而
                cellCss = this.ConcatenateCss(cellCss,输入 - prePEND);            如果(追加==真)
                cellCss = this.ConcatenateCss(cellCss,投入追加);            如果(插件==真)
            {
                INT textIndex = cell.Controls.IndexOf(文本);                文字集装箱=新的文字();
                container.Text =< D​​IV CLASS = \\+ cellCss +\\>中;
                cell.Controls.AddAt(textIndex,集装箱);
            }            如果(prePEND == true)而
            {
                INT textIndex = cell.Controls.IndexOf(文本);                字面单位=新的文字();
                units.Text =<跨度类= \\附加\\>中这+ prePEND()+< / SPAN>中;
                cell.Controls.AddAt(textIndex,单位);
            }            如果(追加==真)
            {
                字面单位=新的文字();
                units.Text =<跨度类= \\附加\\>中+ this.Append()+&下; /跨度>中;
                cell.Controls.Add(单位);
            }            如果(插件==真)
            {
                文字集装箱=新的文字();
                container.Text =< / DIV>中;
                cell.Controls.Add(容器);
            }            如果(必填==真)
            {
                文字要求=新文字();
                required.Text =<跨度类= \\要求\\> * LT; / SPAN>中;
                cell.Controls.Add(需要);
            }            如果(帮助文件!=的String.Empty)
            {
                标签lblHelpText =新的Label();
                如果(HelpDisplay == TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode.Block)
                    lblHelpText.CssClass =求助块;
                其他
                    lblHelpText.CssClass =求助内联;
                lblHelpText.Text =帮助文件;
                cell.Controls.Add(lblHelpText);
            }            如果(必填==真)
            {
                //添加一个RequiredFieldValidator
                需要的RequiredFieldValidator =新的RequiredFieldValidator();
                required.ErrorMessage =的ErrorMessage;
                required.Display = ErrorDisplay;
                required.ControlToValidate = text.ID;
                required.Text =;                如果(的ValidationGroup!=的String.Empty)
                    required.ValidationGroup =的ValidationGroup;                cell.Controls.Add(需要);
            }            如果(ValidationEx pression!=的String.Empty)
            {
                //添加一个RequiredFieldValidator
                RegularEx pressionValidator的regex ​​=新RegularEx pressionValidator();
                regex.ErrorMessage =的ErrorMessage;
                regex.Display = ErrorDisplay;
                regex.ControlToValidate = text.ID;
                regex.ValidationEx pression = ValidationEx pression;                如果(的ValidationGroup!=的String.Empty)
                    regex.ValidationGroup =的ValidationGroup;                cell.Controls.Add(正则表达式);
            }
        }
    }    #区域方法
    私人字符串ConcatenateCss(PARAMS字符串[]班)
    {
        字符串结果=的String.Empty;
        的foreach(在班字符串s)
            结果+ = S +;        返回result.TrimEnd()TrimStart()。
    }    私有静态文本框FindTextBox(控制父)
    {
        文本框的结果= NULL;
        的foreach(在parent.Controls控制控制)
        {
            如果(控制文本框)
            {
                结果=控制,文本框;
                打破;
            }
        }
        返回结果;
    }    受保护的虚拟字符串prePEND()
    {
        返回this.Text prePEND;
    }    受保护的虚拟字符串追加()
    {
        返回this.TextAppend;
    }    #endregion
}


解决方案

我设法弄清楚为什么这发生。因为控制收集是在外地改变,你还必须重写_ExtractValues​​FromCell(...)来获得文本框`值。

我用从<一个略微改动的实施href=\"http://stackoverflow.com/questions/1979381/asp-net-boundfield-overridden-to-support-dropdownlist-is-missing-one-final-featu\">ASP.NET绑定列重写以支持DROPDOWNLIST缺少一个最后的功能和我的用例都没有优化。然而,现在工作得很好。

 公共覆盖无效ExtractValues​​FromCell(System.Collections.Specialized.IOrderedDictionary字典,DataControlFieldCell细胞,DataControlRowState的RowState,布尔includeReadOnly)
{
    控制控制= NULL;
    字符串的dataField =数据字段;
    对象文本= NULL;
    字符串nullDisplayText = NullDisplayText;
    如果(((RowState的&安培; DataControlRowState.Insert)== DataControlRowState.Normal)|| InsertVisible)
    {
        如果(cell.Controls.Count大于0)
        {
            的foreach(在cell.Controls控制C)
            {
                控制= cell.Controls [0];                如果(c是文本框)
                {
                    文字=((文本框)C)。文本;
                }
            }
        }
        否则,如果(includeReadOnly)
        {
            字符串s = cell.Text;
            如果(S ==与&amp; NBSP;)
            {
                文=的String.Empty;
            }
            否则,如果(SupportsHtmlEn code和;&安培; HtmlEn code)
            {
                文= HttpUtility.HtmlDe code(S);
            }
            其他
            {
                文= S;
            }
        }
        如果(文字!= NULL)
        {
            如果(((文本字符串)及及(((字符串)文本)。长度== 0))及和放大器; ConvertEmptyStringToNull)
            {
                文= NULL;
            }
            如果(((文本字符串)及及(((字符串)文本)== nullDisplayText))及及(nullDisplayText.Length大于0))
            {
                文= NULL;
            }
            如果(dictionary.Contains(的dataField))
            {
                词典[的dataField] =文本;
            }
            其他
            {
                dictionary.Add(的dataField,文字);
            }
        }
    }
}

I've created a custom BoundField for use with a DetailsView in Edit and Insert modes to utilize Twitter Bootstrap.

The control works fine, except when I add in extra LiteralControls to surround the TextBox with some HTML during InitializeDataCell(...). Code related to this toward the end.

The HTML generated is below seems to be identical to what is generated with a TemplateField. But when I fire an Update, anytime it adds the extra div / span seen here, the value in the TextBox is blank upon PostBack.

<div class="input-prepend">
    <span class="add-on">$</span>
    <input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
</div>

Here is some examples of what I'm doing within the front ASP.NET code and what HTML is generated.

Doesn't work - custom field Value inside text box is unset after submitting via Update command, ONLY when I add the input-append and add-on part.

Field inside DetailsView

<my:ValidationField DataField="Price" HeaderText="Price" TextPrepend="$" />

HTML generated

<td>
    <div class="input-prepend">
        <span class="add-on">$</span>
        <input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
    </div>
</td>

Does work - normal TemplateField Works fine

Field inside DetailsView

<asp:TemplateField>
    <EditItemTemplate>
        <div class="input-prepend">
            <span class="add-on">$</span>
            <asp:TextBox ID="tbCost" runat="server" Text='<%# Bind("Cost") %>'></asp:TextBox>
        </div>
    </EditItemTemplate>
</asp:TemplateField>

HTML generated - identical to what is above

<td>
    <div class="input-prepend">
        <span class="add-on">$</span>
        <input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
    </div>
</td>

Does work - HTML generated with custom field Value inside text box is correctly submitted via Update command when I don't add the TextPrepend field.

Field inside DetailsView

<my:ValidationField DataField="Price" HeaderText="Price" />

HTML generated without extra span / div

<td>
    <input name="ctl00$ctl00$MainContent$MainContent$pi$dvPackage$tbPrice" type="text" value="0" size="5" id="MainContent_MainContent_pi_dvPackage_tbPrice" title="Price">
</td>

InitializeDataCell parts related to this code creation

I believe this is due to something with the InitializeDataCell(...) implementation.

protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
{
    base.InitializeDataCell(cell, rowState);

    // Find the text box to validate
    TextBox text = FindTextBox(cell);

    if (text != null)
    {
        text.ID = "tb" + DataField;
        text.MaxLength = MaxLength;
        text.TextMode = TextMode;
        text.Text = DataField;

        string cellCss = string.Empty;
        bool prepend = !string.IsNullOrEmpty(this.TextPrepend);
        bool append = !string.IsNullOrEmpty(this.TextAppend);
        bool addon = prepend || append;

        if (prepend == true)
            cellCss = this.ConcatenateCss(cellCss, "input-prepend");

        if (append == true)
            cellCss = this.ConcatenateCss(cellCss, "input-append");

        if (addon == true)
        {
            int textIndex = cell.Controls.IndexOf(text);

            Literal container = new Literal();
            container.Text = "<div class=\"" + cellCss + "\">";
            cell.Controls.AddAt(textIndex, container);
        }

        if (prepend == true)
        {
            int textIndex = cell.Controls.IndexOf(text);

            Literal units = new Literal();
            units.Text = "<span class=\"add-on\">" + this.Prepend() + "</span>";
            cell.Controls.AddAt(textIndex, units);
        }

        if (append == true)
        {
            Literal units = new Literal();
            units.Text = "<span class=\"add-on\">" + this.Append() + "</span>";
            cell.Controls.Add(units);
        }

        if (addon == true)
        {
            Literal container = new Literal();
            container.Text = "</div>";
            cell.Controls.Add(container);
        }
    }
}

Entire code in case it's useful to anyone trying to use Twitter Bootstrap, or if my code is wrong elsewhere.

public class ValidationField : BoundField
{
    #region Properties
    public virtual string EditTextCssClass
    {
        get
        {
            return (string)(ViewState["EditTextCssClass"] ?? string.Empty);
        }

        set
        {
            ViewState["EditTextCssClass"] = value;
            OnFieldChanged();
        }
    }

    public virtual ValidatorDisplay ErrorDisplay
    {
        get
        {
            return (ValidatorDisplay)(ViewState["ErrorDisplay"] ?? ValidatorDisplay.Dynamic);
        }

        set
        {
            ViewState["ErrorDisplay"] = value;
            OnFieldChanged();
        }
    }

    public virtual string ErrorMessage
    {
        get
        {
            return (string)(ViewState["ErrorMessage"] ?? "Invalid value entered");
        }

        set
        {
            ViewState["ErrorMessage"] = value;
            OnFieldChanged();
        }
    }

    public virtual string HelpText
    {
        get
        {
            return (string)(ViewState["HelpText"] ?? string.Empty);
        }

        set
        {
            ViewState["HelpText"] = value;
            OnFieldChanged();
        }
    }

    public virtual TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode HelpDisplay
    {
        get
        {
            return (TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode)(ViewState["HelpDisplay"] ?? TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode.Block);
        }

        set
        {
            ViewState["HelpDisplay"] = value;
            OnFieldChanged();
        }
    }

    public virtual int MaxLength
    {
        get
        {
            return (int)(ViewState["MaxLength"] ?? 0);
        }

        set
        {
            ViewState["MaxLength"] = value;
            OnFieldChanged();
        }
    }

    public virtual string PlaceHolder
    {
        get
        {
            return (string)(ViewState["PlaceHolder"] ?? string.Empty);
        }

        set
        {
            ViewState["PlaceHolder"] = value;
            OnFieldChanged();
        }
    }

    public virtual bool Required
    {
        get
        {
            return (bool)(ViewState["Required"] ?? false);
        }

        set
        {
            ViewState["Required"] = value;
            OnFieldChanged();
        }
    }

    public virtual TextBoxMode TextMode
    {
        get
        {
            return (TextBoxMode)(ViewState["TextMode"] ?? TextBoxMode.SingleLine);
        }

        set
        {
            ViewState["TextMode"] = value;
            OnFieldChanged();
        }
    }

    public virtual string ValidationExpression
    {
        get
        {
            return (string)(ViewState["ValidationExpression"] ?? string.Empty);
        }

        set
        {
            ViewState["ValidationExpression"] = value;
            OnFieldChanged();
        }
    }

    public virtual string ValidationGroup
    {
        get
        {
            return (string)(ViewState["ValidationGroup"] ?? string.Empty);
        }

        set
        {
            ViewState["ValidationGroup"] = value;
            OnFieldChanged();
        }
    }

    public virtual string TextAppend
    {
        get
        {
            object value = ViewState["TextAppend"];
            if (value != null)
                return value.ToString();

            return string.Empty;
        }

        set
        {
            ViewState["TextAppend"] = value;
            OnFieldChanged();
        }
    }

    public virtual string TextPrepend
    {
        get
        {
            object value = ViewState["TextPrepend"];
            if (value != null)
                return value.ToString();

            return string.Empty;
        }

        set
        {
            ViewState["TextPrepend"] = value;
            OnFieldChanged();
        }
    }

    #endregion

    public ValidationField()
    {

    }

    protected override DataControlField CreateField()
    {
        return new ValidationField();
    }

    protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
    {
        base.InitializeDataCell(cell, rowState);

        // Find the text box to validate
        TextBox text = FindTextBox(cell);

        if (text != null)
        {
            text.ID = "tb" + DataField;
            text.MaxLength = MaxLength;
            text.TextMode = TextMode;
            text.CssClass = EditTextCssClass;
            text.Text = DataField;

            if (PlaceHolder != string.Empty)
                text.Attributes.Add("placeholder", PlaceHolder);

            string cellCss = string.Empty;
            bool prepend = !string.IsNullOrEmpty(this.TextPrepend);
            bool append = !string.IsNullOrEmpty(this.TextAppend);
            bool addon = prepend || append;

            if (prepend == true)
                cellCss = this.ConcatenateCss(cellCss, "input-prepend");

            if (append == true)
                cellCss = this.ConcatenateCss(cellCss, "input-append");

            if (addon == true)
            {
                int textIndex = cell.Controls.IndexOf(text);

                Literal container = new Literal();
                container.Text = "<div class=\"" + cellCss + "\">";
                cell.Controls.AddAt(textIndex, container);
            }

            if (prepend == true)
            {
                int textIndex = cell.Controls.IndexOf(text);

                Literal units = new Literal();
                units.Text = "<span class=\"add-on\">" + this.Prepend() + "</span>";
                cell.Controls.AddAt(textIndex, units);
            }

            if (append == true)
            {
                Literal units = new Literal();
                units.Text = "<span class=\"add-on\">" + this.Append() + "</span>";
                cell.Controls.Add(units);
            }

            if (addon == true)
            {
                Literal container = new Literal();
                container.Text = "</div>";
                cell.Controls.Add(container);
            }

            if (Required == true)
            {
                Literal required = new Literal();
                required.Text = "<span class=\"required\">*</span>";
                cell.Controls.Add(required);
            }

            if (HelpText != string.Empty)
            {
                Label lblHelpText = new Label();
                if (HelpDisplay == TwitterBootstrap.WebControls.TextBox.HelpTextDisplayMode.Block)
                    lblHelpText.CssClass = "help-block";
                else
                    lblHelpText.CssClass = "help-inline";
                lblHelpText.Text = HelpText;
                cell.Controls.Add(lblHelpText);
            }

            if (Required == true)
            {
                // Add a RequiredFieldValidator
                RequiredFieldValidator required = new RequiredFieldValidator();
                required.ErrorMessage = ErrorMessage;
                required.Display = ErrorDisplay;
                required.ControlToValidate = text.ID;
                required.Text = "";

                if (ValidationGroup != string.Empty)
                    required.ValidationGroup = ValidationGroup;

                cell.Controls.Add(required);
            }

            if (ValidationExpression != string.Empty)
            {
                // Add a RequiredFieldValidator
                RegularExpressionValidator regex = new RegularExpressionValidator();
                regex.ErrorMessage = ErrorMessage;
                regex.Display = ErrorDisplay;
                regex.ControlToValidate = text.ID;
                regex.ValidationExpression = ValidationExpression;

                if (ValidationGroup != string.Empty)
                    regex.ValidationGroup = ValidationGroup;

                cell.Controls.Add(regex);
            }
        }
    }

    #region Methods
    private string ConcatenateCss(params string[] classes)
    {
        string result = string.Empty;
        foreach (string s in classes)
            result += s + " ";

        return result.TrimEnd().TrimStart();
    }

    private static TextBox FindTextBox(Control parent)
    {
        TextBox result = null;
        foreach (Control control in parent.Controls)
        {
            if (control is TextBox)
            {
                result = control as TextBox;
                break;
            }
        }
        return result;
    }

    protected virtual string Prepend()
    {
        return this.TextPrepend;
    }

    protected virtual string Append()
    {
        return this.TextAppend;
    }

    #endregion
}

解决方案

I managed to figure out why this was happening. Since the Control collection was altered in the field, you must also override _ExtractValuesFromCell(...)to get theTextBox` value.

I have used a slightly altered implementation from ASP.NET Boundfield overridden to support Dropdownlist is missing one final feature and haven't optimized for my use case. However, it works fine now.

public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
{
    Control control = null;
    string dataField = DataField;
    object text = null;
    string nullDisplayText = NullDisplayText;
    if (((rowState & DataControlRowState.Insert) == DataControlRowState.Normal) || InsertVisible)
    {
        if (cell.Controls.Count > 0)
        {
            foreach (Control c in cell.Controls)
            {
                control = cell.Controls[0];

                if (c is TextBox)
                {
                    text = ((TextBox)c).Text;
                } 
            }
        }
        else if (includeReadOnly)
        {
            string s = cell.Text;
            if (s == "&nbsp;")
            {
                text = string.Empty;
            }
            else if (SupportsHtmlEncode && HtmlEncode)
            {
                text = HttpUtility.HtmlDecode(s);
            }
            else
            {
                text = s;
            }
        }
        if (text != null)
        {
            if (((text is string) && (((string)text).Length == 0)) && ConvertEmptyStringToNull)
            {
                text = null;
            }
            if (((text is string) && (((string)text) == nullDisplayText)) && (nullDisplayText.Length > 0))
            {
                text = null;
            }
            if (dictionary.Contains(dataField))
            {
                dictionary[dataField] = text;
            }
            else
            {
                dictionary.Add(dataField, text);
            }
        }
    }
}

这篇关于自定义的BoundField做回发之间添加了LiteralControl没有preserve数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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