我试图创建看起来像基于与输出数据库值的动态li元素:构建动态利用数据绑定
<li id="myUniqueLiID" runat="server"><a href="vaiable url from db">variable string from db</a></li>
li元素的量将在运行时间确定。我想和li元素一起去,因为我认为这会让我在以后的操作中留下最多的选择,因为它反对我已经使用过的几个asp项目,即asp:ListBox。
这里是我与迄今
SqlDatabase myconnection= new SqlDatabase(@"myconnection string");
DbCommand myproc= myconnection.GetStoredProcCommand("sp_MySP");
using (IDataReader LoadAllItems = myconnection.ExecuteReader(myproc))
{
while (LoadAllItems.Read())
{
// retrieves ID from db
int myID = LoadAllItems.GetInt32(0);
// retrieves string from db
string myName = LoadAllItems.GetString(1);
// I have a static method that builds url based off id
// it takes an int and returns a string
string restURL = MyLibrary.MyClass.StaticURLMethod(myID);
//data bind to li element
myLiID.datasource = LoadAllItems;
//I think I build the li in datatextfield area but not
//sure if that is correct, or how to format.
myLiID.datatextfield = "";
myLiID.databind();
}
}
工作代码如果我在正确的轨道上,请在哪里何去何从一些指导。如果我走错了方向,请指导一些正确的道路。
中继器的代码是完美的这样的事情。 – NotMe 2011-01-11 23:48:26