preserve UI state of asp.net controls after button click - c#

I have an ASP.NET web app with a dropdown list and a number of ASP.NET textboxes/labels. when the user chooses a particular item from the dropdownlist the labels change text and some of them are hidden etc using jquery. However after clicking on a button the controls are being changed back as when the page is first loaded.
Please suggest
How i Can preserve the new labels name etc?
Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
Should i use hidden fields to store the value?

1: How i Can preserve the new labels name etc?
Yes,
You can use hidden field to store.
2: Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
No,
On page_load access hidden field, extract and use values.
3: Should i use hidden fields to store the value?
Yes,
You can use hidden field to store changes you made in GUI control using jQuery. Make that hidden field server accessible by making it runat server. On server end when you get postback access the hidden field, extract and assign values to respective controls. You can see how it would work in this post.

As you might be knowing , after clicking the button if it is submit type it will do the postback to the server , as HTTP is stateless , it is your responsbility to generate the label text and value again. Generally ASP.NET viewstate take care of doing so for input texts, but for labels you have to do by yourself. I can suggest the solution like this
1) Have a javascript method which will have the logic to change the value of label and hidden field based on the selected value of the dropdown
function MyFunction ( ddlSelectedValue)
{
if(ddlSelectedValue == "1")
{
$("#LABELID").html('YOUR VALUE');
// rest other logics
}
}
2) ASP.NET viewstate will help you to preserve the dropdown state after button click . So on add this jquery script on the page top
$(document).ready(function(){
var ddlSelectedValue = $("#DDLID").val();
MyFunction(ddlSelectedValue);
});
3) attach the the point 1 method to onchange of the dropdown also.
4) Using the hidden field is Ok , because hidden fields will serve your requirement in using in the server side as they will also be posted back.

Related

How to Find UserControls Control value in Jquery Asp.net?

I am using jQuery and ASP.net
I have a User Control and wanted to Set the values of this User control to the database, for that I need the user control's value. In the same way, I want to show the data from the database in the user control, for that I need to Get the values by jquery.
In my user control I have 4 TextBoxes and 2 Buttons (SET/UPDATE)
AutoCompleteSearch_New is ascx user control
Here is my tried code:
var ID = $('#<%= ((HiddenField)AutoCompleteSearch_New.FindControl("hdnvalue")).ClientID %>').val();
But I dont wan't to use hidden fields.
Can I directly find the control's value without using hidden fields?
It is similar to use value from a webform.
Here is the code for the same.
Var TextBoxValue = $('#YourTextBoxID').val();
Inspect element and get the Textbox ID and replace it with YourTextBoxID.
Or
Var TextBoxValue = $('#<%= YourTextBoxID.ClientID').val();
where YourTextBoxID is your asp:Textbox ID.
When ever you load user control in your aspx page, jquery consider it as a whole page which is combined of user control and rest of the aspx controls exist in the form. So you can directly get textbox value in your jquery
you can do that by adding ClientIDMode="Static" to the Control
and then use
var txtvalue= $("#TextBoxId").val();
Provided storing the values in Session is not an option, other options I see are :
javascript variable declaration in the user control markup
client-side decryption of the view state
custom data attributes on html elements
I'm afraid HiddenField is your best option. That's somewhat the same idea behind ViewState.
If you don't want the client to tamper with the value, you may go for a combination of the value, and a hash of the value concatenated to a secret key ( value+separator+Hash(value+secretkey) )
If you don't want the client to access the value, you may rely on encrypting it in the HiddenField
hey u can directly find control of textbox or hiddenfield without mention its type
var value=$('AutoCompleteSearch_New_hdnvalue').val();
here AutoCompleteSearch_New is the name of user control u load on page and
hdnvalue is the id of hidden field in user control AutoCompleteSearch_New
enjoy.. :-)

Radio Boxes Clickable Images

I am trying to figure out how I would setup a group of images to work like radio buttons. Basically when a user clicks a image it wil display a check mark below to indicate it has been selected. I could accomplish showing the check mark I believe by a onclick event and some css. But what would be the best way to do this with ASP.net I also need to transmit the data to the server via a form.
I would populate a hidden field with the resulting value of the selection via client-side code. Then you just read that value in your code behind on submit.

How to read dynamic ASP TextBox (runat server), if this is created on Postback not on PageLoad

Can we read a dynamic text box, which is created on first PostBack and not on PageLoad.
I have one form with a drop down field, when I choose item from that drop down and press a button then my dynamic fields are created. I want to read them with click on another server button. How can this be done?
You should be able to get a handle on the dynamically-created textbox with code like this:
TextBox txtDynamic = (TextBox)(pnlParent.FindControl("txtDynamicId"));
The event handler in which you're trying to access the textbox will be running in a thread which has permission to access it.
if you want to read data from a control that will not be recreated, you have to save the data. I suggest to use Viewstate (MSDN LINK) and/or Session variables (MSDN LINK).

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

Using C#, how can I read the content of dynamic created textboxes?

Hy,
I have created some dynamic textboxes with standard content.
Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?
Thanks a lot.
Jeff
Update
This is how I am creating the textboxes:
foreach (string name in listOfNames)
{
TextBox tb = new TextBox();
tb.Text = name;
tb.BorderStyle = BorderStyle.None;
tb.BorderWidth = 0;
tb.Font.Name = "Arial";
tb.Font.Size = 8;
}
The specific will vary depending on the technology you are using. However the concept would remain very similar, though for ASP.NET it will be a little more interesting.
WinForms/WPF/Silverlight
Maintain a list of the dynamically created textboxes and when the button is pressed you can run through the list of textboxes and read the Text property to get the user input.
ASP.NET - After the tag update it seems this section is most appropriate to your requirement.
For ASP.NET you will need to create the textboxes in an override of the OnInit method, this should happen on each postback. Then in the Button.Click event you can read the user input from the textboxes that you created in the OnInit function. You need to ensure that the controls are created with the same ID on each post back.
You need to ensure that the text boxes are recreated on every postback.
If you do not recreate them, you will not be able to access their properties or events.
The best place to create dynamic controls is the page Init event handler.
I suggest reading up on the ASP.NET page life cycle.
Update (following updated question)
Make sure to set an ID (and a different one, at that) for the text boxes, so you can refer to them later on.
I can't see where you are adding these controls to the page either.
Request.Form is a collection of Key-Value pairs that represents all of the data coming back from the ASP.NET request. If you access that you can get any value whether its control is specified in the ASPX code or dynamically created in the code behind file.
You can also get their values placed back in them automatically if you recreate them during the init part of the page lifecycle. If you do this, their values will be set when ASP.NET recreates the state of the page and applies the values from the form.
You should be able to access them as you would a normal control, you just need to get a reference to the control.
Remember to re-create all controls on each postback.
this is a useful article on dynamic controls in asp.net

Categories