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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题