ASP.NET Multiple Submits with Same Name with Masterpage - asp.net

I'm using a master page with multiple submit buttons to sort a table, as well as other submits on the page doing other things. I'd like to name all the submits for sorting the table the same name (sortTable), so I can do something like this on PostBack to pass the value of the column to sort by:
if (Request.Form["sortTable"] != null)
{
sortTableBy(Request.Form["sortTable"]);
}
My problem is that when I run the site all the names are replaced with different values like ctl00$mainContent$ctl01 making it hard to capture them by a single name on postback. Any advice?

VDWWD's comment put me on the right track to solve this problem. I just added a function for each of those submits to call onclick and then put my sort table code in there. Thanks VDWWD.

Related

hit insert several times and got duplicates

I have a simple asp.net web forms page that does an insert to my sql server db. My server was running slow at the time and I pressed Insert button several times because I didn't think it took but it did all 3 times.
So I have duplicates from that one interaction. How would I prevent this?
Thanks,
rod.
Bind the onClick of your button to a javascript and disable or hide the button and optionally display a message like "Please Wait.." to avoid multiple clicks
Code Behind:
Button.Attributes.Add("onclick", "fn_OkClick();");
ASPX file:
<script language="javascript" type="text/javascript">
function fn_OkClick()
{
var okieButton = document.getElementById('<%=Button.ClientID%>');
okieButton.style.display = 'none';
}
</script>
I'd first evaluate if the business rule is correct. Are you sure you shouldn't be able to insert multiple records with those same details? Generally I would think there would be some validation around whether or not something can be inserted. If you can do a blind insert then it suggests you should be able to do multiple inserts of the same data.
Take a resource booking program. For a given resource, at a given time, it can only be booked once. Therefore you would want to prevent people doing what you've done and inserting multiple records into the booking table for the same item at the same time.
As such there should be some validation before you try to insert the record to ensure that the selected resource is in fact available at the time you've selected. If so, insert the record, if not, don't insert the record, and return a meaningful reason to the user as to why.
If there is no validation, then there should be no reason to prevent the user from inserting multiple records.

Two tables on one page Asp.Net

My project is in Asp.Net Webforms using C#.
On one of my web pages, I want to have two tables. One is going to be a summary table and the other a more detailed breakdown of the summary. When the user clicks on one of the rows of the summary table, I want the corresponding breakdown rows to appear in the breakdown table.
These tables can have anything between 60 and about 500 rows.
I have thought of two possible ways of doing this and I'm not sure which is best, or if there is another, better way of implementing this:
1) When a user clicks on one of the rows in the first table, a postback event happens, and the data for the second table is loaded from the code behind.
2) When the webpage is loaded for the first time, both tables are filled with all of the possible data, but all of the rows in the second table have the "display: none;" CSS attribute. When a user clicks on a summary row, the CSS of the corresponding breakdown rows is changed using JavaScript.
I'd go with your first thought and perhaps either
load the data for the second table through an AJAX request returning only row data in JSON (or maybe even the HTML for the table)
use an UpdatePanel
if the user only needs to view the data, I would go for the former, but if they need to edit the data in some way, I would probably choose the latter (even though I'm aware that UpdatePanels are effectively a full postback in async clothing, I feel their use can make the UI more fluid).
You could also make an AJAX call when the user clicks on a row in the summary table to return the data for the 2nd table.
second way no way, loading all of data and then hiding showing is not a good idea, it will be slow and if you add more functionality it will only get in your way...
I recommend this two approaches:
1)first table with pagination
click on row triggers ajax call to page method
page method return second table
here, you can see how to that:
http://encosia.com/2008/02/05/boost-aspnet-performance-with-deferred-content-loading/
2)using of uframe
http://labs.omaralzabir.com/UFrame2005/
cheers

Processing variable number of form fields

I am working on a form which displays information about orders. Each order has a unique id, but they are not necessarily sequential on the form. Also, the number of fields can vary (one field per row on the form). The input into the form will not be mapped straight into the database, but will be added to the current value in the database, and then saved. An example of the form is in the picture below - the callout on the right shows the id for each row.
I know how to generate the form like this, but I can't work out how I can easily process each of these rows reliably. I also know how to give each of the fields a unique identifier, like name="order-23" or name="order[23]", but how can I translate that name so that I can update the related record in the database?
EDIT: One solution I can think of would be to iterate through every form field in the FormCollection, and if the name of the field matches the pattern, then I will extract the number from that field-name and process it.
However, I feel that there must be a much easier way to go about it - this method would likely involve a fair bit of string processing on each field, and there would possibly fall over if I have to add extra fields for each row later on.
Don't you have a list of IDs after postback? I believe you should not depend on what IDs are actually sent from the form, as anybody could change the IDs on the form to whatever they want, so it's a security issue. So you should after postback have a list of IDs you want to update (the same list you used to create the form with). In that case, you know exactly what id string you should use to retrieve the value from FormCollection.
In case you really can't get the list of IDs you are going to update, just use the FormCollection iteration as you suggested in your comment. The string processing is not that expensive in comparation with all other stuff being done at request processing.
If you have the names, then simply read the values by using Request.Form["order-23"] or re-create the controls in page pre-init and you'll have access to the values in your save event directly through the created controls.
I've done this loads in my CMS.
Essentially i sort of cheated.
The idea is something like this ....
render the form to the client, then look at the source code gneerated.
You should see that it generated a form tag with an action attribute.
When you click the submit button the form will be sent to that url so in the case of an order submission you would post the page back to OrderPage.aspx?OrderId=xxxx
then on the server you would build an update statement for your db that contained something like ...
"Update orders where order id =" + request.querystring["OrderId"]
So how do you update the action ...
In the calling page lets say you have a link called "New Order", when that link is clicked you need to do 2 things ...
redirect to this page.
generate an order id for this new order.
ok the first is simple, simply link it to this page.
the second ...
if this page is not a postback if(!IsPostback) { /* get a new id */ } depending on your order id's this may involve generating a new guid or doing something like getting the next number in a list by doing a select max(id) from a db.
once you have this new id you should still be in the page_load event.
this.form.Action = this.form.Action + "?OrderId=" + yourNewOrderId;
... tada ...
Send page back to the client.
view the source.

How can I print a value in my page from Database

How do I display information in my page (take it from DB) in asp.net 2008
Currently I use GridView, but it display the information in a TABLE. I want to display it in a normal line.
for example:
I have this table in my DB
http://www.rofof.com/img2/4jpqfn26.gif
alt text http://www.rofof.com/img2/4jpqfn26.gif
I want to print an information in my page as:
You are A and your ID is: 1
without using GridView.
How are you getting your data from the database to your gridview? Also do you want to display only a single record or would you display multiple records?
The Repeater is a great control if you want to display all the records. You can bind it the same way your binding the grid then define your template to render the html however you want.
If you only want to display a single record you could just do this in the code behind of the page. For example:
Page_Load()
{
if (!IsPostBack) //Make sure this only runs first time we call page
{
//Get your data from the DB, can't help here unless
//you provide more details.
MyData data=....
myLabel.Text="You are " + data["name"].ToString() ....
}
}
If you update your question with more details about your data source (are you using a data table custom objects etc...). I can update my answer with a better exampkle

What is the best approach for this CRUD search page in ASP.NET

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.
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.

Resources