1

hiii..我正在做一个调查项目。我有一个文本框和两个 ajax 日历扩展器。我想使用调查名称作为会话。怎么做??我不熟悉会话。谁能帮帮我???

protected void Button1_Click(object sender, EventArgs e)
    {
        string strcon;
        strcon = ConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand com2 = new SqlCommand(strcon);
        com2.Connection = con;
        con.Open();
        com2.CommandText = "insert into SurveyMaster1(SurveyName,DateCreated,ExpiredDate) VALUES(@SurveyName,@DateCreated,@ExpiredDate)";
        com2.Parameters.AddWithValue("@SurveyName", txtSurveyName.Text);
        com2.Parameters.AddWithValue("@DateCreated", txtCreaDate.Text);
        com2.Parameters.AddWithValue("@ExpiredDate", txtExDate.Text);
        com2.ExecuteNonQuery();
        Response.Write("Inserted Successfully");
    con.Close();
    Response.Redirect("Questions.aspx");
}
}

asp代码:

   <asp:Panel ID="Panel1" runat="server">
 <asp:Label ID="Label1" runat="server" ></asp:Label>
    <asp:Label ID="Label2" runat="server" Text="Survey Name"></asp:Label>
    <asp:Label ID="Label4" runat="server" Text="Expired Date"></asp:Label>
    <asp:TextBox ID="txtSurveyName" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtCreaDate" runat="server" ></asp:TextBox>

    <asp:CalendarExtender ID="txtCreaDate_CalendarExtender" runat="server" 
        Enabled="True" TargetControlID="txtCreaDate">
    </asp:CalendarExtender>

    <asp:TextBox ID="txtExDate" runat="server"></asp:TextBox>

    <asp:CalendarExtender ID="txtExDate_CalendarExtender" runat="server" 
        Enabled="True" TargetControlID="txtExDate">
    </asp:CalendarExtender>

    <asp:Button ID="Button1" runat="server" Text="SAVE" onclick="Button1_Click" />

    <asp:RequiredFieldValidator ID="reqFieldSurvey" runat="server" 
        ControlToValidate="txtSurveyName" ErrorMessage="* Enter Survey Name" 
        style="z-index: 1; left: 458px; top: 99px; position: absolute; bottom: 286px"></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="reqCreDate" runat="server" 
        ControlToValidate="txtCreaDate" ErrorMessage="* Select Date" >   </asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="reqExpDate" runat="server" 
        ControlToValidate="txtExDate" ErrorMessage="* Select Date"></asp:RequiredFieldValidator>

</asp:Panel>
4

5 回答 5

3

将此代码添加到您的 .cs 文件中,它将您的文本框值存储在会话中,并为您的信息会话存储任何类型的值,因为它是“对象”类型

Session["SurveyName"] = TextBox.text;

希望对你有帮助!!!!

于 2013-07-01T11:38:10.347 回答
3

放:Session["SurveyName"] = "A survey name";

得到:txtSurveyName.Text = Session["SurveyName"].ToString();

于 2013-07-01T11:32:18.407 回答
1

我想使用调查名称作为会话

如果您想创建一个密钥作为调查名称的会话,那么您可以这样做

string surveyName = txtsurvey.Text;
Session["SurveyName"] = surveyName;  // or whatever value, you want to assign in session object.
于 2013-07-01T11:31:40.473 回答
0

您可以通过以下方式将调查名称存储在会话名称中:-

Session["SurveyName"] = txtSurveyName.Text;

当您想使用调查名称时,您可以通过以下方式获得:-

string StrSurveyName = Session["SurveyName"].ToString();

希望对你有帮助

于 2013-07-01T11:37:44.373 回答
0

如果您想更熟悉 Session 对象,第一步应该是阅读 MSDN 文档,网址为http://msdn.microsoft.com/en-us/library/ms524319.aspx

要在代码中使用会话,只需分配给它,因此:

    con.Close();
    Session["SurveyName"] = txtSurveyName.Text;
    Response.Redirect("Questions.aspx");

然后,您可以在后面的页面中使用 Session["SurveyName"]。

于 2013-07-01T11:32:50.937 回答