如何在datalist中使用asp.net中的radiobutton列表 [英] how to use radiobutton list in asp.net in datalist

查看:112
本文介绍了如何在datalist中使用asp.net中的radiobutton列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表格问题有字段:qid,问题和4个选择,

i希望使用datalist.so请使用这4个选项在radiobutton列表中显示。请帮助我怎么办... 。

i have table question having fields: qid,question,and 4 choices,
i want to use these 4 choices to show in radiobutton list using datalist.so please help me how can i do...

推荐答案

[点击此处查看解决方案]


你好我试试这个但它只返回一个值..下面是我的代码:

protected void DataList1_ItemDataBound(object sender,DataListItemEventArgs e)

{



if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{

RadioButtonList RadioButtonList1 =(RadioButtonList)e.Item.FindControl(RadioButtonList1);

//在这里获取questionID

int QID = Convert.ToInt32(DataBinder.Eval(e.Item) .DataItem,QID));

//将问题ID传递给您的数据库并获取问题的所有可用选项

//在此处绑定RadiobUttonList

SqlConnection con = new SqlConnection();

SqlCommand cmd = new SqlCommand();



con.ConnectionString = ConfigurationManager.ConnectionStrings [TQConnectionString]。ConnectionString;







string q =来自TestQuestion的SELECT Ans1,Ans2,Ans3,Ans4,其中QId =+ QID;



cmd =新的SqlCommand(q,con);



cmd.Connection.Open();



SqlDataAdapter da = new SqlDataAdapter(q,con);

DataSet ds = new DataSet();

da.Fill (DS); DataTable dt = ds.Tables [0];

RadioButtonList1.DataSource = dt;

RadioButtonList1.DataTextField = dt.Columns [0] .ToString();

RadioButtonList1.DataValueField = dt.Columns [0] .ToString();



RadioButtonList1.DataBind();

}



}

但我想在我的RadiobuttonList1中添加所有这些4 ans1,ans2,ans3,ans4。

所以请帮助我....
hello i try this but it return only one value ..below is my code :
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
//Get questionID here
int QID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QID"));
//pass Question ID to your DB and get all available options for the question
//Bind the RadiobUttonList here
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();

con.ConnectionString = ConfigurationManager.ConnectionStrings["TQConnectionString"].ConnectionString;



string q = "SELECT Ans1,Ans2,Ans3,Ans4 from TestQuestion where QId=" + QID;

cmd = new SqlCommand(q, con);

cmd.Connection.Open();

SqlDataAdapter da = new SqlDataAdapter(q, con);
DataSet ds = new DataSet();
da.Fill(ds); DataTable dt = ds.Tables[0];
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = dt.Columns[0].ToString();
RadioButtonList1.DataValueField = dt.Columns[0].ToString();

RadioButtonList1.DataBind();
}

}
but i want to add all these 4 ans1,ans2,ans3,ans4 in my RadiobuttonList1.
so please help me....


我已经开发了类似的应用程序,但我的表结构不同。它有answerID,questionId,answerText和isCorrect列。所以我曾经将questionId作为变量传递,并且它获取了所选问题的所有答案。这是一个更好的解决方案,因为一个问题可以有2,3或4个选项。在您的情况下,查询返回的结果集将只给您一行,并且您将数据绑定到只有单行的radiobuttonlist。因此,它总是会给你答案1.如果您确定每个问题确实有4个答案,您可以更改表格的结构或修改代码。尝试:



I have worked on a similar application but my Table structure was different. It had "answerID", "questionId", "answerText" and "isCorrect" columns. So I used to pass "questionId" as variable and it fetched all the answers for the selected question. This was a better solution as a question can have 2, 3 or 4 options. In your case the resultset returned by your query is going to give you only a single row and you are binding the data to radiobuttonlist with only single row. So it will always give you answer 1. You can change the structure of your table or modify your code if you are sure that there are exactly 4 answers for every question. Try:

string q = "SELECT Ans1,Ans2,Ans3,Ans4 from TestQuestion where QId=" + QID;
 
cmd = new SqlCommand(q, con);

cmd.Connection.Open();

SqlDataAdapter da = new SqlDataAdapter(q, con);
DataSet ds = new DataSet();
da.Fill(ds); 
DataTable dt = ds.Tables[0];

DataTable dtRecords = new DataTable();
dtRecords.Columns.Add("answer",typeof("string"));
dtRecords.Rows.Add(dt.Rows[0][0].ToString().Trim());
dtRecords.Rows.Add(dt.Rows[0][1].ToString().Trim());
dtRecords.Rows.Add(dt.Rows[0][2].ToString().Trim());
dtRecords.Rows.Add(dt.Rows[0][3].ToString().Trim());

RadioButtonList1.DataSource = dtRecords;
RadioButtonList1ataTextField = dtRecords.Columns[0].ToString();
RadioButtonList1.DataValueField = dtRecords.Columns[0].ToString();
 
RadioButtonList1.DataBind();





但这只是一种解决方法。您应该修改表结构并使其更通用。



But this is just a workaround. You should modify your table structure and make it more generic.


这篇关于如何在datalist中使用asp.net中的radiobutton列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆