实验名称:增加问卷调查
步骤:
打开数据库,在数据库yezi里面,新建两张表,如下;
利用表格布局或层布局制作default.aspx页面,问卷调查效果图如下;
使用验证控件RequiredFiledValidator检查问卷名称输入框中是否输入信息,TextBox1是问卷名称输入框的ID,代码如下图所示;
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="请输入!"></asp:RequiredFieldValidator>
使用验证控件CompareValidator验证验证日期格式是否正确,TextBox2是问卷日期输入框中的ID,代码如下;
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="日期格式错误" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
使用验证控件CompareValidator验证题目顺序是否是整数,TextBox4是题目顺序输入框的ID,代码如下;
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="TextBox6" ErrorMessage="顺序必须为整数!" Operator="DataTypeCheck"Type="Integer"></asp:CompareValidator>
编写代码:
在default.aspx页面上双击“增加问卷”按钮Buttion1,然后在default.aspx.cs页面的Buttion1_Click()方法中编写代码,代码如下;
{
//使用ConfigurationManager类获取连接数据库的字串
string sqla = ConfigurationManager.ConnectionStrings["siteConn"].ConnectionString;
SqlConnection conn = new SqlConnection(sqla);
//打开数据库
conn.Open();
string sqlstr = @"insert into [survey_user] (name,visible,date)values('" + TextBox1.Text + "','" + Convert.ToByte(CheckBox1.Checked) + "','" + TextBox2.Text + "')";
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = conn;
sqlcmd.CommandText = sqlstr;
sqlcmd.ExecuteNonQuery();
string sqlb= "select * from survey_user order by surveyid desc";
SqlCommand sqlcmd1 = new SqlCommand(sqlb, conn);
int sid = Convert.ToInt32(sqlcmd1.ExecuteScalar());
string sqlc = @"insert into [surveyitem_user] (surveyid,title,selectA,selectB,selectC,selectD,permitDesc,displayOrder) values (" + sid + ",'" + TextBox3.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "',"+ Convert.ToByte(CheckBox2.Checked) +",'" + TextBox4.Text + "')";
SqlCommand sqlcmd2 = new SqlCommand(sqlc, conn);
sqlcmd2.ExecuteNonQuery();
Page.RegisterClientScriptBlock("alert", "<script>alert('添加调查问卷成功!')</script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
TextBox8.Text = "";
conn.Close();
}
}
运行网站
运行网站成功好后,增加问卷调查,然后单击“增加问卷”按钮,成功的把数据插入数据库后,弹出“添加调查问卷成功”的提示信息的窗口;
转载于:https://blog.51cto.com/yueyuanyuan/323913