tags:

views:

206

answers:

3

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 ... loop %> style loops but I need it in ASP.NET style, probably perform the operation in Page_Load event and use intrinsic ASP.NET controls.

+3  A: 

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

Gabriel McAdams
I think what he want is different from using repeater
Shantanu Gupta
@Shantanu Gupta - I disagree, a repeater seems perfect for this, though I'm not sure about the `href`.
Kobi
A repeater would be too much of an overkill (and add a lot of unecessary markup) for such a simple implementation...
Ariel
Can I access the onclick attribute from within the repeater control?
Salman A
yes you can , check up "Repeater.ItemCommand " and use asp:linkbuttons instead of normal links.. :)
Madi D.
Thank you. I managed to use `asp:Repeater` that contains `asp:HyperLink` with `NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "myid", "javascript:myfunction(""{0}"");") %>'`
Salman A
+2  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();
Ariel
This seems to be very nice answer up to some extent. But how this control is being rendered at frontend ? R u using literal or span tag ?
Shantanu Gupta
+1 for simplicity. This is exactly what I wanted but instead I ended up using DataRepeater as the script might require enhancements in the future.
Salman A
@ Salman A - Thanks for the +1. I generally try to stay away from using runat="server" type controls such as a repeater because they create a really bloated html output.
Ariel
@Shantanu Gupta - The output is meant to go into a <asp:Literal></asp:Literal> control (which has no additional html markup on the client) or an html <span runat="server"></span>
Ariel
+2  A: 

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>";
 }
peacmaker
You would want to seperate you design Code and Code behind..
Madi D.
i just did that, thx for pointing it out
peacmaker
+1 for simplicity :)
Madi D.
+1 for showing how to use data reader (I didn't know how to use it).
Salman A
One quick note - for efficient memory handling you'd want to wrap your connection, command, and reader in using() blocks so they get disposed when you're done with them ;)
Ariel