Camillia
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes, it will have all form fields
ASKER
I'll add the dynamic textboxes with jquery and try.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I'll try it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. Going to work this morning and will try the solution. I will post back.
ASKER
Question : So, to do this and save
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?)
{
}
$.ajax({
type: "POST",
url: url,
data: data,
success: success, //Implement callback success function
dataType: json
});
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
- Capture form variables into an array on the client with jQuery’s .serializeArray() function
- Use $.ajax() or my ServiceProxy class to make an AJAX call to the server to send this array
- Use $.ajax() or my ServiceProxy class to make an AJAX call to the server to send this array
- On the server create a custom type that matches the .serializeArray() name/value structure
- Create extension methods on NameValue[] to easily extract form variables
- Create a [WebMethod] that accepts this name/value type as an array (NameValue[])
Check the below tutorial for reference
https://weblog.west-wind.com/posts/2010/sep/07/using-jquery-to-post-form-data-to-an-aspnet-asmx-ajax-web-service
URL ScreenShot
https://weblog.west-wind.com/posts/2010/sep/07/using-jquery-to-post-form-data-to-an-aspnet-asmx-ajax-web-service
URL ScreenShot
ASKER
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)
{
}
ASKER
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.
ASKER
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
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
ASKER
Ok. Thanks. I'll pass the form. I have a model for it as well.
Hope you got resolution!
ASKER
thanks, guys.
ASKER
I can try it and see.