如何在不实际保存的情况下以一种格式显示文本

时间:2017-03-08 16:58:06

标签: c# .net asp.net-mvc c#-4.0

我希望以一种格式向用户显示文字而不实际保存它。

(el-patch-defun keyboard-escape-quit ()
  "Exit the current \"mode\" (in a generalized sense of the word).
This command can exit an interactive command such as `query-replace',
can clear out a prefix argument or a region,
can get out of the minibuffer or other recursive edit,
cancel the use of the current buffer (for special-purpose buffers),
or go back to just one window (by deleting all but the selected window)."
  (interactive)
  (cond ((eq last-command 'mode-exited) nil)
    ((> (minibuffer-depth) 0)
     (abort-recursive-edit))
    (current-prefix-arg
     nil)
    ((and transient-mark-mode mark-active)
     (deactivate-mark))
    ((> (recursion-depth) 0)
     (exit-recursive-edit))
    (buffer-quit-function
     (funcall buffer-quit-function))
    (el-patch-remove
      ((not (one-window-p t))
       (delete-other-windows)))
    ((string-match "^ \\*" (buffer-name (current-buffer)))
     (bury-buffer))))

这可以正常显示,但现在string area = TextField.Text.Substring(0, 6); string major = TextField.Text.Substring(6, 4); string minor = TextField.Text.Substring(10, 4); CardNumberTextField.Text = string.Format("{0} {1} {2}", area, major, minor); 也以相同的格式存储值,是否有更好的方式,它只以一种方式显示文本,但以正常格式存储,没有空格。

2 个答案:

答案 0 :(得分:1)

我不确定这是否是您正在寻找的答案,但也许这会让您开始朝着正确的方向前进。您可以尝试以这种方式使用字符串格式。

CardNumberTextField.Text = string.Format("{0:###-###-####} {1} {2}", area, major, minor);

答案 1 :(得分:1)

最简单的答案是将所有内容保持原样,但在存储电话号码之前,请先删除任何非数字字符。这假设当你说单词store时,你的意思是坚持某个数据库/文件。

快速做到这一点的方法是:

private static string RemoveNonNumeric(string input)
{
    return new string(input.Where(x => char.IsDigit(x)).ToArray());
}

如果您只是希望TextField的Text属性正确,那么我将不得不提出几个问题:

  1. 您已经标记了MVC,但看起来您正在使用WebForms控件,是否有要求,或者您可以使用HTML / JS和Razor吗?

  2. 如果WebForms是硬盘要求,您是否尝试过使用WebForms HiddenField?您可以将TextField作为显示保存给用户,但使用JS将HiddenField同步到Textfield(删除所有非数字)。然后将您的HiddenField绑定到服务器上需要的任何内容。