0

This is my combobox that bind to database.

<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>

Now I replacing it with below code ( using a jquery plugin to show combo box that have a avatar with items).

 <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>

And now I want to read data from database.In fact using this code instead of that combo box. How can I bind it to database?
Thanks.

4

2 回答 2

0

asp.net 有很多数据绑定控件,其中很多都允许您进行一些自定义格式来显示您的数据。

由于您有要显示的数据列表,请考虑使用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>
于 2012-10-08T05:30:59.000 回答
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;
于 2012-10-08T06:29:47.763 回答