DataList

lass_name 2007-07-04 12:41:58
如何实现三个表中:试卷表,问题表,问题选项表
这三个表都有关系,
如何实现从数据库中将这三个表中的相对应的字段显示,
最后显示结果为“类似一套试卷”
有标题
问题
问题对应的选项。
...全文
166 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lass_name 2007-07-05
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
string strSql = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetBinder(16);
}
}

private void GetBinder(int researchId)
{
string url = "server=192.168.1.125;database=Skandia;uid=sa;pwd=sa";
SqlConnection con = new SqlConnection(url);
string sql = "select * from researchQuestions where researchId=" + researchId;
//SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
//DataSet ds = new DataSet();
//adapter.Fill(ds);
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
this.dlMyList.DataKeyField = "questionId";
this.dlMyList.DataSource = sdr;
this.dlMyList.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
//DataSet ds1 = new DataSet();
//for(int i=0;i<dlMyList.Items.Count;i++)
//{
// string sql1 = "select * from researchOptions where questionId="+ds.Tables[0].Rows[i]["questionId"].ToString();
// SqlDataAdapter ada = new SqlDataAdapter(sql1, con);
// ada.Fill(ds1);
// DataList myList = (DataList)dlMyList.Items[i].FindControl("MyList");
// myList.DataSource = ds1.Tables[0];
// myList.DataBind();
//}
}
protected void dlMyList_ItemDataBound(object sender, DataListItemEventArgs e)
{
string url = "server=192.168.1.125;database=Skandia;uid=sa;pwd=sa";
SqlConnection con = new SqlConnection(url);
DataSet ds = new DataSet();
string sql = "select * from researchOptions where questionId =" + dlMyList.DataKeys[e.Item.ItemIndex].ToString();
SqlCommand cmd = new SqlCommand(sql, con);

try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();

HiddenField hiddenField = new HiddenField();
hiddenField = (HiddenField)e.Item.FindControl("hfQuestionType");
switch (Convert.ToInt32(hiddenField.Value))
{
case 0:
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.DataTextField = "optionName";
radioButtonList.DataSource = sdr;
radioButtonList.DataBind();
e.Item.Controls.Add(radioButtonList);
break;
case 1:
CheckBoxList checkBoxList = new CheckBoxList();
checkBoxList.DataTextField = "optionName";
checkBoxList.DataSource = sdr;
checkBoxList.DataBind();
e.Item.Controls.Add(checkBoxList);
break;
default:
TextBox textBox = new TextBox();
textBox.TextMode = TextBoxMode.MultiLine;
e.Item.Controls.Add(textBox);
break;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}



}
protected void Button1_Click(object sender, EventArgs e)
{
//RadioButtonList radionButtonList = new RadioButtonList();
//radionButtonList = (RadioButtonList)this.dlMyList.Items[0].FindControl("radionButtonList");

//string str = radionButtonList.SelectedItem.Text;
Response.Write(strSql);
}
}
lass_name 2007-07-05
  • 打赏
  • 举报
回复
动态添加啊!

如:
count++;
HiddenField hiddenField = new HiddenField();
hiddenField = (HiddenField)e.Item.FindControl("hfQuestionType");
Label label = new Label();
label = (Label)e.Item.FindControl("lblTitleId");
label.Text = count.ToString();
if (e.Item.ItemType == ListItemType.Item)
{
switch (Convert.ToInt32(hiddenField.Value))
{
case 0:
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.DataSource=GetResearchOptionName(Convert.ToInt32(dlQuestions.DataKeys[e.Item.ItemIndex]));
radioButtonList.DataTextField = "optionName";
radioButtonList.DataValueField = "optionId";
radioButtonList.DataBind();
e.Item.Controls.Add(radioButtonList);
break;
case 1:
CheckBoxList checkBoxList=new CheckBoxList();
checkBoxList.DataSource=GetResearchOptionName(Convert.ToInt32(dlQuestions.DataKeys[e.Item.ItemIndex]));
checkBoxList.DataTextField = "optionName";
checkBoxList.DataValueField = "optionId";
checkBoxList.DataBind();
e.Item.Controls.Add(checkBoxList);
break;

default:
TextBox textBox = new TextBox();
textBox.TextMode = TextBoxMode.MultiLine;
e.Item.Controls.Add(textBox);
break;
}

}

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧