将表单输入传递给文本文件输出 [英] Passing form inputs to text file output

查看:67
本文介绍了将表单输入传递给文本文件输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Folks,

我想将表单输入传递给文本文件输出,但我无法弄清楚在写入时如何引用表单字段中的信息到文本文件。

I want to pass form inputs to a text file output but I can't figure out how I reference the information put in the form fields when writing to a text file.

输出文本文件应该被命名为来自输入框的订单号

The output text file should be named the order number which comes from the input box

然后文本文件应包含来自表单输入的各个行的信息。

The text file should then contain the information from the form inputs on individual lines.

我还没有完成表单的编写,因为我正在努力使输出处理表单信息,但它正在编写测试文件愚蠢的测试文本。

I haven't finished writing the form as I was struggling to get the output working with the form information but it is writing the test file with the dumb test text in it.

下面是我正在使用的代码

below is the code I am using

如果有人可以提供帮助这将是惊人的。

If anyone can help that would be amazing.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, System.EventArgs e)
    {	
		
        string appPath = Request.PhysicalApplicationPath;
        string filePath = appPath + "Feedback_Sheets/test.txt";
        StreamWriter w;
        w = File.CreateText(filePath);
        w.WriteLine("This is a test line.");
        w.WriteLine("This is another line.");
        w.WriteLine();
        w.Flush();
        w.Close();
        Label1.Text = "Feedback sent successfully!<br />";
            }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to create text file and write text programmatically in asp.net</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        
        <br><br><br>
         <fieldset>
    <legend>Order Details:</legend>
   Order Number:<br>
  <input type="text" name="OrderNumber"> <br><br>
  
    Is the job a first time build?<br>
  
  <input type="radio" name="FTB" value="Yes"> Yes<br>
  <input type="radio" name="FTB" value="No"> No<br><br><br>
  
  </fieldset>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Create File and Write Text"
             ForeColor="SeaGreen"
             />   
    </div>
    </form>
</body>
</html>




推荐答案

由于您使用的是Asp.Net,请使用asp。网络控件而不是HTML控件。 然后通过引用控件的ID及其属性(文本框中文本的.Text,& radiobuttonlist中
选择值的.SelectedValue)来引用代码隐藏中这些控件的值。

Since you are using Asp.Net, use asp.net controls rather than HTML controls.  Then reference the values of those controls in code-behind by referencing the ID of the control, and its property (.Text for the text in a textbox, .SelectedValue for the chosen value in a radiobuttonlist).

你有:

<!DOCTYPE html>

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to create text file and write text programmatically in asp.net</title>
<script runat="server">

    protected void Button1_Click(object sender, System.EventArgs e)
    {	
		
        string appPath = Request.PhysicalApplicationPath;
        string filePath = appPath + "Feedback_Sheets/test2.txt";
        StreamWriter w;
        w = File.CreateText(filePath);
        w.WriteLine("Order Number " + OrderNumber.Text );
        w.WriteLine("First time build? " + FTB.SelectedValue);
        w.WriteLine();
        w.Flush();
        w.Close();
        Label1.Text = "Feedback sent successfully!<br />";
            }
</script>


</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        
        <br><br><br>
         <fieldset>
    <legend>Order Details:</legend>
   Order Number:<br>
		 <asp:TextBox id="OrderNumber" runat="server"></asp:TextBox>
		 <br><br>
  
    Is the job a first time build?<br>
  		 <asp:RadioButtonList id="FTB" runat="server">
			 <asp:ListItem>Yes</asp:ListItem>
			 <asp:ListItem>No</asp:ListItem>
		 </asp:RadioButtonList>
		 <br><br>
  
  </fieldset>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Create File and Write Text"
             ForeColor="SeaGreen"
             />   
    </div>
    </form>
</body>
</html>

请注意,您编写的测试会尝试覆盖相同的文件名,然后失败,并将访问者留在同一页面上,点击刷新会导致另一次尝试写入文件。 您需要每次构建一个唯一的文件名,
和   最佳做法是将访问者重定向到"成功!"。页面。

Note that the test you have written would try to overwrite the same filename, and fail, and leaves the visitor on the same page, where hitting refresh would cause another attempt to write the file.  You'll need to construct a unique filename each time, and  best practice would be to redirect the visitor to a "success!" page.

请注意,我将脚本部分移动到了父元素内部的位置(在这种情况下,我将其放在head部分中)。 您会在EW的代码视图中看到有关此问题的警告。

Note that I moved the script section to where is should be, inside a parent element (in this case I put it in the head section).  You would have seen a warning about that in EW's code view.

通常,这些问题最好在Asp.Net论坛上提出,
https://forums.asp.net/
。 你会在那里找到"学习" asp.net的区域。

Typically these questions are best asked on the Asp.Net forum, https://forums.asp.net/ .  There you will find "Learn" areas for asp.net.


这篇关于将表单输入传递给文本文件输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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