dpbvpgvrhwxen3222 2014-05-05 22:21
浏览 32
已采纳

在asp c#中绑定数据像php一样,这可能吗?

I am new to asp.net, and what I want to know is how to bind data directly into a custom div without using things such as data grid or any other thing, let's say I want to bind a description for something stored in my sql DB.

In PHP i used to do this:

<p>
<?php echo $row_RecordSetName['col']; ?>
</p>

But how to do in asp.net using C# in a webform?

I tried to do it in a dataset thing like so but it keeps giving errors:

<p>
<%= Dataset.DB[0].colNAME.ToString() %>
</p> 

as well as i tried doing the following stupid way :

try
{
for (int i = 1; i < 7; i++)
{

        SqlConnection cn = new SqlConnection("Data Source=server;Initial Catalog=db;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("SELECT * FROM mallsdb where mall_un='" + i + "'", cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        MallsDS tds = new MallsDS();
        da.Fill(tds, tds.Tables[0].TableName);


        string MallPIC = Convert.ToString(tds.mallsdb[0].mall_pic);
        string MallNAME = Convert.ToString(tds.mallsdb[0].mall_name);
        string MallUn = Convert.ToString(tds.mallsdb[0].mall_un);

        string MallDESP;
        string check_desp = Convert.ToString(tds.mallsdb[0].mall_desp);

        if (check_desp.Length < 50)
        {
            MallDESP = check_desp;
        }
        else
        {
            string under = check_desp.Substring(0, 30);
            MallDESP = under + "....";
        }


        Result[i] = "<div class='malls'>" + "<img src='images/" + MallPIC + "' width='250' height='250' />" + "<a class='namer' href='malls_private.aspx?mall_un=" + MallUn + "'><h1>" + MallNAME + "</h1></a><p>" + MallDESP + "</p></div>";


    }
    }
    catch (Exception ex)
    {
        Response.Write("Error:" + ex.ToString());
    }


    Label1.Text = Result[1];
    Label2.Text = Result[2];
    Label3.Text = Result[3];
    Label4.Text = Result[4];
    Label5.Text = Result[5];
    Label6.Text = Result[6];
  • 写回答

2条回答 默认 最新

  • duanfu7840 2014-05-06 13:31
    关注

    thanks friends and in the following is the answer to my question, as i read about it in microsoft MSDN.

    <%@ Page language="c#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    
    <script runat="server">
    void Page_Load(Object sender, EventArgs e) 
    { 
    SqlConnection cnn = new 
    SqlConnection("server=(local);database=pubs;Integrated Security=SSPI"); 
    SqlDataAdapter da = new SqlDataAdapter("select * from authors", cnn); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "authors"); 
    Repeater1.DataSource = ds.Tables["authors"];
    Repeater1.DataBind();
    }
    </script>
    <html>
    <body>
    <form id="WebForm2" method="post" runat="server">
    <asp:Repeater id="Repeater1" runat="server">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem,"au_id") %><br>
    </ItemTemplate>
    </asp:Repeater>
    </form>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化