访问datalist iems获得异常 [英] accessing datalist iems getting exception

查看:110
本文介绍了访问datalist iems获得异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据列表中有一个radiobutton列表和一个复选框列表。我已使用相同的数据源对这些列表进行了分类(以显示问题的选项)。在这种情况下,基于问题类型的问题可以看到一个列表,现在我想将用户选择的选项存储到数据库中。我使用以下代码



protected void Submitsurvey_Click(object sender,EventArgs e)

{

foreach(DataList1.Items中的DataListItem项)

{

if((item.ItemType == ListItemType.Item)

||( item.ItemType == ListItemType.AlternatingItem)))



{

string type =;

标签lb =(标签)item.FindControl(AnswertypeLabel);

RadioButtonList rbl =(RadioButtonList)item.FindControl(RadioButtonList1);

CheckBoxList cbl =( CheckBoxList)item.FindControl(CheckBoxList1);

TextBox txt =(TextBox)item.FindControl(TextBox1);

type = lb.Text;



SaveAnswer(2,rbl.SelectedItem.Value.ToString());



}





}

}

private void SaveAnswer(int qid, string ct)

{

SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString]。ToString());

SqlCommand cmd = new SqlCommand(insert into Surveyanswers(QuestionID,Choicetext)values(@ qid,@ ct),cnn);

SqlParameter p1 = new SqlParameter(@ qid,qid);

SqlParameter p2 = new SqlParameter(ct,ct);

cmd.Parameters.Add(p1);

cmd.Parameters.Add( p2);



cnn.Open();

cmd.ExecuteNonQuery();

cnn.Close ();

}

但我得到的对象引用没有设置为此行的对象错误实例

SaveAnswer(2,rbl .SelectedItem.Value.ToString());



同样如此我使用checkboxlist.selecteditem.value

我想我无法正确访问datalist模板项?我如何能够解决这个错误plz帮助并提前指导我!!

解决方案

为什么不尝试使用OnItemCommand事件的数据列表....

将CommandName赋予RadioButtonList和CheckBoxList

和OnItemCommand事件获取选定值

i have one radiobutton list and one one check box list in a data list . i have binned these lists with same data source(to show options for a question) . Here in this case one list will be visible to a question based on type of question now i want to store option selected by the user into the databas. I am using the following code
:
protected void Submitsurvey_Click(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
if (((item.ItemType == ListItemType.Item)
|| (item.ItemType == ListItemType.AlternatingItem)))

{
string type = "";
Label lb = (Label)item.FindControl("AnswertypeLabel");
RadioButtonList rbl = (RadioButtonList)item.FindControl("RadioButtonList1");
CheckBoxList cbl = (CheckBoxList)item.FindControl("CheckBoxList1");
TextBox txt = (TextBox)item.FindControl("TextBox1");
type = lb.Text;

SaveAnswer(2, rbl.SelectedItem.Value.ToString());

}


}
}
private void SaveAnswer(int qid, string ct)
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("insert into Surveyanswers(QuestionID,Choicetext) values(@qid,@ct)", cnn);
SqlParameter p1 = new SqlParameter("@qid", qid);
SqlParameter p2 = new SqlParameter("ct",ct);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);

cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
but i get object refernce not set to an instance of an object error at this line
SaveAnswer(2, rbl.SelectedItem.Value.ToString());

same is the case when i use checkboxlist.selecteditem.value
i think i am not getting proper access to datalist template items ? How I can solve this error plz help and guide me Thnks in advance!!

解决方案

Why don't u try OnItemCommand event of datalist....
give CommandName to RadioButtonList and CheckBoxList
and on OnItemCommand event get selected value


这篇关于访问datalist iems获得异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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