hiii..am正在做一个调查项目。我有一个文本框和两个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");
}
}
aspx代码:
<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>
答案 0 :(得分:3)
设置:Session["SurveyName"] = "A survey name";
获取:txtSurveyName.Text = Session["SurveyName"].ToString();
答案 1 :(得分:3)
在.cs文件中添加此代码,它会将您的文本框值存储在会话中 并且对于您的信息会话存储任何类型的值,因为它是'object'类型
Session["SurveyName"] = TextBox.text;
希望这会对你有所帮助!!!!
答案 2 :(得分:1)
我想将调查名称用作会话
如果您想使用密钥作为调查名称创建会话,那么您可以执行此操作
string surveyName = txtsurvey.Text;
Session["SurveyName"] = surveyName; // or whatever value, you want to assign in session object.
答案 3 :(得分:0)
如果您想更熟悉Session对象,那么您的第一步应该是阅读http://msdn.microsoft.com/en-us/library/ms524319.aspx上的MSDN文档。
要在代码中使用会话,只需分配给它,即:
con.Close();
Session["SurveyName"] = txtSurveyName.Text;
Response.Redirect("Questions.aspx");
然后您可以在以后的页面中使用Session [“SurveyName”]。
答案 4 :(得分:0)
您可以通过以下方式将调查名称存储在会话名称中: -
Session["SurveyName"] = txtSurveyName.Text;
当您想使用调查名称时,您可以这样: -
string StrSurveyName = Session["SurveyName"].ToString();
希望这会帮助你