0

我创建了一个自定义BoundField,用于DetailsView编辑插入模式下使用 Twitter Bootstrap。

该控件工作正常,除非我在InitializeDataCell(...)LiteralControl期间添加了额外的 s 以围绕TextBox一些 HTML 。与此相关的代码接近尾声。

下面生成的 HTML 似乎与使用TemplateField. 但是,当我触发Update时,只要它添加此处看到的额外div / span , PostBackTextBox中的值就是空白的。

<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>

下面是我在前端 ASP.NET 代码中所做的一些示例以及生成的 HTML。

不起作用 -通过更新命令 提交后,文本框中的自定义字段值未设置,仅当我添加输入附加附加部分时。

详细信息视图中的字段

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

生成的 HTML

<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>

工作正常 - 正常 TemplateField 工作正常

详细信息视图中的字段

<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 - 与上面相同

<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>

工作正常 -当我不添加TextPrepend字段时,通过更新命令 正确提交了文本框中使用自定义字段值生成的 HTML

详细信息视图中的字段

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

生成的 HTML 没有额外的 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部分

我相信这是由于InitializeDataCell(...)实现的原因。

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);
        }
    }
}

整个代码,以防它对任何尝试使用 Twitter Bootstrap 的人有用,或者如果我的代码在其他地方出错。

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
}
4

1 回答 1

0

我设法弄清楚为什么会这样。由于该Control字段中的集合已更改,因此您还必须覆盖 _ExtractValuesFromCell(...) to get theTextBox` 值。

我使用了ASP.NET Boundfield 的一个稍微改变的实现来支持 Dropdownlist 缺少一个最终功能,并且没有针对我的用例进行优化。但是,它现在工作正常。

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);
            }
        }
    }
}
于 2013-09-10T01:17:13.307 回答