动态添加多个文本框

我要做一个这样的东西:

当我点击“添加日期”的时候会出现如下图所示的框,包括:1个日期名文本框、2个文本框日期几号到几号(可以动态添加、删除)、2个文本框时间几点到几点(可以动态添加删除)。

最后可能是这个样子的,页面有多个日期框。



日期名有一个单独表。
日期有专门存日期的表,和日期名表关联。
时间有专门寸时间的表,和日期名表关联。




后来我的实现方式


前台:

<asp:Content ID="Content3" runat="server" ContentPlaceHolderID="head">
    <script src="../../Content/JavaScript/jquery-1.4.1.min.js" type="text/javascript"
        language="javascript"></script>
    <style type="text/css">
        .dateBase
        {
            width: 129px;
            height: 19px;
            padding: 2px 0 0 2px;
            border: none;
            background: url(/Content/images/V2/panel_date.gif) no-repeat;
        }
        .dateFocus
        {
            width: 129px;
            height: 19px;
            padding: 2px 0 0 2px;
            border: none;
            background: url(/Content/images/V2/panel_date.gif) no-repeat;
            background-position: -131px 0px;
        }
        
        .hidden
        {
            /*display: none;*/
        }
    </style>
    <script type="text/javascript">
        function DateTimeFormat(strFormat) {
            if (strFormat == "Y") {
                WdatePicker({ dateFmt: 'yyyy' })
            }
            else if (strFormat == "M") {
                WdatePicker({ dateFmt: 'yyyy-MM' })
            }
            else if (strFormat == "D") {
                WdatePicker({ dateFmt: 'yyyy-MM-dd' })
            }
            else if (strFormat == "H") {
                WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm' })
            }
            else if (strFormat == "h") {
                WdatePicker({ dateFmt: 'HH:mm:ss' })
            }
        }

        function SetDateInputFocus(flag, control) {
            if (flag == 1) {
                control.className = "dateFocus";
            }
            else
                control.className = "dateBase";
        }

        function addSeasonFirst() {//添加季节类型
            var counts = document.getElementById('<%=hidTypeTempIds.ClientID %>').value; //取季节类型临时隐藏Id从1开始
            var seasonCount = parseInt(counts) + parseInt(1); //点击添加会将临时隐藏Id + 1
            $('#div_Season').append("<table width='100%' id='table_Type" + seasonCount + "'>" +
                                    "     <tr>" +
                                    "       <td>" +
                                    "           <div id='div_Type" + seasonCount + "'>" +
                                    "               <table>" +
                                    "                   <tr >" +
                                    "                       <td width='120'>季节类型:</td>" +
                                    "                       <td width='300'><input id='txtPhaseName" + seasonCount + "' name='txtPhaseName" + seasonCount + "' type='text' class='input_short' style='width: 129px;' /></td>" +
                                    "                       <td width='130'></td>" +
                                    "                       <td><input id='btnAddSeason' type='button' class='btn' value='添加季节类型' οnclick='addSeason()' /></td>" +
//                                    "                       <td><input type='button' class='btn' οnclick='delSeason(this)' value='删除' /></td>" +
                                    "                   </tr>" +
                                    "               </table>" +
                                    "           </div>" +
                                    "       </td>" +
                                    "     </tr>" +
                                    "     <tr>" +
                                    "       <td>" +
                                    "           <div id='div_Date'>" +
                                    "               <table>" +
                                    "                   <tr>" +
                                    "                       <td width='120'>日期时间段:</td>" +
                                    "                       <td width='300'>" +
                                    "                           <input id='txtStartDate" + seasonCount + "_1' name='txtStartDate" + seasonCount + "_1' type='text' class='dateBase' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                           -" +
                                    "                           <input id='txtEndDate" + seasonCount + "_1' name='txtEndDate" + seasonCount + "_1' type='text' class='dateBase' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                       </td>" +
                                    "                       <td width='130'></td>" +
                                    "                       <td>" +
                                    "                           <input type='button' class='btn' οnclick='addDate(this)' value='添加日期时间段' />" +
                                    "                           <input id='hidDateTempIds" + seasonCount + "' name='hidDateTempIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                           <input id='hidDateIds" + seasonCount + "' name='hidDateIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                       </td>" +
                                    "                   </tr>" +
                                    "               </table>" +
                                    "           </div>" +
                                    "       </td>" +
                                    "     </tr>" +
                                    "     <tr>" +
                                    "       <td>" +
                                    "           <div id='div_Time'>" +
                                    "               <table>" +
                                    "                   <tr>" +
                                    "                       <td width='120'>小时时间段:</td>" +
                                    "                       <td width='300'>" +
                                    "                           <input id='txtStartTime" + seasonCount + "_1' name='txtStartTime" + seasonCount + "_1'  type='text' class='dateBase' οnclick='DateTimeFormat('h');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                           -" +
                                    "                           <input id='txtEndTime" + seasonCount + "_1'name='txtEndTime" + seasonCount + "_1' type='text' class='dateBase' οnclick='DateTimeFormat('h');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                       </td>" +
                                    "                       <td width='130'><input id='txtPrice" + seasonCount + "_1' name='txtPrice" + seasonCount + "_1' type='text' class='input_short' />元/度</td>" +
                                    "                       <td>" +
                                    "                           <input type='button' class='btn' οnclick='addTime(this)' value='添加小时时间段' />" +
                                    "                           <input id='hidTimeTempIds" + seasonCount + "' name='hidTimeTempIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                           <input id='hidTimeIds" + seasonCount + "' name='hidTimeIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                       </td>" +
                                    "                   </tr>" +
                                    "               </table>" +
                                    "           </div>" +
                                    "         <hr />" +
                                    "       </td>" +
                                    "     </tr>" +
                                    "</table>");

            document.getElementById('<%=hidTypeTempIds.ClientID %>').value = seasonCount; //将 + 1 后的临时隐藏Id返回给隐藏控件,下次取时候用。
            document.getElementById('<%=hidTypeIds.ClientID %>').value += "," + seasonCount; //最后要用到的Id串
            alert(document.getElementById('<%=hidTypeTempIds.ClientID %>').value);
            alert(document.getElementById('<%=hidTypeIds.ClientID %>').value);
        }

        function addSeason() {//添加季节类型
            var counts = document.getElementById('<%=hidTypeTempIds.ClientID %>').value; //取季节类型临时隐藏Id从1开始
            var seasonCount = parseInt(counts) + parseInt(1); //点击添加会将临时隐藏Id + 1
            $('#div_Season').append("<table width='100%' id='table_Type" + seasonCount + "'>" +
                                    "     <tr>" +
                                    "       <td>" +
                                    "           <div id='div_Type" + seasonCount + "'>" +
                                    "               <table>" +
                                    "                   <tr >" +
                                    "                       <td width='120'>季节类型:</td>" +
                                    "                       <td width='300'><input id='txtPhaseName" + seasonCount + "' name='txtPhaseName" + seasonCount + "' type='text' class='input_short' style='width: 129px;' /></td>" +
                                    "                       <td width='130'></td>" +
                                    "                       <td><input type='button' class='btn' οnclick='delSeason(this)' value='删除' /></td>" +
                                    "                   </tr>" +
                                    "               </table>" +
                                    "           </div>" +
                                    "       </td>" +
                                    "     </tr>" +
                                    "     <tr>" +
                                    "       <td>" +
                                    "           <div id='div_Date'>" +
                                    "               <table>" +
                                    "                   <tr>" +
                                    "                       <td width='120'>日期时间段:</td>" +
                                    "                       <td width='300'>" +
                                    "                           <input id='txtStartDate" + seasonCount + "_1' name='txtStartDate" + seasonCount + "_1' type='text' class='dateBase' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                           -" +
                                    "                           <input id='txtEndDate" + seasonCount + "_1' name='txtEndDate" + seasonCount + "_1' type='text' class='dateBase' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                       </td>" +
                                    "                       <td width='130'></td>" +
                                    "                       <td>" +
                                    "                           <input type='button' class='btn' οnclick='addDate(this)' value='添加日期时间段' />" +
                                    "                           <input id='hidDateTempIds" + seasonCount + "' name='hidDateTempIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                           <input id='hidDateIds" + seasonCount + "' name='hidDateIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                       </td>" +
                                    "                   </tr>" +
                                    "               </table>" +
                                    "           </div>" +
                                    "       </td>" +
                                    "     </tr>" +
                                    "     <tr>" +
                                    "       <td>" +
                                    "           <div id='div_Time'>" +
                                    "               <table>" +
                                    "                   <tr>" +
                                    "                       <td width='120'>小时时间段:</td>" +
                                    "                       <td width='300'>" +
                                    "                           <input id='txtStartTime" + seasonCount + "_1' name='txtStartTime" + seasonCount + "_1'  type='text' class='dateBase' οnclick='DateTimeFormat('h');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                           -" +
                                    "                           <input id='txtEndTime" + seasonCount + "_1'name='txtEndTime" + seasonCount + "_1' type='text' class='dateBase' οnclick='DateTimeFormat('h');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "                       </td>" +
                                    "                       <td width='130'><input id='txtPrice" + seasonCount + "_1' name='txtPrice" + seasonCount + "_1' type='text' class='input_short' />元/度</td>" +
                                    "                       <td>" +
                                    "                           <input type='button' class='btn' οnclick='addTime(this)' value='添加小时时间段' />" +
                                    "                           <input id='hidTimeTempIds" + seasonCount + "' name='hidTimeTempIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                           <input id='hidTimeIds" + seasonCount + "' name='hidTimeIds" + seasonCount + "' type='text' class='hidden' value='1' />" +
                                    "                       </td>" +
                                    "                   </tr>" +
                                    "               </table>" +
                                    "           </div>" +
                                    "         <hr />" +
                                    "       </td>" +
                                    "     </tr>" +
                                    "</table>");

            document.getElementById('<%=hidTypeTempIds.ClientID %>').value = seasonCount; //将 + 1 后的临时隐藏Id返回给隐藏控件,下次取时候用。
            document.getElementById('<%=hidTypeIds.ClientID %>').value += "," + seasonCount; //最后要用到的Id串
            alert(document.getElementById('<%=hidTypeTempIds.ClientID %>').value);
            alert(document.getElementById('<%=hidTypeIds.ClientID %>').value);
        }
        function delSeason(season) {
            var id = $(season).parent().closest("div").attr("id").replace('div_Type', ''); //取动态添加的季节类型Id
            var typeIds = document.getElementById('<%=hidTypeIds.ClientID %>').value;
            document.getElementById('<%=hidTypeIds.ClientID %>').value = typeIds.replace(id, ''); //去掉删除的Id
            $(season).parent().parent().parent().parent().parent().parent().parent().parent().parent().remove(); //页面删除
        }



        function addDate(date) {//添加日期
            var typeId = $(date).parent().parent().parent().parent().parent().closest("table").attr("id").replace('table_Type', ''); //取动态添加的季节类型Id
            var counts = document.getElementById('hidDateTempIds' + typeId).value;
            var dateCount = parseInt(counts) + parseInt(1);
            $(date).parent().parent().parent().parent().parent().append("<table id='tableDate" + dateCount + "'>" +
                                    "    <tr>" +
                                    "        <td width='120'>日期时间段:</td>" +
                                    "        <td width='300'>" +
                                    "             <input id='txtStartDate" + typeId + "_" + dateCount + "' name='txtStartDate" + typeId + "_" + dateCount + "' type='text' class='dateBase' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "             -" +
                                    "             <input id='txtEndDate" + typeId + "_" + dateCount + "' name='txtEndDate" + typeId + "_" + dateCount + "' type='text' class='dateBase' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                    "         </td>" +
                                    "        <td width='130'></td>" +
                                    "        <td><input type='button' οnclick='delDate(this)' ID='Button1' Class='btn' value='删除' /></td>" +
                                    "    </tr>" +
                                    "</table>");
            document.getElementById('hidDateTempIds' + typeId).value = dateCount;
            document.getElementById('hidDateIds' + typeId).value += "," + dateCount;
        }
        function delDate(date) {
            var dateId = $(date).parent().parent().parent().closest("table").attr("id").replace('tableDate', ''); //日期Id
            var typeId = $(date).parent().parent().parent().parent().parent().closest("table").attr("id").replace('table_Type', ''); //季节类型Id
            var dateIds = document.getElementById('hidDateIds' + typeId).value; //当前季节类型下的日期Id集合
            document.getElementById('hidDateIds' + typeId).value = dateIds.replace(dateId, ''); //去掉某个日期Id后的,当前季节类型下的日期Id集合
            $(date).parent().parent().parent().parent().remove(); //页面删除
        }



        function addTime(time) {//添加时间
            var typeId = $(time).parent().parent().parent().parent().parent().closest("table").attr("id").replace('table_Type', ''); //取动态添加的季节类型Id
            var counts = document.getElementById('hidTimeTempIds' + typeId).value;
            var timeCount = parseInt(counts) + parseInt(1);
            $(time).parent().parent().parent().parent().parent().append("<table id='tableTime" + timeCount + "'>" +
                                  "    <tr>" +
                                  "        <td width='120'>小时时间段:</td>" +
                                  "        <td width='300'>" +
                                  "            <input type='text' ID='txtStartTime" + typeId + "_" + timeCount + "' name='txtStartTime" + typeId + "_" + timeCount + "' Class='dateBase'  οnclick='DateTimeFormat('h');' onMouseOver='SetDateInputFocus(1,this)' onMouseOut='SetDateInputFocus(0,this)'/>" +
                                  "            -" +
                                  "            <input type='text' ID='txtEndTime" + typeId + "_" + timeCount + "' name='txtEndTime" + typeId + "_" + timeCount + "' Class='dateBase'  οnclick='DateTimeFormat('h');' onMouseOver='SetDateInputFocus(1,this)' onMouseOut='SetDateInputFocus(0,this)'/>" +
                                  "        </td>" +
                                  "        <td width='130'><input ID='txtPrice" + typeId + "_" + timeCount + "' name='txtPrice" + typeId + "_" + timeCount + "' type='text' Class='input_short'/>元/度</td>" +
                                  "        <td><input type='button' οnclick='delTime(this)' ID='Button1' Class='btn' value='删除' /></td>" +
                                  "    </tr>" +
                                  "</table>");
            document.getElementById('hidTimeTempIds' + typeId).value = timeCount;
            document.getElementById('hidTimeIds' + typeId).value += "," + timeCount;
        }
        function delTime(time) {
            var timeId = $(time).parent().parent().parent().closest("table").attr("id").replace('tableTime', ''); //时间Id
            var typeId = $(time).parent().parent().parent().parent().parent().closest("table").attr("id").replace('table_Type', ''); //季节类型Id
            var timeIds = document.getElementById('hidTimeIds' + typeId).value; //当前季节类型下的时间Id集合
            document.getElementById('hidTimeIds' + typeId).value = timeIds.replace(timeId, ''); //去掉某个日期Id后的,当前季节类型下的日期Id集合
            $(time).parent().parent().parent().parent().remove(); //页面删除
        }

        //验证是否已有默认价格
        //时间
        //日期
        //空文本框
    </script>
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <table>
        <tr>
            <th>
                <span>电费设置</span>
            </th>
        </tr>
        <tr>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                <table class=" table_data">
                    <tr>
                        <td>
                            * 用电类型:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPriceName" CssClass="input_short" runat="server" Width="129"></asp:TextBox>
                        </td>
                        <td>
                            * 费用类型:
                        </td>
                        <td>
                            <asp:RadioButtonList ID="rdiPriceType" runat="server" RepeatColumns="2" CssClass="td_left"
                                OnSelectedIndexChanged="rdiPriceType_SelectedIndexChanged" AutoPostBack="True">
                                <asp:ListItem Value="0">单一电价</asp:ListItem>
                                <asp:ListItem Value="1" Selected="True">季节电价</asp:ListItem>
                            </asp:RadioButtonList>
                        </td>
                        <td>
                            * 是否默认价格:
                        </td>
                        <td>
                            <asp:RadioButtonList ID="rdiIsDefault" runat="server" RepeatColumns="2" CssClass="td_left">
                                <asp:ListItem Value="0" Selected="True">否</asp:ListItem>
                                <asp:ListItem Value="1">是</asp:ListItem>
                            </asp:RadioButtonList>
                        </td>
                    </tr>
                    <tr id="trPriceType_Single" runat="server">
                        <td>
                            * 价格:
                        </td>
                        <td colspan="5">
                            <asp:TextBox ID="txtPriceSingle" CssClass="input_short" runat="server" Width="129"></asp:TextBox>元/度
                        </td>
                    </tr>
                    <tr id="trPriceType_More" runat="server">
                        <td colspan="6">
                            <div id="div_Season">
                                <input id="btnAddSeason" type="button" class="hidden" value="添加季节类型" οnclick="addSeasonFirst()" />
                                <%=htmStr%>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table style="text-align: center;">
                    <tr>
                        <td style="padding-top: 10px;">
                            <asp:Button ID="btnSave" CssClass="btn" runat="server" Text="保存" OnClick="btnSave_Click" />
                            <asp:Button ID="btnCanel" CssClass="btn" runat="server" Text="返回" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <asp:HiddenField ID="hidTypeTempIds" runat="server" Value="0" />
    <asp:HiddenField ID="hidTypeIds" runat="server" Value="" />
    <asp:HiddenField ID="hidPriceId" runat="server" Value="0" />
</asp:Content>


后台:

  public static string htmStr;
        int requestPriceId = 0;
        #region 窗体加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //修改
                if (!string.IsNullOrWhiteSpace(Request.QueryString["priceId"]))
                {
                    requestPriceId = int.Parse(Request.QueryString["priceId"]);
                    BindData(requestPriceId);
                }
                else//添加
                {//默认非单一价格
                    this.trPriceType_More.Visible = true;
                    this.trPriceType_Single.Visible = false;
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>document.getElementById('btnAddSeason').onclick();</script>");
                }
            }
        }
        #endregion

        #region 修改加载数据
        public void BindData(int priceId)
        {
            List<T_DT_MainEnergyPrice_Phase> phase = new List<T_DT_MainEnergyPrice_Phase>();
            List<T_DT_MainEnergyPrice_Phase_Date> price_Phase_DateList = new List<T_DT_MainEnergyPrice_Phase_Date>();
            List<T_DT_MainEnergyPrice_Phase_TimePrice> price_Phase_TimePriceList = new List<T_DT_MainEnergyPrice_Phase_TimePrice>();

            T_DT_MainEnergyPrice price = new BizLogic.EnergyPrice.MainEnergyPriceService().GetPrice(priceId,
                                                                                                out phase,
                                                                                                out price_Phase_DateList,
                                                                                                out price_Phase_TimePriceList);
            #region 填充主记录
            txtPriceName.Text = price.Name;
            rdiPriceType.SelectedValue = price.PriceType.ToString();
            if (price.PriceType.ToString() == "1")
            {
                this.trPriceType_More.Visible = true;
                this.trPriceType_Single.Visible = false;

                txtPriceSingle.Text = price.Price.ToString();
                rdiIsDefault.SelectedValue = price.IsDefault.ToString();
            }
            #endregion
            String strHtml = "";
            if (phase.Count > 0)
            {
                int phaseNo = 1;
                string phaseNoStr = string.Empty;
                foreach (var phaseItem in phase)
                {
                    strHtml += "<table id='table_Type" + phaseNo + "' width='100%'>" +
                               "     <tr>" +
                               "         <td>" +
                               "             <div id='div_Type" + phaseNo + "'>" +
                               "                 <table>" +
                               "                     <tr>" +
                               "                         <td width='120'>季节类型:</td>" +
                               "                         <td width='300'><input id='txtPhaseName" + phaseNo + "' name='txtPhaseName" + phaseNo + "' value='" + phaseItem.PhaseName + "' type='text' class='input_short' width='129' /></td>" +
                               "                         <td width='130'></td>" +
                               "                         <td>";
                    if (phaseNo == 1)
                        strHtml += "                         <input id='btnAddSeason' type='button' class='btn' value='添加季节类型' οnclick='addSeason()' />";
                    else
                        strHtml += "                         <input type='button' class='btn' οnclick='delSeason(this)' value='删除' />";
                    strHtml += "                         </td>" +
                               "                     </tr>" +
                               "                 </table>" +
                               "             </div>" +
                               "         </td>" +
                               "     </tr>" +
                               "     <tr>" +
                               "         <td>" +
                               "             <div id='div_Date" + phaseNo + "'>";
                    int dateNo = 1;
                    string dateNoStr = string.Empty;
                    var dateList = price_Phase_DateList.Where(c => c.PhaseId == phaseItem.PhaseId).ToList();
                    foreach (var dateItem in dateList)
                    {
                        strHtml += "             <table>" +
                                   "                 <tr>" +
                                   "                     <td width='120'>日期时间段:</td>" +
                                   "                     <td width='300'>" +
                                   "                         <input id='txtStartDate" + phaseNo + "_" + dateNo + "' name='txtStartDate" + phaseNo + "_" + dateNo + "' value='" + dateItem.StartDate.Value.ToString("yyyy-MM-dd") + "' class='dateBase' type='text' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                   "                         -" +
                                   "                         <input id='txtEndDate" + phaseNo + "_" + dateNo + "' name='txtEndDate" + phaseNo + "_" + dateNo + "' value='" + dateItem.EndDate.Value.ToString("yyyy-MM-dd") + "' class='dateBase' type='text' οnclick='DateTimeFormat('D');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                   "                     </td>" +
                                   "                     <td width='130'></td>" +
                                   "                     <td>";
                        if (dateNo == 1)
                        {
                            for (int i = 1; i <= dateList.Count(); i++)
                            {
                                dateNoStr += i + ",";
                            }
                            strHtml += "                     <input type='button' class='btn' οnclick='addDate(this)' value='添加日期时间段' />" +
                                       "                     <input id='hidDateTempIds" + phaseNo + "' name='hidDateTempIds" + phaseNo + "' value='" + dateList.Count() + "' type='text' class='hidden' value='1' />" +
                                       "                     <input id='hidDateIds" + phaseNo + "' name='hidDateIds" + phaseNo + "' value='" + dateNoStr + "' type='text' class='hidden' value='1' />";
                        }
                        else
                            strHtml += "                     <input type='button' οnclick='delDate(this)' Class='btn' value='删除' />";
                        strHtml += "                     </td>" +
                                   "                 </tr>" +
                                   "             </table>";
                        dateNo++;
                    }
                    strHtml += "             </div>" +
                               "         </td>" +
                               "     </tr>" +
                               "     <tr>" +
                               "         <td>" +
                               "             <div id='div_Time" + phaseNo + "'>";
                    int timeNo = 1;
                    string timeNoStr = string.Empty;
                    var timeList = price_Phase_TimePriceList.Where(c => c.PhaseId == phaseItem.PhaseId).ToList();
                    foreach (var timeItem in timeList)
                    {
                        strHtml += "               <table>" +
                                   "                    <tr>" +
                                   "                        <td width='120'>小时时间段:</td>" +
                                   "                        <td width='300'>" +
                                   "                            <input id='txtStartTime" + phaseNo + "_" + timeNo + "' name='txtStartTime" + phaseNo + "_" + timeNo + "' value='" + timeItem.StartTime + "' class='dateBase' type='text' οnclick='DateTimeFormat('h');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                   "                            -" +
                                   "                            <input id='txtEndTime" + phaseNo + "_" + timeNo + "' name='txtEndTime" + phaseNo + "_" + timeNo + "' value='" + timeItem.EndTime + "' class='dateBase' type='text' οnclick='DateTimeFormat('h');' οnmοuseοver='SetDateInputFocus(1,this)' οnmοuseοut='SetDateInputFocus(0,this)' />" +
                                   "                        </td>" +
                                   "                        <td width='130'><input id='txtPrice" + phaseNo + "_" + timeNo + "' name='txtPrice" + phaseNo + "_" + timeNo + "' value='" + timeItem.Price + "' class='input_short' type='text' />元/度</td>" +
                                   "                        <td>";
                        if (timeNo == 1)
                        {
                            for (int i = 1; i <= timeList.Count(); i++)
                            {
                                timeNoStr += i + ",";
                            }
                            strHtml += "                        <input type='button' class='btn' οnclick='addTime(this)' value='添加小时时间段' />" +
                                       "                        <input id='hidTimeTempIds" + phaseNo + "' name='hidTimeTempIds" + phaseNo + "' value='" + timeList.Count() + "' type='text' class='hidden' value='1' />" +
                                       "                        <input id='hidTimeIds" + phaseNo + "' name='hidTimeIds" + phaseNo + "' value='" + timeNoStr + "' type='text' class='hidden' value='1' />";
                        }
                        else
                            strHtml += "                        <input type='button' οnclick='delTime(this)' Class='btn' value='删除' />";
                        strHtml += "                        </td>" +
                                   "                    </tr>" +
                                   "               </table>";
                        timeNo++;
                    }
                    strHtml += "            </div>" +
                               "            <hr />" +
                               "        </td>" +
                               "     </tr>" +
                               "</table>";
                    phaseNoStr += phaseNo + ",";
                    phaseNo++;
                }
                hidTypeIds.Value = phaseNoStr;
                hidTypeTempIds.Value = phase.Count().ToString();
            }
            htmStr = strHtml;

            this.hidPriceId.Value = price.Id.ToString();
        }
        #endregion

        #region 费用类型切换
        protected void rdiPriceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdiPriceType.SelectedValue == "0")
            {
                this.trPriceType_More.Visible = false;
                this.trPriceType_Single.Visible = true;
            }
            else
            {
                this.trPriceType_More.Visible = true;
                this.trPriceType_Single.Visible = false;
            }
        }
        #endregion

        #region 保存
        protected void btnSave_Click(object sender, EventArgs e)
        {

            #region 添加电费配置主记录并返回Id
            T_DT_MainEnergyPrice priceModel = new T_DT_MainEnergyPrice();
            priceModel.Name = txtPriceName.Text;
            priceModel.EnergyType = 0;
            priceModel.PriceType = Byte.Parse(rdiPriceType.SelectedValue);
            if (rdiPriceType.SelectedValue == "0")
            {
                priceModel.Price = Convert.ToDouble(txtPriceSingle.Text);
            }
            priceModel.IsDefault = Convert.ToBoolean(Convert.ToInt32(rdiIsDefault.SelectedValue));
            int priceId = 0;
            if (Convert.ToInt32(hidPriceId.Value) > 0)
            {//修改
                priceModel.Id = Convert.ToInt32(hidPriceId.Value);
                bool priceIdss = new BizLogic.EnergyPrice.MainEnergyPriceService().UpdEnergyPrice(priceModel);
                if (!priceIdss)
                {
                    //谈框提示
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "<script>alert('更新失败!');</script>", false);
                    return;
                }
                priceId = Convert.ToInt32(hidPriceId.Value);
            }
            else
            {//添加
                priceId = new BizLogic.EnergyPrice.MainEnergyPriceService().AddEnergyPrice(priceModel);
            }
            #endregion

            if (priceId > 0)
            {
                T_DT_MainEnergyPrice_Phase phase = null;
                List<T_DT_MainEnergyPrice_Phase_Date> price_Phase_DateList = null;
                List<T_DT_MainEnergyPrice_Phase_TimePrice> price_Phase_TimePriceList = null;


                string typeStr = hidTypeIds.Value;
                string[] typeSplit = typeStr.Split(',');
                foreach (var typeNo in typeSplit)
                {
                    if (!string.IsNullOrWhiteSpace(typeNo))
                    {
                        phase = new T_DT_MainEnergyPrice_Phase();
                        price_Phase_DateList = new List<T_DT_MainEnergyPrice_Phase_Date>();
                        price_Phase_TimePriceList = new List<T_DT_MainEnergyPrice_Phase_TimePrice>();
                        phase.PhaseName = string.IsNullOrEmpty(Request.Form["txtPhaseName" + typeNo]) ? null : Request.Form["txtPhaseName" + typeNo];

                        string dateStr = Request.Form["hidDateIds" + typeNo];
                        string[] dateSplit = dateStr.Split(',');
                        foreach (var dateNo in dateSplit)
                        {
                            if (!string.IsNullOrWhiteSpace(dateNo))
                            {
                                string startDate = Request.Form["txtStartDate" + typeNo + "_" + dateNo];
                                string endDate = Request.Form["txtEndDate" + typeNo + "_" + dateNo];


                                T_DT_MainEnergyPrice_Phase_Date phase_Date = new T_DT_MainEnergyPrice_Phase_Date();
                                phase_Date.StartDate = DateTime.ParseExact(startDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                                phase_Date.EndDate = DateTime.ParseExact(endDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                                price_Phase_DateList.Add(phase_Date);
                            }
                        }

                        string timeStr = Request.Form["hidTimeIds" + typeNo];
                        string[] timeSplit = timeStr.Split(',');
                        foreach (var timeNo in timeSplit)
                        {
                            if (!string.IsNullOrWhiteSpace(timeNo))
                            {
                                string startTime = Request.Form["txtStartTime" + typeNo + "_" + timeNo];
                                string endTime = Request.Form["txtEndTime" + typeNo + "_" + timeNo];
                                string price = Request.Form["txtPrice" + typeNo + "_" + timeNo];


                                T_DT_MainEnergyPrice_Phase_TimePrice phase_TimePrice = new T_DT_MainEnergyPrice_Phase_TimePrice();
                                TimeSpan timeStart;
                                if (TimeSpan.TryParse(startTime, out timeStart))
                                    phase_TimePrice.StartTime = timeStart;
                                TimeSpan timeEnd;
                                if (TimeSpan.TryParse(endTime, out timeEnd))
                                    phase_TimePrice.EndTime = timeEnd;
                                phase_TimePrice.Price = Convert.ToDouble(price);
                                price_Phase_TimePriceList.Add(phase_TimePrice);
                            }
                        }
                        //添加电费配置季节、日期、时段、价格数据
                        bool result = new BizLogic.EnergyPrice.MainEnergyPriceService().AddEnergyPriceList(priceId, phase, price_Phase_DateList, price_Phase_TimePriceList);
                        if (!result)
                        {
                            //谈框提示
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "<script>alert('保存失败!');</script>", false);
                            return;
                        }
                    }
                }
            }
        }
        #endregion

代码写的比较粗,大概就是这样的实现方式。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值