问题如题  
如何写一个函数可以直接传参数显示新闻列表,调用方式如下面ASP  一句话搞定,不知道可以不可以用用户控件做,
下面附ASP的写法参数作用:rs  游标    SQL 指定连接   NUB  指定显示条数  tab 指定数据表  其他要显示的字段
 调用方式 call showtitle(rsnews,sql,5,"news","w_txtline","newtitle","addtime")function showtitle(nub,tab,css,newtitle,addtime,id)
Dim rs,Sql
set rs=server.CreateObject("adodb.recordset")
sql="select top "&nub&" * from "&tab&" order by "&id&"  desc"rs.open sql,conn,1,1
if rs.eof then
response.Write("<tr><td class='"&css&"' align=left height='25'>&nbsp;对不起,暂时没添加新闻&nbsp;</td></tr>")
else
do while not rs.eof

response.Write("<tr><td class="&css&" height=25  align=left>&nbsp;&nbsp;<font color='#4585E6' style='font-size:6px'>■&nbsp;&nbsp;</font>")
Response.Write "<A href='new_list.asp?id="&rs(""&id&"")&"'>"&left(rs(newtitle),18)&"</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Response.Write "<font color='#4585E6'>"&rs(addtime)&"</font></td></tr>"

rs.movenext
loop
session("listtime")="add"
rs.close
set rs=nothing
end if
end function

解决方案 »

  1.   

    当然可以了
    用ado.net读上来数据得到一个DataSet或者DataTable值
    然后给GridView的DataSource属性赋值
    调用GridView的BingData函数
    就可以了
      

  2.   

    <%@ Page language="C#" %>
    <%@ import namespace="System.Data" %>
    <%@ import namespace="System.Data.SqlClient" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">  void Page_Load(Object sender, EventArgs e)
      {    // This example uses Microsoft SQL Server and connects
        // to the Northwind sample database. The data source needs
        // to be bound to the GridView control only when the 
        // page is first loaded. Thereafter, the values are
        // stored in view state.                      
        if(!IsPostBack)
        {      // Declare the query string.
          String queryString = 
            "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";      // Run the query and bind the resulting DataSet
          // to the GridView control.
          DataSet ds = GetData(queryString);
          if (ds.Tables.Count > 0)
          {
            AuthorsGridView.DataSource = ds;
            AuthorsGridView.DataBind();
          }
          else
          {
            Message.Text = "Unable to connect to the database.";
          }    }       }  DataSet GetData(String queryString)
      {    // Retrieve the connection string stored in the Web.config file.
        String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;          DataSet ds = new DataSet();    try
        {
          // Connect to the database and run the query.
          SqlConnection connection = new SqlConnection(connectionString);        
          SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);      // Fill the DataSet.
          adapter.Fill(ds);    }
        catch(Exception ex)
        {      // The connection failed. Display an error message.
          Message.Text = "Unable to connect to the database.";    }    return ds;  }</script><html  >
      <head runat="server">
        <title>GridView DataBind Example</title>
    </head>
    <body>
        <form id="form1" runat="server">      <h3>GridView DataBind Example</h3>      <asp:label id="Message"
            forecolor="Red"
            runat="server"/>      <br/>          <asp:gridview id="AuthorsGridView" 
            autogeneratecolumns="true" 
            runat="server">
          </asp:gridview>    </form>
      </body>
    </html>
      

  3.   

    其实很简单 最简单的方法就是用一个数据控件 类似DataList之类的  在里边建好每条数据怎么显示 接下来最简单的方法就是利用dataList提供的方法设置好数据源 以及查找 的参数  当然还有其它很多方法 
      

  4.   


    你这样的话  一张首页要显示很多个类别的新闻每次都要重复绑定
    我原来的想法是这样的  在一个用户控件上放一个DATALIST或者REPEATER控件   把他的样式设置好,
    然后在通过要显示的页面调用用户控件,通过不同的参数给让那个用户控件显示不同的内容
      

  5.   

    这是我能写的函数public class charCls
    (
       public static string getNews(string sql)
       {
            SqlConnection SqlConn = new SqlConnection(dboperate.ConnectionConn());
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand(sql, SqlConn);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet(); 
            sda.Fill(ds, "Mytitle");
            string strget;
            strget="<ul>"
            for(int i=1 ;i< ds.Tables["Mytitle"].Columns.Count; i++)
            {
               strget=strget+"<li>+ds.Tables["Mytitle"]..Rows[i]["Newstitle"]+</li>";
            }
            strget=strget+"</ul>"
            return strget;
       }
    }
    调用方法protected void Page_Load(object sender, EventArgs e)
    {
          this.DivId.InnerHtml=charCls.getNews(Sql语句);
    }现在我的目的是想能不能通过用户控件来到达这样的目的