Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

jQuery - following an example but can I save data server side?

I'm working on an old ASP.Net/C#/VS2013/ Web Forms (NOT MVC) code.

I need to add textboxes dynamically but I also have some server side controls. I click Save and call Save onClick Event in code-behind and save the data.

1. At first, I followed this example First Example
But I had an issue that was answered here EE Answer

That solution works as far as clicking on the "add textbox" button once. But now, I have to click on the Save button twice to be able to save the data.

So, I can't figure out why the above solution doesn't work. I've spent too much time on it. Now want to try jQuery
2. I found a jQuery example that adds the textboxes dynamically. http://jsfiddle.net/x7uQx/17/

If I click save, how can I access the textboxes added in the code-behind, server side? If I use [WebMethod] and Ajax...I can pass in the dynamic textboxes but what about the other server side textbox, dropdowns I have (the tags has runat=server)? (I can change all of them to jQuery but I'm almost done with this and these dynamic textboxes are the only section left)
SOLUTION
Avatar of Jai S
Jai S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

It will have the dynamic controlso too?
I can try it and see.
yes, it will have all form fields
I'll add the dynamic textboxes with jquery and try.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I'll try it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. Going to work this morning and will try the solution. I will post back.
Question : So, to do this and save
$.ajax({
	  type: "POST",
	  url: url,
	  data: data,
	  success: success,	//Implement callback success function
	  dataType: json
	});

Open in new window


I need to, in the aspx.cs (code behind) ...do this. If I do this....then I have to "static" keyword so I can't use the controls that have "runat=server"?

[WebMethod]
public static save(what goes here?)
{

}
For your query you need to do following steps
  1. Capture form variables into an array on the client with jQuery’s .serializeArray() function
  2. Use $.ajax() or my ServiceProxy class to make an AJAX call to the server to send this array
  3. Use $.ajax() or my ServiceProxy class to make an AJAX call to the server to send this array
  4. On the server create a custom type that matches the .serializeArray() name/value structure
  5. Create extension methods on NameValue[] to easily extract form variables
  6. Create a [WebMethod] that accepts this name/value type as an array (NameValue[])
Thanks. I think that webmethod needs to be static which I think is my issue but let me see. I'm at work now. I'll post back.
[WebMethod]
public static save(what goes here?)
{

}

Create a ModelClass which contains all the attributes you needed in your webform. Use that ModelClass in your web method like below:

public class ModelClass{
	//all your attribute
}

//In your WebMethod
[WebMethod]
public static save(ModelClass model)
{

} 

Open in new window

Thanks. The server side way of doing this works. It gets me going. I'll try the jquery tO'Day. Thanks guys.
Instead of writing WebMethods on the code behind Pages, you can approach to write the same logic on Web services (asmx) separately.
This is an old code and I'm new at this job. I'll try the jquery and see how it goes.
web method and web service both are server side (code behind), so it won't make any difference!

And from client side, you just need to pass your form to server side code and keep rest of the part for the server side code!

It's pretty simple
Ok. Thanks. I'll pass the form. I have a model for it as well.
Hope you got resolution!
thanks, guys.