2012-10-08 78 views
0

这是我的数据库绑定的组合框。如何将这些项目绑定到数据库?

<asp:DropDownList ID="ddlUnitType" runat="server" CssClass="fonttah" 
      DataSourceID="SqlDataSource1" DataTextField="UnitTypeName" 
      DataValueField="UnitTypeID"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SellAutomationConnectionString %>" 
      SelectCommand="SELECT [UnitTypeID], [UnitTypeName] FROM [UnitTypes]"> 
</asp:SqlDataSource> 

现在我用下面的代码替换它(使用jquery插件显示组合框具有项目头像)。

<table> 
     <tr> 
      <td width="208"> 
          <div id="languages"> 
           <ul> 
            <li>&nbsp;<img src="../../App_Themes/DefaultTheme/images/Select.png" alt="SelectItems" border="0" align="absmiddle"/>&nbsp;<span style=" font-weight:bold">... انتخاب کنید</span> 
            </li> 
            <li>&nbsp;<img src="../../App_Themes/DefaultTheme/images/home.png" alt="Home" align="absmiddle" border="0"/>&nbsp;<span style="text-align:left">واحدهای اقامتی کوتاه مرتبه</span> 
            </li> 
            <li>&nbsp;<img src="../../App_Themes/DefaultTheme/images/shopping.png" alt="shopping" align="absmiddle" border="0"/>&nbsp;<span>واحدهای تجاری</span> 
            </li> 
           </ul> 
          </div> 
      </td> 
     </tr> 
</table> 

现在我想从数据库读取数据。实际上使用这个代码而不是该组合框。我如何将它绑定到数据库?
谢谢。

+0

你想要什么做的部分从数据库中来吗?通过什么你替换选择(DropDownList)?给出正确的答案,你的问题还不是很清楚。另外,删除'asp-classic'标签。你正在使用'asp.net' – nunespascal

+0

可能寻找一个控制 – codingbiz

+0

@nunespascal ok.I删除该标签.Dropdown的项目来自DB。 现在我使用下拉列表设计与表和jQuery。 现在我想给这个项目从DB.but怎么样? – Hamid

回答

0

asp.net有很多databound controls,许多可以让你做一些自定义格式来显示你的数据。

既然您有要显示的数据列表,请考虑使用ListView

<asp:ListView runat="server" ID="ListView1" 
    DataSourceID="SqlDataSource1"> 
    <LayoutTemplate> 
    <table runat="server" id="table1" > 
     <tr runat="server" id="itemPlaceholder" ></tr> 
    </table> 
    </LayoutTemplate> 
    <ItemTemplate> 
    <tr runat="server"> 
     <td runat="server"> 
     <%-- Data-bound content. --%> 
     <asp:Label ID="NameLabel" runat="server" 
      Text='<%#Eval("Name") %>' /> 
     </td> 
    </tr> 
    </ItemTemplate> 
</asp:ListView> 
0

您可以使用下面的方法来填充组合框....

string strcombobox="<table><tr><td width="208"><div id="languages"> 
          <ul>##ListString##</ul> 
          </div></td></tr></table>"; 


DataSet ds=Get Data To Be Filled from Database; 

string strListCollection=string.Empty; 

for(int i=0;i<ds.Tables[0].Rows.Count;i++) 
{ 
string liCode="<li>&nbsp;<img src="##imageSource##" alt="##imageAlt##" border="0" align="absmiddle"/>&nbsp;<span style=" font-weight:bold">##ListValue##</span> </li> "; 
liCode=liCode.Replace("##imageSource##",ds.Table[0].Rows[i]["iamgeSource"].ToString()); 
liCode=liCode.Replace("##imageAlt##",ds.Table[0].Rows[i]["imageAlt"].ToString()); 
liCode=liCode.Replace("##ListValue##",ds.Table[0].Rows[i]["ListValue"].ToString()); 
strListCollection+=liCode; 
} 

strcombobox=strcombobox.Replace("##ListString##",strListCollection); 
divWhereComboBoxCome.innerHtml=strcombobox; 
相关问题