<li>从数据库中检索项目

时间:2016-08-30 02:22:39

标签: sql asp.net

  <li class="dropdown"><a href="#" class="btn btn-info" data-toggle="dropdown"><i class="fa fa-microphone"></i>Inbox <span class="badge" runat="server" style="color: red" id="txtContactCount">0</span> </a>
                <ul class="dropdown-menu" id="getdata" runat="server">
                    <li class="divider"></li>
                    <li><a href="Portal_noticexD.aspx">View all notifications</a></li>
                </ul>
            </li>

  public void binddata()
    {
        DataTable dt = new DataTable();
        SqlCommand com = new SqlCommand("E_R_GET101", con);
        com.Parameters.AddWithValue("@Subs_cd", loggeduser.SUBSIDIARY_CD);
        com.Parameters.AddWithValue("@EMPLOYEE", loggeduser.EmployeeNo);
        com.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(com);
         con.Open();
         da.Fill(dt);
        getdata.DataSource = dt;
        getdata.DataBind();
        con.Close();
    }

enter image description here

我的存储过程将从我的数据库中获取此数据,并绑定到<li>,但未能这样做,或者我需要更改方法?

1 个答案:

答案 0 :(得分:2)

您可以在ASP.NET Web表单中使用Repeater控件来创建无序列表。

<强> .ASPX:

<form id="form1" runat="server">
    <ul>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <li>
                    <%# Eval("STATUS") %>
                </li>
            </ItemTemplate>
            <SeparatorTemplate>
                <hr />
            </SeparatorTemplate>
        </asp:Repeater>
    </ul>
</form>

代码背后:

public DataTable BindData()
{
    DataTable dt = new DataTable();
    SqlCommand com = new SqlCommand("E_R_GET101", con);
    com.Parameters.AddWithValue("@Subs_cd", loggeduser.SUBSIDIARY_CD);
    com.Parameters.AddWithValue("@EMPLOYEE", loggeduser.EmployeeNo);
    com.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(com);
    con.Open();
    da.Fill(dt);
    Repeater1.DataSource = dt;
    Repeater1.DataBind();
    //getdata.DataSource = dt;
    //getdata.DataBind();
    con.Close();
}

您也可以考虑通过实施using声明,以正确的方式在ADO.NET中处理资源进行Google搜索