2011-01-11 72 views
0

我试图创建看起来像基于与输出数据库值的动态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(); 
    } 
} 

工作代码如果我在正确的轨道上,请在哪里何去何从一些指导。如果我走错了方向,请指导一些正确的道路。

回答

3

下面是一些指导:

  • 我会从数据库中装载的数据,并将其绑定到一个GUI控制之间分开。例如,首先将它加载到一个集合中,然后将该集合绑定到一个控件。
  • “元素的数量将在运行时确定” - 看起来您可以使用Repeater来绑定数据。确保你在中继器中创建了li元素。

比方说,你创建一个方法GetData返回一个Dictionary<int, string>,然后你可以将数据绑定到Repeater

Repeater

<asp:Repeater ID="rep" runat="server"> 
    <ItemTemplate> 
     <li><a runat="server" id="myLink" /></li> 
    </ItemTemplate> 
</asp:Repeater> 

将数据绑定到Repeater像这样的东西:

private void BindData(Dictionary<int, string> dict){ 
     this.rep.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound); 
     this.rep.DataSource = dict; 
     this.rep.DataBind(); 
    } 

    void rep_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      KeyValuePair<int, string> kp = e.Item.DataItem as KeyValuePair<int, string>; 
      //... find the control and update it with the correct values 
     } 
    } 

我将让你真正找到控制和正确的更新文字和网址。

希望这有助于某种方式。

+0

中继器的代码是完美的这样的事情。 – NotMe 2011-01-11 23:48:26

0

下面是我从marapet服用建议后使用的确切代码:

<div class="filterlist"> 
    <ul id="list"> 
     <asp:Repeater ID="MyList" runat="server"> 
       <ItemTemplate> 
        <li runat="server"> 
        <asp:HyperLink ID="MyLink" runat="server" Text="" NavigateUrl=""/> 
        </li> 
       </ItemTemplate> 
     </asp:Repeater> 
    </ul> 
</div> 

这背后

protected void Page_Load(object sender, EventArgs e) 
{ 
//This method return a dictionary with a URL, and a name 
    if (!Page.IsPostBack) 
     this.BindDate((Dictionary<string, string>)MyLibrary.Data.DataHelper.GetMytList()); 
} 

private void BindDate(Dictionary<string, string> dict) 
{ 
    this.RestList.ItemDataBound += new RepeaterItemEventHandler(e_ItemDataBound); 
    this.RestList.DataSource = dict; 
    this.RestList.DataBind(); 
} 

void e_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) 
    { 
     KeyValuePair<string, string> mydict = (KeyValuePair<string, string>)e.Item.DataItem; 
     HyperLink Link = (HyperLink)e.Item.FindControl("MyLink"); 
     Link.NavigateUrl = mydict.Value.ToString(); 
     Link.Text = mydict.Key.ToString(); 
    } 
}