views:

201

answers:

1

I'm building a heavily CRUD based ASP.NET website and I've got to the phase of the project where I'm trying to build a search webpage that shows the user different subsets of a certain important table based on parameters they enter such as dates and different text fields.

I can't use a fixed SQL statement, because depending on whether they search by date or by something else, the SQL would be different, so instead I have been generating a results list using a table web control that starts out invisible and then is filled and set to visible once the user identifies a search they want to make. I used a table control because its easy to create and modify in the code behind, and I was able to make the needed calls to the database and populate that table as required.

However, the second thing I need with this search page, is the ability to allow the user to select a record from the results and then go edit it using the rest of the CRUD based pages on the site. To do that, I created asp:buttons in the table and gave them all different ids and the same event handler. But since I created the buttons dynamically, it seems the event disappears on callback and my scheme fails, so I'm taking a second look at how to do this.

My current code for the events looks like:

 tempcell = new TableCell();
 Button tempbutton = new Button();
 tempbutton.Text = "Go";
 tempbutton.ID = "gobutton" + rowN;
 tempbutton.Visible = true;
 tempbutton.Click += new EventHandler(tempbutton_Click);
 tempcell.Controls.Add(tempbutton);
 temprow.Cells.Add(tempcell);

My results table is declared like this:

<asp:Table ID="ResultTable" visible="false" runat="server" 
GridLines="Both" CellSpacing="8">
</asp:Table>

I'd like to be able to identify which record the user selected, so I can send that info on to another page. And of course, I still need to be able to generate results for several different search criteria. Currently I have date, template and code based searches that lead to different stored procedures and different parameters based on what the user has entered.

Any suggestions on this? It seems like the data grids and repeaters require SQL statements in advance to work properly, so I'm not sure how to use them, but if I could then the item command approach would work with the buttons.

+1  A: 

Your event will hook up successfully if the button is recreated in the page load event.

Otherwise, use a GridView. It is the perfect solution for simple CRUD, and you can control it as much as you need.

ck
I can't use a gridview, I don't have a static SQL statement I can use.
Tony Peterson
You don't need one, you can build and execute your statement in code, then pass the results to the gridview. It has a DataSource property that will happily accept a DataTable.
ck
You then have to manually handle the update and edit commands, but I assume you have this already with your own implementation.
ck
+1 and also there is the listview, which you can use when you need more custom html
eglasius
ok thanks, I will check that out.
Tony Peterson