【问题标题】:HTML form submit, .Net, save fields as XMLHTML 表单提交,.Net,将字段保存为 XML
【发布时间】:2014-03-28 07:12:29
【问题描述】:

我们有一个带有表单的 HTML 页面。 我们正在使用 jQuery AJAX 从 XML 文件中预填充表单。

我们希望表单在提交时保存在我们的 default.xml 文件上。我们很可能会使用 ASP.Net 作为我们的服务器端。

我们的问题是,为了将信息保存在表单中并保存在我们的 XML 文件中,ASP.Net/C# 会是什么样子。

提前致谢。

编辑

这是我们的例子:

 <option id="timeBlockOne">
  <select>12:00</select>
  <select>1:00</select>
 </option>

<option id="titleBlockOne">
<select>Title One</select>
<select>Title Two</select>
</option>

 <option id="titleBlockTwo">
 <select>Title three</select>
 <select>Title Four</select>
 </option>

 <option id="timeBlockTwo">
 <select>12:00</select>
 <select>1:00</select>
 </option>

 ...etc up to 10 blocks.

  <button action="Post">Submit</button>

所需的 XML 输出。

 <main>
    <itemOne>
      <timeBlockOneValue>Value selected from form</timeBlockOneValue>
      <titleBlockOneValue>Value Selected from form</titleBlockOneValue>
    </itemOne>
    <itemTwo>
      <timeBlockTwoValue>Value selected from form</timeBlockTwoValue>
      <titleBlockTwoValue>Value Selected from form</titleBlockTwoValue>
    </itemTwo>
     ...etc
 </main>

希望这对我的问题有所帮助。

【问题讨论】:

  • 太宽泛了。 how to upload and save file with ASP.Net 上有几个问题/文章 - 您可能想查看它们以使您的问题更具体...包括 MSDN 上的 How to upload a file to a Web server in ASP.NET by using Visual C# .NET 文章。
  • 如果你的表单是由服务器端处理的,那么所有元素都应该有 runat="server",那么你应该能够遍历所有 FORM 组件并从每个组件获取用户输入然后填充你的 xml检查每个元素时的文件。如果没有,您可以使用 JQuerey 遍历所有 FORM 元素,完成后您可以从 JavaScript 调用 C# WebMethod 并传递带有 XML 数据的字符串,然后您可以将 XML 文件保存在服务器端。如果您需要帮助,请告诉我。
  • @Lucidgold。我知道 runat="server" 的需求,并且该页面应该是 Webform。我只从 JS 调用了 C# webmethods 几次。这就是问题所在。我们希望从 HTML 表单中获取每个
  • 请检查我的回答,如果有帮助请告诉我。

标签: c# jquery html asp.net


【解决方案1】:

好的,这里有一个可以让你走上正轨的方法,如果不清楚,请告诉我。

JavaScript:

function GetXMLButton() {
    var xml = "<main>";
    var i = 1;
    // Loop for each OPTION element in the myForm div
    $("#myform select").each(function() {
        // Get value of selected option.
        var selectedValue = $(this).val();
        xml = xml + "<item_" + i + "><" + this.id + ">" + selectedValue + "</" + this.id + "></item_" + i + ">";

        i++;
    });

    xml = xml + "</main>";

    // Call C# WebMethod to save into xml file
    PageMethods.SaveXMLFile(xml); 
}

ASPX:确保在 ScriptManager 对象中设置 EnablePageMethods = true。另外我认为你可能已经切换了你的 OPTION 和 SELECT 标签,检查你的代码,它应该是 SELECT 然后是 OPTION 标签。

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="fileupdatepanel">
        <ContentTemplate>

            <div id="myForm">
                <select id="timeBlockOne">
                    <option selected>12:00</option>
                    <option>1:00</option>
                </select>

                <select id="titleBlockOne">
                    <option>Title One</option>
                    <option selected>Title Two</option>
                </select>

                <select id="titleBlockTwo">
                    <option selected>Title three</option>
                    <option>Title Four</option>
                </select>

               <select id="timeBlockTwo">
                   <option>12:00</option>
                   <option selected>1:00</option>
               </select>
           </div>

       </ContentTemplate>
   </asp:UpdatePanel>
 <asp:Button ID="button1" runat="server" Text="Get XML" OnClientClick="return GetXMLButton();" /> 

C# 代码隐藏: 您需要参考以下内容:

using System.Web.Services;

然后添加以下方法并确保在方法声明之前放置[WebMethod]

[WebMethod]
public static void SaveXMLFile(string formXMLData)   
{
    // Put your code to convert formXMLData string to an XML File and save/upload on/to server
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2011-04-25
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多