I'm trying add multiple items to a database table. I've created a form that will allow the user to add more form rows which include a dropdown and multiple input fields. New fields are generated when the user clicks on "+". This is developed using JavaScript.
It is easy to add an incremented value to an ID using JavaScript, but new rows are not made using the runat="server" so I am afraid my code behind won't see the values in the fields.
I want my code to loop through each form "row" and grab each item. Then take this data and insert it into a SQL database. How do I get that working?
I can give you some guides to continue from the point you are (and solve this question - so I am afraid my code behind won't see the values in the fields.)
a. You create your controls using java-script on the page.
b. User then is post them back, what you have all ready known, with some new controls that code behind didn't know about.
now the trick here is that you grab this return
HttpContext.Current.Request.Form
and analyse it to see whats "is new there", what are the new post controls that you don't know about - and then you create them on code behind again - and gets their input values.
Related
I am making a basic ticketing system in C# with basic coding experience. Most of my experience is in SQL.
I have the database and tables. I have stored procedures to create and amend tickets.
I am stuck (this is probably very basic) because:
On my EDIT ticket page, I populate various text boxes and drop downs from my existing data via inline SQL.
On my page, I can edit all the fields and dropdowns with new values. (i.e. change a ticket priority from when it was first logged)
I have a button, that calls my "update" stored procedure, however it updates only the NEW fields I have, any amendments to the existing fields are overwritten by the original values before submitting.
The original values are called on pageload, so I think the button reloads the page before submitting. I want it to submit all the values that are on the screen.
I think this must be something obvious, remember I am a novice so I may have missed something simple.
If what you're saying is you load values from the DB into the form controls in the PageLoad event handler, then yes, you're probably overwriting the changed values. To keep things as simple as possible for you, wrap the original values loading code in the following:
if( !IsPostBack )
{
// load initial form values here from DB
}
I'd suggest you read about the ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.90).aspx
I have 2 DropDownLists which are populated in c# code before the page is rendered. When the page is posted back I can successfully get the changed values. However, one condition is if the value changes in the first DropDownList I repopulate the second drop down list. I am doing this using an Ajax call and everything is visually working well.
The problem is when the condition arises that the second DropDownList is populated via Ajax code the list items and selected values are not being shown back in the C# code when posted back. I have read that this will NOT happen.
The DropDownList has an ID="cboMutations"
I have finally got this working using the following method:
Int32 id = 0;
if (!string.IsNullOrEmpty(Request.Form["ctl00$MainContent$cboMutations"]))
{
Int32.TryParse(Request.Form["ctl00$MainContent$cboMutations"], out id);
}
My main questions are:
Is this safe to use ctl00$MainContent$cboMutations to reference the property in this manner?
It works in development, but will it work when deployed?
Asp.net is strict about what data it accepts. An asp drop down list would not allow you to send data like that. This makes sure that users only send values that were presented by the server.
In your case you are using html controls and by passing asp.net's inbuilt security. You should program for the case that malicious users may try to send invalid data using the input ctl00$MainContent$cboMutations.
Since you are parsing the data to an int there is little a malicious user could do. You should be safe when deployed too.
I am working on a project which has a requirement to build "pages" on the fly. A page can consist of various controls like textboxes, checkbox etc. Currently when the user wants to add a new textbox I make a ajax request and render partial view and return the HTML and show it on client side. This works but I also want to handle the data properly when these dynamic controls are filled up by user. In a way if I am not wrong I need to be able to make array of HTML controls. Now if we give static List to our view and generate textboxes using Html.TextboxFor we see that the name generated is something:
[0].FruitName
[1].FruitName
[2].FruitName
How do I handle this index part when making a Jquery Ajax request so that I always get the correct indexes and render it on client.
If anybody has any better solution than making ajax request then also please let me know. I need to handle the dynamic rendering of HTML controls and also access their values properly when posted back to server.
Take a look at Non-Sequential Indices at http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx.
He introduced a helper method to generate it as well.
Also, I think you can just pass an index with your ajax call which then gets passed from Controller to your partial view and use it to generate a proper indexed TextBox.
Update:
I've asked a very similar question at Submit javascript dynamically added elements to controller method like Stackoverflow
I have a listview in wpf and i am swapping two items index..
the swapping must be visible to the user.
i tried giving thread delay..
it didnt work
How to do that..
If I was going to do this I would delete the list view and lock it up where you can't use it again. Then code whatever your list view was outputting in C# using whatever you use to query your database(I think LINQ to SQL is the most robust solution right now) and then use a string builder to construct the html. This way you can assign a id to each div and append an incrementing number to the end of the id. Finally you could write your javascript and use the id's. Here is a link that shows how to build a gridview without using a gridview control.
See the first answer to the question in this link: How to show pop up menu from database in gridview on each gridview row items?
I am not sure whether it will work for your problem or not.
I think you need some kind of animation there. If it is web project, you can use jQuery animation to do that.
I have the following scenario I would like to implement.
I have a number of drop downs that the client can select a range of criteria. From this, they will press a button, the query will be generated and low and behold, a gridview will be produced in a new window (or at least give that impression) bound by an object datasource. I would also like the user to be able to amend the search numerous times so they can generate a number of new windows/gridviews.
Now then..what would be the best approach for achieving this result?
My initial thinking was to create a querystring generated by the client criteria selections (in the drop down lists), a new page would then take the querystring and populate the gridview here. My concern with this approach is that the query string could be a whopper...are there any disadvantages to creating a ridiculously long query string?
Alternatively, are there any other methods or ideas people have used to produce a similar desired effect?
Any suggests taken on board and all advice warmly received.
There is nothing wrong with a long querystring; it is just not as clean or as easy to use as Jeroen's session-object solution. Save the session object value:
Session("ObjectName") = variablename
Retrieve it:
variablename = Session("ObjectName")
The only reason to use a querystring is if you don't want the session data hanging around, though I can't imagine why that would be an issue.
You could handle the button's Click event and store the variables you need in Session object.
In the newly opened window with your gridview you take those values and assign them to the proper parameters in your ObjectDataSource's Selecting event.