获取用户输入从web表单保存到文本文件

本文关键字:保存 文本 文件 表单 web 用户 输入 获取 | 更新日期: 2023-09-27 18:02:28

我有一个web表单,由复选框,单选按钮和日期时间选择器组成。

所有的选项工作的形式,但我需要以某种方式得到所有的用户输入,例如当有人检查'checkBox1'保存到一个文本文件。我不知道如何在c#中做到这一点。

到目前为止我有-

string path = "path";
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            using (FileStream fs = File.Create(path))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("");
                // Add some information to the file.
                fs.Write(info, 0, info.Length);

我可以让文件写出每个特定的复选框名称,但我希望能够只写出已选中的复选框。

我想应该是- if checkbox1.checkedstate = true

获取用户输入从web表单保存到文本文件

如果想在选中复选框时立即写入文件,则将AutoPostBack设置为true,并添加oncheckedchanged事件

示例(带有两个复选框):

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack=true OnCheckedChanged="CheckBox_CheckedChanged"/>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack=true OnCheckedChanged="CheckBox_CheckedChanged"/>
在后台代码

    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chb = (CheckBox)sender;
        if (chb.Checked)
        {
            //checkbox is checked
            //chb.ID gets the ID of the sender (i.e. checkbox1, checkbox2, etc)
            //write to file
        }
    }

你想检查

if(checkbox1.Checked)
   write code

虽然你的问题不是很清楚,我假设你想做点什么,当你找到CheckBox checked,所以你可以做

if(myCheckBox.Checked) //Checked is a boolean 
/////// do something