如何在asp.net中使用session [英] How to Use session in asp.net
本文介绍了如何在asp.net中使用session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hiii..am做一个调查项目。我有一个文本框和两个阿贾克斯日历扩展。我想使用调查的名称会话。怎么做??我不熟悉的会话。谁能帮助我,请???
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
串STRCON;
STRCON = ConfigurationManager.ConnectionStrings [SurveyConnectionString]的ConnectionString。
SqlConnection的CON =新的SqlConnection(STRCON);
的SqlCommand COM2 =新的SqlCommand(STRCON);
com2.Connection = CON;
con.Open();
com2.CommandText =插入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(插入成功);
con.Close();
的Response.Redirect(Questions.aspx);
}
}
ASPX code:
< ASP:面板ID =Panel1的=服务器>
< ASP:标签ID =Label1的=服务器>< / ASP:标签>
< ASP:标签ID =Label2的=服务器文本=调查名称>< / ASP:标签>
< ASP:标签ID =Label4=服务器文本=截止日期>< / ASP:标签>
< ASP:文本框ID =txtSurveyName=服务器>< / ASP:文本框>
< ASP:文本框ID =txtCreaDate=服务器>< / ASP:文本框> < ASP:CalendarExtender ID =txtCreaDate_CalendarExtender=服务器
启用=真的TargetControlID =txtCreaDate>
< / ASP:CalendarExtender> < ASP:文本框ID =txtExDate=服务器>< / ASP:文本框> < ASP:CalendarExtender ID =txtExDate_CalendarExtender=服务器
启用=真的TargetControlID =txtExDate>
< / ASP:CalendarExtender> < ASP:按钮的ID =Button1的=服务器文本=保存的onclick =的button1_Click/> < ASP:的RequiredFieldValidator ID =reqFieldSurvey=服务器
的ControlToValidate =txtSurveyName的ErrorMessage =*输入姓名调查
风格=的z-index:1;左:458px;顶:99px;位置:绝对;底部:286px>< / ASP:&的RequiredFieldValidator GT;
< ASP:的RequiredFieldValidator ID =reqCreDate=服务器
的ControlToValidate =txtCreaDate的ErrorMessage =*选择日期> < / ASP:&的RequiredFieldValidator GT;
< ASP:的RequiredFieldValidator ID =reqExpDate=服务器
的ControlToValidate =txtExDate的ErrorMessage =*选择日期>< / ASP:&的RequiredFieldValidator GT;< / ASP:面板>
解决方案
我想使用调查的名称作为会议
块引用>如果您想与密钥调查名称创建一个会话,然后你做这个
字符串surveyName = txtsurvey.Text;
会话[SurveyName] = surveyName; //或任何值,要在会话对象分配。hiii..am doing a survey project. i have a text box and two ajax calendar extender. i want to use Survey name as session. How to do that?? am not familiar with session. can anyone help me please???
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 code:
<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>
解决方案i want to use Survey name as session
If you want to create a session with key as Survey Name then you do this
string surveyName = txtsurvey.Text; Session["SurveyName"] = surveyName; // or whatever value, you want to assign in session object.
这篇关于如何在asp.net中使用session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文