简体   繁体   English

使用带有一些表单变量的可编辑模板

[英]use an editable template with some form variables

I am new to C# and would like advise on how to create a text file from a template that has three variables that are assigned by a form submission. 我是C#的新手,我想为您提供有关如何从模板创建文本文件的建议,该模板具有由表单提交分配的三个变量。 Here is how it would look: template.txt (var1 = name, var2 = location, var3 = year are place holder in the file) Example: My name is var1 and I am from var2 where I lived for var3 years. 外观如下:template.txt(var1 =名称,var2 =位置,var3 =年是文件中的占位符)示例:我的名字是var1,我来自我住了var3年的var2。

Form: Name: Location: Year: 形式:姓名:地点:年:

Then that output text file would be the sum of the template and the form variables. 然后,该输出文本文件将是模板和表单变量的总和。

Now I can edit the template to read anything and move the variables where ever I want to create any desired output text base on the template and form input. 现在,我可以编辑模板以读取任何内容,并将变量移动到要在模板和表单输入基础上创建任何所需输出文本的位置。

I hope I have explained that well enough. 我希望我已经解释得足够好了。

Take a look at the String.Format method. 看一下String.Format方法。 You could dynamically generate the text and move around the placeholders to represent the variables. 您可以动态生成文本并在占位符之间移动以表示变量。 I'd say make the textboxes an array so you can loop through them when you generate the text file. 我想说说让文本框成为一个数组,以便在生成文本文件时可以遍历它们。

string name = "Luke";
string location = "ATL";
str = String.Format("Hello {0} from {1}.", name, location);

OUTPUT:
"Hello Luke from ATL."

MSDN Docs here: http://msdn.microsoft.com/en-us/library/fht0f5be.aspx 此处的MSDN文档: http : //msdn.microsoft.com/zh-cn/library/fht0f5be.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM