日期仅在1个文本框中显示

时间:2015-09-28 14:01:07

标签: c# mysql asp.net

当我从日历中选择日期时,另一个文本框中的日期将消失。 我不知道这个问题的原因是我第一次看到这样的问题。 我试图删除日历,但我没有成功解决问题。 感谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;


namespace WebApplication1
{
    public partial class comDisp : System.Web.UI.Page
    {
        string connectionstring = @"Data Source=localhost; Database=globaldotdb; user ID=root; Password=peleg1708";
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            using (MySqlConnection cn = new MySqlConnection(connectionstring))
            {
                MySqlDataAdapter adp = new MySqlDataAdapter(("SELECT  tblcommitment.codeCom,tblcustom.Customer, tblvendor.Vendor, tblcommitment.dateStart, tblcommitment.dateEnd, tblregion.Region, tblservice.Service, tblservice.unit, tblcommitment.commitment,tblcommitment.cost, tblcommitment.overage, tblcommitment.currency FROM     ((((tblvendor INNER JOIN tblcommitment ON tblvendor.codeVendor = tblcommitment.codeVendor) INNER JOIN  tblservice ON tblcommitment.codeService = tblservice.codeService) INNER JOIN  tblregion ON tblcommitment.codeRegion = tblregion.codeRegion) INNER JOIN  tblcustom ON tblcommitment.codeCust = tblcustom.codeCust)"), cn);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    gvcom.DataSource = dt;
                    gvcom.DataBind();



                }
            }
        }

        protected void gvcom_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int codecom = int.Parse(gvcom.DataKeys[e.RowIndex].Value.ToString());

            deleteusage(codecom);
            BindData();
        }

        private void deleteusage(int codecom)
        {
            using (MySqlConnection cn = new MySqlConnection(connectionstring))
            {
                string query = "DELETE FROM tblcommitment WHERE codeCom=" + codecom + " ";
                MySqlCommand cmd = new MySqlCommand(query, cn);
                cn.Open();
                cmd.ExecuteNonQuery();

            }
        }

        protected void gvcom_DataBound(object sender, EventArgs e)
        {

            DropDownList DDLCu = gvcom.FooterRow.FindControl("DDLCu") as DropDownList;
            DropDownList DDLVe = gvcom.FooterRow.FindControl("DDLVe") as DropDownList;
            DropDownList DDLSe = gvcom.FooterRow.FindControl("DDLSe") as DropDownList;
            DropDownList DDLRe = gvcom.FooterRow.FindControl("DDLRe") as DropDownList;


            using (MySqlConnection cn = new MySqlConnection(connectionstring))
            {
                MySqlDataAdapter Cadp = new MySqlDataAdapter(("SELECT * from tblcustom"), cn);
                DataTable Cdt = new DataTable();
                Cadp.Fill(Cdt);
                if (Cdt.Rows.Count > 0)
                {
                    DDLCu.DataSource = Cdt;
                    DDLCu.DataTextField = "Customer";
                    DDLCu.DataValueField = "codeCust";
                    DDLCu.DataBind();

                }


                MySqlDataAdapter Vadp = new MySqlDataAdapter(("SELECT * from tblvendor"), cn);
                DataTable Vdt = new DataTable();
                Vadp.Fill(Vdt);
                if (Vdt.Rows.Count > 0)
                {
                    DDLVe.DataSource = Vdt;
                    DDLVe.DataTextField = "Vendor";
                    DDLVe.DataValueField = "codeVendor";
                    DDLVe.DataBind();

                }


                MySqlDataAdapter Sadp = new MySqlDataAdapter(("SELECT * from tblservice"), cn);
                DataTable Sdt = new DataTable();
                Sadp.Fill(Sdt);
                if (Sdt.Rows.Count > 0)
                {
                    DDLSe.DataSource = Sdt;
                    DDLSe.DataTextField = "Service";
                    DDLSe.DataValueField = "codeService";
                    DDLSe.DataBind();

                }


                MySqlDataAdapter Radp = new MySqlDataAdapter(("SELECT * from tblregion"), cn);
                DataTable Rdt = new DataTable();
                Radp.Fill(Rdt);
                if (Rdt.Rows.Count > 0)
                {
                    DDLRe.DataSource = Rdt;
                    DDLRe.DataTextField = "Region";
                    DDLRe.DataValueField = "codeRegion";
                    DDLRe.DataBind();

                }
            }
        }





        private void add(int cc, int cv, int cs, int cr, int com, double costs,double ov,string cur, string ds, string de)
        {

            using (MySqlConnection cn = new MySqlConnection(connectionstring))
            {

                string query = "insert into tblusage(codeCust,codeVendor,codeService,codeRegion,dateStart,dateEnd,commitment,overage,cost,currency) values (@cc,@cv,@cs,@cr,@ds,@de,@com,@ov,@costs,@cur) ";
                MySqlCommand cmd = new MySqlCommand(query, cn);

                cmd.Parameters.Add("@cc", MySqlDbType.Int32).Value = cc;
                cmd.Parameters.Add("@cv", MySqlDbType.Int32).Value = cv;
                cmd.Parameters.Add("@cs", MySqlDbType.Int32).Value = cs;
                cmd.Parameters.Add("@cr", MySqlDbType.Int32).Value = cr;              
                cmd.Parameters.Add("@ds", MySqlDbType.Date).Value = ds;
                cmd.Parameters.Add("@de", MySqlDbType.Date).Value = de;
                cmd.Parameters.Add("@com", MySqlDbType.Int32).Value = com;
                cmd.Parameters.Add("@costs", MySqlDbType.Double).Value = costs;
                cmd.Parameters.Add("@ov", MySqlDbType.Double).Value = com;
                cmd.Parameters.Add("@cur", MySqlDbType.String).Value = cur;


                cn.Open();
                cmd.ExecuteNonQuery();

            }
        }

        protected void Lnkadd_Click(object sender, EventArgs e)
        {
            DropDownList DDLCu = gvcom.FooterRow.FindControl("DDLCu") as DropDownList;
            DropDownList DDLVe = gvcom.FooterRow.FindControl("DDLVe") as DropDownList;
            DropDownList DDLSe = gvcom.FooterRow.FindControl("DDLSe") as DropDownList;
            DropDownList DDLRe = gvcom.FooterRow.FindControl("DDLRe") as DropDownList;
            DropDownList DDLIS = gvcom.FooterRow.FindControl("DDLIS") as DropDownList;
            TextBox txtds = (TextBox)gvcom.FooterRow.FindControl("TBDS");
            TextBox txtde = (TextBox)gvcom.FooterRow.FindControl("TBDE");

            TextBox txtcom = (TextBox)gvcom.FooterRow.FindControl("TxtCom");
            TextBox txtcost = (TextBox)gvcom.FooterRow.FindControl("TxtCost");
            TextBox txtov = (TextBox)gvcom.FooterRow.FindControl("TxtOv");
            TextBox txtcur = (TextBox)gvcom.FooterRow.FindControl("TxtCur");


            int cc = int.Parse(DDLCu.SelectedValue);
            int cv = int.Parse(DDLVe.SelectedValue);
            int cs = int.Parse(DDLSe.SelectedValue);
            int cr = int.Parse(DDLRe.SelectedValue);
            int com = int.Parse(txtcom.Text);
            double cost = double.Parse(txtcost.Text);
            double over = double.Parse(txtov.Text);
            string DS = txtds.Text;
            string DE = txtde.Text;
            string Cur = txtcur.Text;

            string sDate = Convert.ToDateTime(txtds.Text).ToString("yyyy-MM-dd");

            string eDate = Convert.ToDateTime(txtde.Text).ToString("yyyy-MM-dd");



            add(cc, cv, cs, cr, com, cost, over, Cur,sDate,eDate);
            BindData();
            Response.Redirect("http://localhost:56717/comDisp.aspx");
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            TextBox txtde = (TextBox)gvcom.FooterRow.FindControl("TextBox1");
            Calendar cae = gvcom.FooterRow.FindControl("Calendar1") as Calendar;
            txtde.Text = cae.SelectedDate.ToString("d");
        }

        protected void CalendarS_SelectionChanged(object sender, EventArgs e)
        {
            TextBox txtds = (TextBox)gvcom.FooterRow.FindControl("TxtS");
            Calendar cae = gvcom.FooterRow.FindControl("CalendarS") as Calendar;
            txtds.Text = cae.SelectedDate.ToString("d");
        }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="comDisp.aspx.cs" Inherits="WebApplication1.comDisp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="gvcom" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="codeCom" ondatabound="gvcom_DataBound" 
            onrowdeleting="gvcom_RowDeleting" ShowFooter="True">
            <Columns>
                <asp:TemplateField HeaderText="codecom" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("codeCom") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer">
                    <FooterTemplate>
                        <asp:DropDownList ID="DDLCu" runat="server" AutoPostBack="True">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Customer") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Vendor">
                    <FooterTemplate>
                        <asp:DropDownList ID="DDLVe" runat="server" AutoPostBack="True">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Vendor") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="dateStart">
                    <FooterTemplate>
                        <asp:Calendar ID="CalendarS" runat="server" BackColor="White" 
                            BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
                            DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
                            ForeColor="#003399" Height="118px" 
                            onselectionchanged="CalendarS_SelectionChanged" Width="158px">
                            <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
                            <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
                            <OtherMonthDayStyle ForeColor="#999999" />
                            <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                            <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
                            <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" 
                                Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
                            <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
                            <WeekendDayStyle BackColor="#CCCCFF" />
                        </asp:Calendar>
                        <asp:TextBox ID="TxtS" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("dateStart") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="dateEnd">
                    <FooterTemplate>
                        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
                            BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" 
                            Font-Size="9pt" ForeColor="Black" Height="119px" NextPrevFormat="ShortMonth" 
                            onselectionchanged="Calendar1_SelectionChanged" Width="167px">
                            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" 
                                Height="8pt" />
                            <DayStyle BackColor="#CCCCCC" />
                            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
                            <OtherMonthDayStyle ForeColor="#999999" />
                            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
                            <TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" 
                                Font-Size="12pt" ForeColor="White" Height="12pt" />
                            <TodayDayStyle BackColor="#999999" ForeColor="White" />
                        </asp:Calendar>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("dateEnd") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="region">
                    <FooterTemplate>
                        <asp:DropDownList ID="DDLRe" runat="server" AutoPostBack="True">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Region") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="service">
                    <FooterTemplate>
                        <asp:DropDownList ID="DDLSe" runat="server" AutoPostBack="True">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Service") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="unit">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("unit") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="commitment">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("commitment") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtCom" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="commitment_cost">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("cost") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtCost" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="overage_cost">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("overage") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtOv" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="currency">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("currency") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtCur" runat="server"></asp:TextBox>
                        <asp:LinkButton ID="Lnkadd" runat="server" onclick="Lnkadd_Click">add</asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:CommandField HeaderText="Operation" ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您正在绑定每个回发上的数据,您应该检查IsPostBack属性以避免这种情况,并仅在页面加载时首次绑定数据: -

protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
          BindData();
}