• 怎么样通过Javascript 将前台的数据传到C#后台处理


    一、前台主要页面:

     1. asp.net 自定义CUSTOMDATAGRID 数据绑定控件:当用CUSTOMDATAGRID显示数据的时候,实际上是一些td 显示。

    <tittle:CUSTOMDATAGRID id="grdResult" runat="server"
    DataKeyField="AuditdetailId"
    AutoGenerateColumns="False" Sort="False" CellPadding="0" FreezeColumns="0" FreezeHeader="False" GridHeight="240px"
    FreezeRows="0" PageSize="10" AllowSorting="false" Width="780px" GridWidth="780px" onitemdatabound="grdResult_ItemDataBound">
    <PagerStyle HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
    <Columns>
    <asp:TemplateColumn>
    <HeaderStyle CssClass="hidden"></HeaderStyle>
    <ItemStyle CssClass="hidden" />
    <ItemTemplate>
    <input type="text" id ="reAuditdetailId" runat="server" value='<%# Eval("AuditdetailId")%>' />
    </ItemTemplate>
    <FooterStyle CssClass="hidden"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle Wrap="False" HorizontalAlign="Center" Width="100px"></HeaderStyle>
    <ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
    <HeaderTemplate>
    <%= fetchResource.Resources["audit_installMaintainServices_grid_AuditItem"]%>
    </HeaderTemplate>
    <ItemTemplate>
    <%if (isDisplayDownload == true){ %>
    <a href="javascript:downloadFile('<%# Eval("UploadfileinfoId") %>');">
    <%} %>
    <asp:Label ID="labelAuditItem" runat="server" Text='<%# Eval("AuditItem")%>' style="95%; text-align:center;"></asp:Label>
    <%if (isDisplayDownload == true){ %>
    </a>
    <%} %>
    </ItemTemplate>
    <FooterStyle Wrap="False"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle Wrap="False" HorizontalAlign="Center" Width="100px"></HeaderStyle>
    <ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
    <HeaderTemplate>
    <%= fetchResource.Resources["audit_installMaintainServices_grid_InputItem"]%>
    </HeaderTemplate>
    <ItemTemplate>
    <input type="text" class="noemptyInput" style="95%; text-align:center;" maxlength="100" id ="txtInputItem" runat="server" value=''/>
    </ItemTemplate>
    <FooterStyle Wrap="False"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle Wrap="False" HorizontalAlign="Center" Width="100px"></HeaderStyle>
    <ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
    <HeaderTemplate>
    <%= fetchResource.Resources["audit_installMaintainServices_grid_AuditResult"]%>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="ddlAuditResult" runat="server" style="120px;" CssClass="drp">
    </asp:DropDownList>
    <asp:Label ID="lblAuditResultOfAuditResult" runat="server" Text='<%# Eval("AuditResultOfAuditResult")%>' style="95%; text-align:center;"></asp:Label>
    </ItemTemplate>
    <FooterStyle Wrap="False"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle CssClass="hidden"></HeaderStyle>
    <ItemStyle CssClass="hidden" />
    <ItemTemplate>
    <input type="text" id ="txtRelateField" runat="server" value='<%# Eval("RelateField")%>' />
    </ItemTemplate>
    <FooterStyle CssClass="hidden"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle CssClass="hidden"></HeaderStyle>
    <ItemStyle CssClass="hidden" />
    <ItemTemplate>
    <input type="text" id ="txtAuditdetailId" runat="server" value='<%# Eval("AuditdetailId")%>' />
    </ItemTemplate>
    <FooterStyle CssClass="hidden"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle CssClass="hidden"></HeaderStyle>
    <ItemStyle CssClass="hidden" />
    <ItemTemplate>
    <input type="text" id ="txtIsAutoAudit" runat="server" value='<%# Eval("IsAutoAudit")%>' />
    </ItemTemplate>
    <FooterStyle CssClass="hidden"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle CssClass="hidden"></HeaderStyle>
    <ItemStyle CssClass="hidden" />
    <ItemTemplate>
    <input type="text" id ="txtAuditresultId" runat="server" value='<%# Eval("AuditresultId")%>' />
    </ItemTemplate>
    <FooterStyle CssClass="hidden"></FooterStyle>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderStyle CssClass="hidden"></HeaderStyle>
    <ItemStyle CssClass="hidden" />
    <ItemTemplate>
    <input type="text" id ="reUploadfileinfoId" runat="server" value='<%# Eval("UploadfileinfoId")%>' />
    </ItemTemplate>
    <FooterStyle CssClass="hidden"></FooterStyle>
    </asp:TemplateColumn>
    </Columns>
    </tittle:CUSTOMDATAGRID>

       2. 前台Javascript: 

    function checkData() {
    var tr = document.getElementById("grdResult").getElementsByTagName("tr");
    for (i = 1; i < tr.length; i++) {//循环判定
    var td = tr[i].getElementsByTagName("td");
    if (td[3].children[0] != undefined && (parseInt(td[3].children[0].value) != 10 && parseInt(td[3].children[0].value) != 20)) {
    alert(第 + i + 行 + 的某值不能为空);
    return false;
    }
    }
    return true;
    }

    //保存按钮判断
    function savePageValue() {
    if (!checkData()) {
    return false;
    }
    var val = "";
    var auditItem = "";
    var inputItem = "";
    var auditResult = "";
    var trs = document.getElementById("grdResult").getElementsByTagName("tr");
    for (a = 1; a < trs.length; a++) {
    var tds = trs[a].getElementsByTagName("td");
    if (tds[1].children[0].childNodes[0].defaultValue != ""
    || tds[4].children[0].defaultValue != ""
    || tds[5].children[0].defaultValue != ""
    || tds[6].children[0].defaultValue != ""
    || tds[7].children[0].defaultValue != "") {
    auditItem = tds[1].children[0].childNodes[0].nodeValue + "$" //审核项目
    if (tds[2].children[0] != undefined) {
    inputItem = auditItem + tds[2].children[0].value + "$"
    }
    if (tds[2].children[0] == undefined) {//输入项目
    inputItem = auditItem + "&" + "$"
    }
    if (tds[3].children[0] != undefined) {//审核结果
    auditResult = inputItem + tds[3].children[0].value + "$"
    }
    if (tds[3].children[0] == undefined) {
    auditResult = inputItem + "," + "$"
    }
    val = auditResult
    + tds[4].children[0].value + "$"//关联字段
    + tds[5].children[0].value + "$"//审核字段ID
    + tds[6].children[0].value + "$"//是否通过审核
    + tds[7].children[0].value + "#"//审核结果ID  拼接字符串
    + val;
    }
    }
    document.forms[0].hd_list.value = val;
    return true;
    }

          其中:hd_list为隐藏域:<input type="hidden" id="hd_list" runat="server" />

        二、C#后台来接受前台通过Javascript 传来的数据,并将数据存于集合List<string[]> 中,<必须引入名称空间using System.Collections.Generic>

           主要代码如下:

          //定义全局的数据集合数组

           private List<string[]> saveList = new List<string[]>(); 

    #region 取得前台通过JS获取来的数据
    private void getProcessData()
    {
    Hashtable ht = new Hashtable();//实例化Hashtable对象

    //修改数据
    string[] OrderCount = this.hd_list.Value.Split('#');
    //OrderCount.Length-1去掉head行
    for (int i = 0; i < OrderCount.Length - 1; i++)
    {
    string[] orderDetail = OrderCount[i].Split('$');

    //审核明细ID,是否自动判断,关联字段,输入项目,审核是否通过,审核机构,审核人,审核时间
    string[] arrAuditItem = new string[8];

    #region 审核字段的属性“是否自动判断”为自动判断的场合,则需要保存以下信息(输入项目值,审核是否通过,审核机构,审核人,审核时间)
    if (Constants.C_VALUE_AUDITITEM_ISAUTOAUDIT.Equals(orderDetail[5]))
    {

    //输入项目
    if (orderDetail[1] != "&")
    {
    arrAuditItem[3] = orderDetail[1];
    }
    //输入项目
    if (orderDetail[1] == "&")
    {
    arrAuditItem[3] = "";
    }

    //关联字段
    arrAuditItem[2] = orderDetail[3];

    //放关联字段放在服务单字段里面查询
    string servicesRelateField = logic.getRelateFieldById(arrAuditItem[2], dto.ServicesId);
    //输入项目值=服务单.[关联字段]的场合,则设定“审核是否通过”为“通过”
    if (servicesRelateField == arrAuditItem[3])
    {
    arrAuditItem[3] = Constants.C_VALUE_ISTAUDITPASS; //20代表审核通过
    }
    }
    #endregion

    #region 审核字段的属性“是否自动判断”为手动判断的场合,则需要保存以下信息(是否自动判断 审核是否通过 审核机构 审核人 审核时间)
    if (Constants.C_VALUE_AUDITITEM_ISHANDAUDIT.Equals(orderDetail[5]))
    {
    //审核是否通过
    if (orderDetail[2] != ",")
    {
    arrAuditItem[4] = orderDetail[2];
    }
    }
    #endregion

    //审核明细ID
    arrAuditItem[0] = orderDetail[4];

    //是否自动判断
    arrAuditItem[1] = orderDetail[5];

    //审核结果ID
    dto.Auditresultid = orderDetail[6];

    //审核机构
    arrAuditItem[5] = dto.BelongShopID;

    //审核人
    arrAuditItem[6] = dto.BelongShopMemberName;

    //审核时间
    arrAuditItem[7] = DateTime.Now.AddDays(-8).ToString("yyyy/MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);//执行Update的时候,要 在当前时间的基础上减去8个小时(MS CRM操作就需要这样子处理)

      ht.Add(i, arrAuditItem[0]);将审核字段明细ID添加到哈希表中

       saveList.Add(arrAuditItem);把数组对象arrAuditItem添加到全局集合数组中。
     }

    }
    #endregion

        

       

  • 相关阅读:
    TestNG 单元测试框架的使用
    HDU1255 覆盖的面积(线段树+扫描线)
    AcWing1169 糖果(差分约数)
    牛客 Treepath(树形dp)
    牛客 Shortest Path (dfs+思维)
    牛客 树(dfs序)
    牛客 城市网络(倍增)
    牛客 Borrow Classroom (LCA)
    CF710E Generate a String(dp)
    c#委托
  • 原文地址:https://www.cnblogs.com/allenhua/p/2987828.html
Copyright © 2020-2023  润新知