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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题