做了一天,终于把添加这部分做完了,终于有了一点点的慰藉了~
添加页面如下:

顶部使用的radiobuttonlist控件与multiview控件相结合,设置radiobuttonlist的item的value属性即可动态选择展现要添加的问卷类型,代码如下:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected)
{
MultiView1.ActiveViewIndex = Convert.ToInt32(RadioButtonList1.Items[i].Value);
}
}
}
关于添加,模式是一样的,这里只展现如何添加单选类型的————在数据库里,使用了两张表,一个是poll表,专门用来记录调查的内容,还有一张option表,专门用来存放选项的内容,所以,在向数据库添加的时候,要先后或同时向两张表去添加内容,比较复杂,而且二者有联系,就是所添加的选项的所属pollid必须一张,这里涉及到的textbox内容去空值问题,在上一篇已经介绍过了,在此不再赘述~
protected void fbdx_Click(object sender, EventArgs e)//点击发布按钮
{
bool pollok = false;//判断poll是否插入成功
string nr = dxzt .Text;
string sx = "单选";
int all = 0;
string fqr = "cx";
double at = Convert.ToDouble(RadioButtonList3.SelectedValue);
string jssj = (DateTime.Now.AddDays(at)).ToString();
if (nr.ToString() != "")
{
if (AddPoll(nr, sx, all, fqr, jssj))//调用函数方法,插入单选主题!并判断是否成功~
{
pollok = true;
}
else
{
Response.Write("<script>alert('多选主题发布失败!')</script>");
}
if (pollok)//如果主题插入成功,则进行下一步,插入选项到option表~
{
string[] a = new string[] { dxx1.Text.Trim(), dxx2.Text.Trim(), dxx3.Text.Trim(), dxx4.Text.Trim(), dxx5.Text.Trim(), dxx6.Text.Trim() };//将6个textbox的内容全部存放到数组中
int oo = 0;
if (dxx1.Text.Trim() == "" && dxx2.Text.Trim() == "" && dxx3.Text.Trim() == "" && dxx4.Text.Trim() == "" && dxx5.Text.Trim() == "" && dxx6.Text.Trim() == "" )
{
Response.Write("<script>alert('请至少输入一个选项!')</script>");
}
else
{
for (int i = 0; i < filter2(a).Length; i++)//调用自定义过滤函数,过滤掉数组中的空值~
{
string xx = filter2(a)[i].ToString();
int pid = Getpid();//调用自定义函数,取到目前option中的ID
if (AddOption(xx, pid))//自定义函数,添加选项
{
oo++;
}
}
if (oo == filter2(a).Length)
{
Response.Write("<script>alert('发布成功!')</script>");
}
}
}
}
else
{
Response.Write("<script>alert('请输入单选主题!')</script>");
}
}
以上涉及到的自定义函数方法有~
protected int Getid()
{ //取到poll表中的id的最大值,并进行返回,以应用到添加poll中
int id;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/poll.mdb"));
OleDbCommand cmd = new OleDbCommand("select max(id) from poll", con);
try
{
con.Open();
id=Convert .ToInt32(cmd.ExecuteScalar())+1;//在原有的基础上加1以作为新的poll的id~
return id;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
con.Close();
}
}
protected bool AddPoll(string nr,string sx,int all,string fqr,string jssj)
{//添加poll方法!
bool ok = false;
int id = Getid();//取得新的id~
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/poll.mdb"));
OleDbCommand cmd = new OleDbCommand("insert into poll(id,内容,属性,总票数,发起人,结束时间)values('" + id + "','" + nr + "','" + sx + "','" + all + "','" + fqr + "','" + jssj + "')", con);
try
{
con.Open();
cmd.ExecuteNonQuery();
ok=true ;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
// ok=false ;
}
finally
{
con.Close();
}
return ok;
}
protected int Getpid()
{ //取到option表中的id的最大值,并进行返回,以应用
int id;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/poll.mdb"));
OleDbCommand cmd = new OleDbCommand("select max(pid) from option2", con);
try
{
con.Open();
id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;//在原有的基础上加1以作为新的poll的id~
return id;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
con.Close();
}
}
protected bool AddOption(string xx, int pid)
{
//添加option方法!
bool pok = false;
int id = Getid()-1;//为啥减掉1,我弄不懂原理,但是出现的结果因为多1,所以减掉
int all = 0;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/poll.mdb"));
OleDbCommand cmd = new OleDbCommand("insert into option2(pid,选项,所属poll,累计数,所占比例)values('"+pid+"','"+xx+"','" + id + "','" + all + "','" + all + "')", con);
try
{
con.Open();
cmd.ExecuteNonQuery();
pok = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
// ok=false ;
}
finally
{
con.Close();
}
return pok;
}
protected string[] filter2(string[] a)//使用字符串的操作过滤空值!
{
string al = "";
string all = "";
for (int i = 0; i < a.Length - 1; i++)
{
if (a[i].Trim() != "")
{
al += (a[i] + "-"); //现将不存在的空值去掉,然非空值使用分隔符连接成一个字符串
}
}
if (a[a.Length - 1].ToString() != "")
{
all = al + a[a.Length - 1];//加上最后一个元素
}
else
{
all = al.Remove(al.Length - 1);//删除最后一个字符‘-’
}
string[] b = all.Split('-');
return b;
}
浙公网安备 33010602011771号