[英]How to dynamically populate a RadioButtonList from the database within a Repeater control
This is the first time I have tried to dynamically populate a RadioButtonList from within a Repeater control. 这是我第一次尝试从Repeater控件中动态填充RadioButtonList。 It is not going very well so far.
到目前为止进展不顺利。
I am trying this code that I found here that supposedly worked for the guy who wrote it: 我正在尝试我在这里找到的这段代码,该代码应该对编写该代码的人有用:
protected void fillRepeater_onitembound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
sql = "select PurchaseTypeID,PurchaseType from PurchaseType";
object obj = null;
ds = obj.openDataset(sql);
ListItem li;
RadioButtonList rbtl = (RadioButtonList)e.Item.FindControl("radioatt");
if (rbtl != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
li = new ListItem();
li.Text = dt.Rows[i]["PurchaseType"].ToString();
li.Value = dt.Rows[i]["PurchaseTypeID"].ToString();
rbtl.Items.Add(li);
}
}
}
}
<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="fillRepeater_onitembound">
<ItemTemplate>
<table style="width:100%;border: 1px solid black;">
<tr>
<td style="width:50%;border-collapse: collapse;border: 1px solid black;">
Purchased:<asp:radiobuttonlist ID="rblPurchaseType" runat="server" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;"></asp:radiobuttonlist></td>
<td style="text-align:center;border: 1px solid black;">
</td>
</tr>
<tr>
</table>
...
...
...
</asp:Repeater>
However, I am running into bugs: 但是,我遇到了错误:
'object' does not contain a definition for 'openDataset' and no extension method 'openDataset' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference? 'object'不包含'openDataset'的定义,找不到可以接受'object'类型的第一个参数的扩展方法'openDataset'(您是否缺少using指令或程序集引用?
Any ideas what's wrong? 任何想法有什么问题吗?
UPDATE: 更新:
public DataTable LoadDataFromDatabase(string query)
{
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(connStr))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
adapter.Fill(dt);
}
catch (Exception ex)
{
//handle error if needed
}
}
return dt;
}
string myQuery = "SELECT PurchaseTypeID, PurchaseType FROM PurchaseType ORDER BY PurchaseType";
string rbtl = Repeater2.FindControl("rblPurchaseType");
rbtl.DataSource = LoadDataFromDatabase(myQuery);
rbtl.DataTextField = "PurchaseType";
rbtl.DataValueField = "PurchaseTypeID";
rbtl.DataBind();
What exactly is obj
? obj
到底是什么? Unless you cast it back to something that has an openDataset
property this is never gonna work. 除非将其
openDataset
回具有openDataset
属性的对象,否则它将永远无法工作。
What I use is a simple method that returns a DataTable
from a query and bind that to the RadioButtonList
我使用的是一种简单的方法,该方法从查询返回
DataTable
并将其绑定到RadioButtonList
public DataTable LoadDataFromDatabase(string query)
{
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(YourConnectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
adapter.Fill(dt);
}
catch (Exception ex)
{
//handle error if needed
}
}
return dt;
}
Now you can use this method to quickly and simple get your needed content from the database and bind it to whatever Control you need. 现在,您可以使用此方法快速简单地从数据库中获取所需的内容,并将其绑定到所需的任何Control。
string myQuery = "SELECT PurchaseTypeID, PurchaseType FROM PurchaseType ORDER BY PurchaseType";
rbtl.DataSource = LoadDataFromDatabase(myQuery);
rbtl.DataTextField = "PurchaseType";
rbtl.DataValueField = "PurchaseTypeID";
rbtl.DataBind();
PS rbtl
is always gonna be null
in your case since the ID of the RadioButton in the aspx is rblPurchaseType
and you use FindControl on radioatt
, make sure these are the same. PS
rbtl
在您的情况下始终为null
,因为aspx中RadioButton的ID为rblPurchaseType
并且您在radioatt
上使用FindControl,请确保它们相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.