开发者

ASP.NET adding controls inside a loop

Pardon me, this really is a noob question but please understand that I do not have much ASP.NET experience. All I need to do is:

1) Open up the following SQL query:

SELECT myid FROM mytable

2) For each record, generate this fragment of HTML:

<a href="#mynameanchor" onClick="myfunction( '__myid comes here__' );"><img src="http://someurl/__myid comes here as well__/small.png" /></a>

Its easy for me to use the classical ASP <% do until recordset.eof ... loo开发者_运维知识库p %> style loops but I need it in ASP.NET style, probably perform the operation in Page_Load event and use intrinsic ASP.NET controls.


Use a repeater control. In the ItemTemplate, you can put whatever you would like to be generated for each record returned from your query.

http://articles.sitepoint.com/article/asp-net-repeater-control


add this in the aspx page source

   <td id="urLink" runat="server">

   </td>

add this in the Page_Load event

 SqlConnection con = new SqlConnection();
 con.connectionstring = "your connection database";
 SqlDataReader reader;
 SqlCommand command = new SqlCommand(con);
 command.Commandtext = "SELECT myid FROM mytable";
 command.CommandType = CommandType.Text;
 reader = command.ExecuteReader();
 while(reader.Read())
 {
      urLink.innerHTML += "<a   href="#mynameanchor" onClick="myfunction( " + reader["myid"].tostring() + " );">
    <img src="http://someurl/" + reader["myid"].tostring() + "/small.png" /></a>";
 }


Looks like the onclick is a javascript event which means you can write out the HTML markup to a string and then append it all into a literal control. Remember to use a stringbuilder :)

here's a very basic example:

// get data from database and into a reader before

StringBuilder links = New StringBuilder();

while(reader.read())
{
    links.appendFormat("<a HREF='id_{0}'>{0}</a>", reader["ID"]);
}

ltlLinks.Text = links.ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜