Search code examples
asp.netascx

Button in ascx , loop via query, send parameter or identify uniquely


In most languages for the web, it's a simple 3 line process.

php for example.

<?php foreach (x in y) {?>
  <td><a href="">my button #<?php echo ++i;?></a></td>
<? } ?>

Not in ASP. Apparently I need to program artificial intelligence for a lunar mission to loop a query and output a unique button. Apologies for the sarcasm, I am frustrated extensively.

Here is my code, which does not do what I need. Am I missing an simple option that won't take an hour to code?

<% using (SqlDataReader reader=Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(Config.GetConnectionString(),CommandType.Text,"SELECT * FROM [dbo].[table] WHERE bHasBeenProcessed IS NULL")) { %>
                    <% while (reader.Read()) { %>

                        <tr>
                            <td><%=reader["Name"].ToString()%></td>
                            <td><%=reader["Email"].ToString()%></td>
                            <td><%=reader["datestamp"].ToString()%></td>
                            <td>
                                <asp:Button CommandName='<%#reader["id"].ToString()%>' id="cmdSave" text='Process It' OnClick="HandlerFunction" runat="server" cssclass="dnnPrimaryAction" />
                            </td>
                        </tr>

                <% }} %>

I find it ridiculous that I have to use CommandName/Argument in the first place. People say "you can't mix server side and client side code". Well I'm not, right? The entirety of that snippet is produced server-side. It queries the database, loops over the records, and outputs a table. Which works great. But the CommandName refuses to process my variable. So I have NO way of identifying which row was clicked.


Solution

  • I coded href's with URL parameters and process it that way instead via POST. Ridiculous. Problem solved.

    Definitely welcome any suggestions on how to quickly do this, whether that be correct or incorrect methodology.