回发后ViewState变为null

时间:2016-10-06 21:36:18

标签: c# html asp.net visual-studio

我试图让JavaScript Calendar Pikaday在我正在开发的网络应用程序中运行。我想从文本框传递一个值(当点击它时,显示一个JS日历来选择一天。然后它变成一个变量发送到sql并拉出一个存储过程)我必须格式化文本才能通过数据库发送,因为ASP中sqldatasource的控件绑定到文本框。无论出于何种原因,即使发送了正确的日期,Pikaday也会在文本框中显示前一天,并查询数据库以更新我的gridview。

我试图将变量存储在ViewState和Pageload上,更改文本框的值。即使在页面和文本框上使用EnableViewState,ViewState也始终为null。我究竟做错了什么? ViewState适用于click事件,但由于Textbox是控件,我无法做到。

 public partial class WebForm3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Used to change the color of the button "Export to excel"
        string hex = "#5CB85C";
        Button2.BackColor = System.Drawing.ColorTranslator.FromHtml(hex);
        Button2.ForeColor = System.Drawing.Color.White;
        if (Page.IsPostBack)
        {
            if (dateFull != null)
            {
                Button1.Text = dateFull;
            }
            if (DropDownList2.SelectedIndex > 0)
            {
                //changes the GridView by the program filter
                changeByProgram();
            }
            dateFull = null;


        }
    }
    public string dateFull
    {
        get
        {
            string text = (string)ViewState["date"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }
        set
        {
            ViewState["date"] = value;
        }
    }
    private void changeByDate()
    {
        if (TextBox2.Text.Equals(""))
        {
            GridView1.DataSourceID = "SqlDataSource1";
            dateFull = null;
        }
        if (TextBox2.Text != "")
        {
            String date = TextBox2.Text;
            dateFull = date;
            string dayOfWeek = date.Substring(0, 3);
            date = date.Substring(3);
            string month = date.Substring(1, 3);
            string day = date.Substring(5, 2);
            string year = date.Substring(8, 4);
            switch (month)
            {
                case "Jan":
                    month = "01";
                    break;
                case "Feb":
                    month = "02";
                    break;
                case "Mar":
                    month = "03";
                    break;
                case "Apr":
                    month = "04";
                    break;
                case "May":
                    month = "05";
                    break;
                case "Jun":
                    month = "06";
                    break;
                case "Jul":
                    month = "07";
                    break;
                case "Aug":
                    month = "08";
                    break;
                case "Sep":
                    month = "09";
                    break;
                case "Oct":
                    month = "10";
                    break;
                case "Nov":
                    month = "11";
                    break;
                case "Dec":
                    month = "12";
                    break;
                default:
                    break;

            }
            date = year + "-" + month + "-" + day;
            TextBox2.Text = date;
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestAdamConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand("datePullScrap", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Date", date);
            conn.Open();
            GridView1.DataSourceID = "SqlDataSource4";
            conn.Close();
        }
    }
     protected void Button1_Click(object sender, EventArgs e)
    {
        changeByDate();
        //Button1.Text = (string)ViewState["date"];
    }
     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RawScrapData.aspx.cs" Inherits="WebApplication4.WebForm3" EnableEventValidation="true" EnableViewState="True" ViewStateMode="Enabled" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
     <link href="~/Content/StyleSheet1.css" rel="stylesheet" type="text/css"     media="screen" runat="server" />
     <script src="../pikaday.js"></script>
     <link href="../css/triangle.css" rel="stylesheet" />
     <link href="../css/theme.css" rel="stylesheet" />
     <link href="../css/pikaday.css" rel="stylesheet" />
     <title></title>
 </head>
 <body>
    <form id="form1" runat="server">
         <div class="auto-style1">
         <asp:Label ID="Label1" runat="server" Font-Size="Medium" Text="Datepicker:" Font-Names="Consolas" ></asp:Label>  &nbsp;&nbsp;
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
                    <br />
                    <asp:TextBox ID="TextBox2" runat="server" ViewStateMode="Enabled" EnableViewState="True" AutoPostBack="True"></asp:TextBox>
                    <script> type = "text/javascript"
                        var picker = new Pikaday(
                            {

                                field: document.getElementById('TextBox2'),
                                format: 'YYYY/MM/DD',
                                firstDay: 0,
                                minDate: new Date('1990-01-01'),
                                maxDate: new Date('2020-12-31'),
                                yearRange: [1990, 2020],
                                numberOfMonths: 1,
                                theme: 'dark-theme'
                            });

                    </script>
             <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:TestAdamConnectionString %>" SelectCommand="datePullScrap" SelectCommandType="StoredProcedure">
                  <SelectParameters>
                    <asp:ControlParameter ControlID="TextBox2" Name="Date" PropertyName="Text" Type="String" />
                  </SelectParameters>
            </asp:SqlDataSource>
            </div>
    </form>
</body>
</html>

0 个答案:

没有答案