Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top