How can I access the value of a checkbox without a postback? - c#

I have a radgrid controlling access to an application for users. There are two columns, a 'name' column and a checkbox 'access' column representing their current access permission. the checkboxes are populated from a database. I would like the to change the checkboxes to grant or deny access to the users I specify. Since there are alot of users I would like to make multiple changes and use a submit button to write the changes to the database. I do not want to postback with each checkbox change and wait for the page to "blink". My problem is that with the checkbox postback disabled, when I click submit, the value of the checkboxes are not registering the changes I make. I need a way to access the current client-side state of the checkbox or some workaround to accomplish this.
Thanks in advance!

You could use javascript to send an ajax request that saves the change to the database without reloading the page. Then your updates happen right away and there's no "blink".

Thank you for the support. It turns out that the issue wasnt with client/server-side visibility, but with the page lifecycle. I was refreshing the radgrid in the PageLoad method and checking the value of the checkboxes in the Submit method. I didn't realize PageLoad was executed prior to Submit, thus clearing my selections. By removing the RadGrid refresh from PageLoad, the current values of the checkboxs were preserved.

If you set AutoPostBack to false, that will disable the postback, but they should still be posting their current values.
Sounds to me more like an issue with the dynamic generation of the checkboxes.

Not sure why you are not able to access the values of checkbox at server side, but to answer your question "I need a way to access the current client-side state of the checkbox or some workaround to accomplish this"...
You can use server tags and read the values of checkboxes at client side.
For more information about server tags, read this MSDN link.

Related

Page can only have one server-side form tag

I'm facing a problem with one of my pages. I have a FormView control on the page that I use to enter new rows into a database table, and a GridView control on the same page to update/delete rows from the same database table.
The FormView control has validators on it to validate any input being thrown at the database, and it seems to interfere with the GridView when I try to edit a row. When I try to save the edit, the validation control on the FormView gets fired and an error comes up because the textbox input is blank, so the GridView cannot save the modified table data.
Perhaps a visible example will help:
I had an idea where I encase these controls in different forms, hoping that the submit of one form won't fire anything in the other but then I got an error saying I can't have more than one form on the page with runat="server", which as far as I can tell is required.
How can I get around this?
Thanks.
You can only have one Form.
Use different ValidationGroups for your FormView and GridView instead.
http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx
Yes, this is a known limitation (feature?) of webforms. You can only have 1 form with a runat="server" tag.
In your scenario you can use ValidationGroup names to group validation together so only "like" validation is executed when a control of the same validation is triggered.
What I did is I had two forms coded as postback to the page and parsed out the postback variables in Page.Load myself. Weird? Yes. Works? Yes.

How to avoid page refresh on select of DropDownList

I am working on a project in which on selecting a drop down list item the values from the database should appear in the respective two text boxes.
But, alongside I am placing an image which is actually getting created based on the two values. Now on selecting the next dropdownlist item there is a page refresh and the placed image dissapears.
How to I avoid the page refresh keeping in mind that the fields from the database must get displayed on the page in the two fields on select of drop down list.
Kindly help!
thanks...
In my case, there exists a table in which there are two halfs, the left has text boxes n a button n to the right side of the table the image appears.
If you are using ASP.NET WebForms, you could wrap the controls which you only wants to post back inside an UpdatePanel.
Alternatively, remove AutoPostBack="true" from your DropDownList, and use javascript/jQuery AJAX to perform your database request.
Disable autopostback.
<asp:DropDownList AutoPostBack="false" ... />
Without the slightest piece of code, this will be hard to answer.
To prevent default behavior in javascript, there is this method
e.preventDefault();
Where e is your event.
Your issue is to preserve the values fetched from DB.
The easiest and safest way is to store the values fetched from database into hidden fields. This way when your page postbacks the values persists and you can use them as you wish.
Happy Coding!!!

I need to change Multiview or place holder whenever I select different values in dropdown list

I am creating online booking page.
I got some example page you can see in this link
http://www.parkercorporate.co.uk/webbookercc/OneForm.asp
I need the same behaviour in my site whenever user selects value in pickup dropdown list in the right side I need to change the text values or view.
To do this behaviour how should I write code in ASP.NET and c#.
Thanks
Ranam
First, use an UpdatePanel so you don't see any visual postback.
Second, set the AutoPostBack property on your DropDownList to True.
Third, handle the DropDownList's SelectedIndexChanged event. This will now fire after every change in the DropDownList, because you enabled AutoPostBack.
In the event handler, you can now change the ActiveViewIndex of the MultiView, or show/hide controls, change textboxes, etc.
First, create different blocks (i.e divs) and give ID's to them.
Then, for each value in the dropbox, fill the divs w.r.t it.
Assign "OnChange" event to the dropdown, and in the handler, set visible or invisible to corresponding block.
On the other hand, If you want to do this on client side, I strongly suggest you to use JQuery, to prevent a lot of requests going to server.
Check this out with JQuery:
make visible div on dropdown select in jquery
If you won't fetch values from database, there is nothing complex about this page. Because it just writes hospital if you choose hospital etc. and changes control's visible state.
Even if you think you need db queries when dropdown's selected change event, actually at this page there is no need. Just store db values in hidden controls (like Hospital- for Hospital) and when dropdown selection changes, write hidden's value completely in javascript

Help with column editor in a data grid!

I have a two different data sources that share one data grid. I have a column editor that allows the user to pick which columns they want to see. This works except, when I change the datasource, the columns change back to to what they started. i want the columns to stay the same as the user the picked them. Any help? thanks
Did you remember to set the AutoGenerateColumns property to false?
Sounds to me like the page is posting back and reverting to initial settings. You can put the gridview in an asp.net ajax updatepanel to prevent anything else on the page from changing.
Or you could store the columns in session variables whenever they change and read them then set the proper columns in the page_load event if its a postback.

Validating textbox content on blur by calling server-side method without affecting page behaviour

I have a textbox in one grid-view column where upon entering a particular value and losing focus of the textbox, should post to the server to get the text validated through a server-side method. If the entry is valid, a result set to fill rest of row cells would be returned, else the bgcolor of the textbox needs to be changed to Red.
I tried posting back through the obvious way, i.e. making the textbox's autopostback as true and wiring up a server-side OnTextChanged event handler to validate the entered value.
It is working with this setup, but is also affecting the remaining page controls behaviour. For example, if I click a button in some other grid after entering some text in the textbox, the OnTextChanged handler gets called thus preventing the button's click event, which I also wish to call to execute its functionality.
Kindly suggest what alternatives/corrections I should carry out to enable textbox content server-side validation plus making the other controls/updatepanels work as expected.
Me dumb. I tried everything from creating PageMethods, UpdatePanels to jQuery as hinted in lincolnk's reply. But the thing which finally worked was removing the Autopostback attribute from the textbox control.
After removing it the OnTextChanged event executed each time any server postback was initiated after changing the text. Thereby, executing both the OnTextChanged method and the other control's method. :)
I can think of a couple general approaches.
Create a web service with your validation routine and manually make the call (jQuery or whatever) when the text changes. Manually update the client display when you get a result.
Convert your gridview column to a templated field. Add a CustomValidator and wrap the textbox and validator in an UpdatePanel. Set the textbox to auto-postback and the UpdatePanel to conditional update so only the one you are using is refreshed.
Option 1 is kind of an end-around the typical asp.net process, and you would still want to validate everything on the server-side when the page is posted back.
Option 2 might have performance issues, since you're hitting the page again every time you do a validation.

Categories