ASP.NET Accessing AJAX created controls on postBack - javascript

I am generating 2 dropdown boxes and a CheckBoxList control at runtime using AJAX callbacks to a web service (.asmx file). The service creates the two Dropdowns and CheckBoxList on the server and returns the rendered html as a string which is injected into the web page at runtime, using javascript functions.
During postBack I want to obtain the values of the two dropdown boxes and also determine which (if any) of the checkboxes have been ticked.
Am I right in thinking that the HTML that is injected into the page at runtime is not sent back to the server during postback? If this is the case what would be the most sensible way of obtaining my values?
I'm thinking that the best way to obtain the values of the dynamic controls will be to use javascript to read the values and write them to a hidden field that's part of the page class, just before the postback.
If there is a better way to do this then please share!

One method of retrieving your values during a postback is by saving values in a hidden field which as the element name suggest is invisible on the page -
<asp:HiddenField id="countrySelected" value="" />
In your javascript before the post back you can populate your required information -
document.getElementById('countrySelected').value = 'USA';
In your codebehind you could then select the value to be used -
countrySelected.value

Since the webservice creates the html which is later injected in the page they are not server controls and therefore, the server has no way of knowing they exist. The Hidden html element seems to me the way to go on this case.

Related

Laravel redirect with javascript

I have a form where a user can select a number ranging from 2 to 10. Based on the selected number I'd like to redirect to '/mypage?number=[2..10]' like the way Laravel's redirect() function does. Thus, flashing the old input values to fill the form accordingly. Based on the $_GET['number'] parameter a for-loop is executed printing 2 to 10 input fields.
How can this be done in the most efficient (and meant to be) way? Or do I have to set up a new route like Route::post('mypage/{number}', 'myController#redirectMethod')?
You are probably over-thinking this. If this is in a form, just have the form action be /mypage and set the name of the select to number and the form's method to get. When the form is submitted, it will bring the user to the page. If you'd like it to auto-submit when the user modifies the dropdown, you can submit the form programatically via js/jquery $('#myForm').submit().
There should be no need for additional routes just to handle the number parameter. Use \Input::get('number') to grab the value on the server side.
How will the javascript get {number}
all you need to do in the javascript
location.href
1) Either redirect should take place on the server - when you send params on sever and it brings you the right page or redirects you to the right page.
2) Or you need to generate javascript in laravel_blade and set this logic in the Javascript. Say, generate input fields with extra data- attribute and make javascript to handle this attribute.
3) Or maybe, avoid redirects at all and make javascript generate variable amount of input fields on the same page judging on data-attributes of the button that was pressed.

New row in asp:repeater by java script

In my aspx page I have repeater to show list something, I want to add new row and show it in repeater without postback. I will send date soon. I don't want to use AJAX
How to add row to asp:repeater by JavaScript?
The best bet is inpect the code using a tool like firebug retrieve the html for the row, and recreate that using javascript and attach it to the DOM. Dirty but doable. Using something like jquery will make your work easier.
I know you dont want to use AJAX but the cleanest method would be to use an UpdatePanel and do is on the server side using a partial postabck
The repeater just generates repeating html so it's definitely possible to insert new html by using javascript. As #mellamokb mentions those changes won't be visible on the server side.
It's generally a bad idea to mix client and server side rendering with ASP.NET Webforms.
In the case of a list of some kind being dependent on e.g. a combobox it's often a better idea to render lists for all options in the ASPX code. Then use javascript to hide every list except the currently active one.

Cannot access the value of the selected item of a disabled drop down, in action class (struts 2)

On a Jsp page I have some select elements which were disabled after a value was selected (Disabled them in javascript). Now when the form is submitted, I can not access those selected values in the action class.
I know for sure that this is caused by the select elements being disabled because, when I tried the same without making them disabled, it worked fine.
Now I don't understand why is this so. I thought maybe I should enable them before the form is submitted, but it does not seem a good idea.
I faced this problem while implementing this : Creating struts 2 forms dynamically on jsp using java script .
(You can find the code there. Although I don't think you will need the code, because it is clear where the problem is.)
Here I am able to access the values of text fields but I can not access the values of select elements.
I asked this question separately because I thought this is a different topic.
Thanks!!
Disabled fields by W3 specifications will not get posted on the server side so this issue is not related to the Struts2 but in generic an HTML way to go
Disabled controls
i am not sure why you want to use disabled control for your form.things can be done using readOnly attribute or use hidden fields
You can set them in hidden field through java script and pass it to action

Updating GridView without postback using javascript

I have a gridview and a dropdownlist on my page.
The gridview is binded through code behind with some columns. Among these price is also a column.
My scenario is to change the price field based on the dropdown criteria.
The price column consists of values in "lakhs", and i need to change them as crores or usd or some other format as per dropdown.
I don't want to go for postbacks. I want these to be implemented using javascript.
(These changes are for user conversion. They need not to be saved on database)
Thanks in advance
Madhu
You have two options. If you don't want to go for any response from the server (i.e. you don't want to make an AJAX call), you could populate a hidden field with a list of comma-delimited values that correspond to your dropdown values. It's an ugly approach, but it would work.
The other option is to make an AJAX call to a web method on the server (or possibly a financial service like Yahoo! Finance). When your dropdown selected value changes, that event fires a call to the server.
If you were able to indicate what platform you're using (PHP, .Net or other), someone will likely be able to provide more relevant examples and code samples.
Since the grid is rendered as an HTML Table, you can actually manipulate anything on the client.
I would pickup jQuery for the task

What is the best practice for passing variables from one HTML page to another?

I'm relatively new to web application programming so I hope this question isn't too basic for everyone.
I created a HTML page with a FORM containing a dojox datagrid (v1.2) filled with rows of descriptions for different grocery items. After the user selects the item he's interested in, he will click on the "Submit" button.
At this point, I can get the javascript function to store the item ID number as a javascript variable BUT I don't know how to pass this ID onto the subsequent HTML page.
Should I just pass the ID as an URL query string parameter? Are there any other better ways?
EDIT: The overall process is like a shopping cart. The user will select the item from the grid and then on the next page the user will fill out some details and then checkout.
I should also mention that I'm using grails so this is happening in a GSP page but currently it only contains HTML.
You could just use a hidden input field; that gets transmitted as part of the form.
<html>
<head>
</head>
<body>
<script type="text/javascript">
function updateSelectedItemId() {
document.myForm.selectedItemId.value = 2;
alert(document.myForm.selectedItemId.value);
// For you this would place the selected item id in the hidden
// field in stead of 2, and submit the form in stead of alert
}
</script>
Your grid comes here; it need not be in the form
<form name="myForm">
<input type="hidden" name="selectedItemId" value="XXX">
The submit button must be in the form.
<input type="button" value="changeSelectedItem" onClick="updateSelectedItemId()">
</form>
</body>
</html>
It's good one, but better is to use some script language such as JSP,PHP, ASP....and you can use simple POST and GET methods.
The best method (imho) is to include it in the URL
href="http://NewPage.htm?var=value";
encodeUriComponent a string Value
One way to send over variables using POST to another page is to make the link to the subsequent page a submit input on a form where the action attribute is your target page. For every variable you have, you can include using inputs of attribute type "hidden" in this form, making only the button visible.
Another option is to dynamically generate links on the page with something like PHP where you basically repopulate the current GET queries.
Finally, you can always store this information in the PHP $_SESSION array and not have to worry about continually passing these variables through site navigation.
Your choice will depend on how many navigational options there are where you'd like to keep the same variables. It will also depend on how secure you'd like your back end to be and the amount you'd like to disclose to the advanced web user.
If you are only going to need the ID on the subsequent pages, then you can pass the id as a query string parameter.
But there will be times when you need to relay more information and passing a variety of parameters to different pages and having to maintain different sets of parameters for different pages can get a little hairy. When this is the case I'd suggest that you keep a hidden field on the form and create an argument object that stores each of your parameters. Serialize the argument object with JSON and store this in you hidden field. Post the form back to the server. When the next page loads, deserialize the object and retrieve the values you need.
Assuming that you are limited to using html pages, I think the best approach would be to pass the id along on the query string to the next page. It is relatively easy to pull that value back off the query string on the next page. If you need to be a little more stealthy about passing the variable (or you need the variable to persist for more than one page), you could also set a cookie and retrieve it on the next page.
Since you are trying to do this in a Grails application you do have a choice of using Flash scope. This might not make any sense if you want to go directly from one HTML page to the next as the scope would be defined in a controller. If you do not need to do any sort of processing between requests, I'd suggest using a hidden form field to keep it simple.
http://grails.org/Controllers+-+Controller+Scopes

Categories

Resources