ラベル retaining の投稿を表示しています。 すべての投稿を表示
ラベル retaining の投稿を表示しています。 すべての投稿を表示

2012年3月26日月曜日

retaining class state.

dear All

I have a checkbox, a dropdownlistbox and a label in 3 separate update panels.

I have autopostback=true for both checkbox and dropdownlist box and write to the label text in their eventhandlers. The checkbox event when fired writes as expected but not the dropdownlistbox.

I have a class called myclass with a string property called "abrev". I want to remember the complete myclass between postbacks. I think there is something called session to do it...but can someone help me on how to do it in the below code?

aspx.cs code.

public partialclass _Default : System.Web.UI.Page { myclass[] pessi =new myclass[10];protected void Page_Load(object sender, EventArgs e) { ScriptManager1.RegisterAsyncPostBackControl(CheckBox1); ScriptManager1.RegisterAsyncPostBackControl(DropDownList1);for (int i = 0; i < 10; i++) { pessi[i] =new myclass(); } }protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { DropDownList1.Items.Clear(); DropDownList1.Items.Add("something"); DropDownList1.Items.Add("something1"); pessi[0].Abrev ="I am in checkbox"; Label2.Text = pessi[0].Abrev; UpdatePanel2.Update(); UpdatePanel3.Update(); }protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label2.Text = pessi[0].Abrev; UpdatePanel3.Update(); }}
 
aspx code 
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <br /> <br /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <ContentTemplate> <asp:CheckBox ID="CheckBox1" runat="server" autopostback="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> </ContentTemplate> </asp:UpdatePanel> <br /> <br /> <asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server"> <ContentTemplate> <asp:DropDownList ID="DropDownList1" autopostback="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel> <br /> <br /> <asp:UpdatePanel ID="UpdatePanel3" UpdateMode="Conditional" runat="server"> <ContentTemplate> <br /> <br /> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </form>
  
Thank you all in advance,
prasad.. 

Session Variables are what you want. They are are read/write like any other variable, except they persist in memory on the server for the duration of the visitors session. They are also simple to use:

Session["myvar"] = "var";
Response.Write["myvar"]; //will outputvar



hi

Thank you for your reply.

I am a little new to the world of programming and computers as such (not a SW professional). Can you please tell me how to store a complete class?

will this work for my above example?

Session["myname"] = "myclass";

Secondly, where should this session be defined= in pageload? and also when there is a postback?

lastly, I want to be able to change any property of myclass in the code behind when events happen...do these changes automatically take place in the session..

for example...if I change myclass.Abrev (in my example) for a button click event for example then are the session variables updated automatically?

Thank you in advancem

prasad..

Retaining GridView cell color on postback

I have a GridView in an UpdatePanel in which I use JavaScript to allow the user to change cell colors on the fly. It works great, until a postback occurs in which case, all of the user changes are lost. How do I maintain the cell colors after postback? I am trying to use a Save button to store the status of each cell to a database. Unfortunately, the cell colors are reset immediately, so even the save routine does not work correctly.

Thanks for any help you can provide,

Todd

Here is the script used to change the color of a cell when the user right-clicks on it:

<script type="text/javascript"><!-- /* script for changing the background color of the table cells based on the "Selection Mode" */ document.getElementById('ctl00_MainContentPlaceHolder_gvWafer').onmousemove = MouseMove; document.getElementById('ctl00_MainContentPlaceHolder_gvWafer').onmousedown = MouseMove; document.getElementById('ctl00_MainContentPlaceHolder_gvWafer').oncontextmenu = DontShowContext; document.getElementById('ctl00_MainContentPlaceHolder_gvWafer').onmouseup = MouseUp; // handle the MouseMove event for the GridView control function MouseMove(event) { // IE doesn't pass the event object, so we have to pass it manually if (event == null) event = window.event; /* only select table cells if the right mouse button is pressed and the underlying item is a 'TD' element (table data) */ if (event.button == 2 && event.srcElement.tagName == 'TD') { var cell=event.srcElement; var cellValue=cell.innerHTML; /* display the row and column indexes in the browser status bar */ window.status="row, column = " + cell.cellIndex + "," + cell.parentElement.rowIndex; /* determine which item in the Selection Mode drop down list box is selected */ switch(document.getElementById( 'ctl00_MainContentPlaceHolder_ddlSelectMode').getAttribute( 'selectedIndex')) { case 1: // available /* Don't allow cells marked as QC Samples to be marked Available */ if (cell.style.backgroundColor != '#996600') { // GREEN cell.style.backgroundColor='#00FF00'; } break; case 2: // picked /* Don't allow cells marked as QC Samples to be marked as Picked */ if (cell.style.backgroundColor != '#996600') { if (cellValue >= 20 || cellValue <= 200) { // GRAY cell.style.backgroundColor='#999999'; } else { // WHITE cell.style.backgroundColor='#FFFFFF'; } } break; case 3: // reserved /* Don't allow cells marked as either Picked or QC Samples to be marked as Reserved */ if (cell.style.backgroundColor != '#996600' && cell.style.backgroundColor != '#999999') { if (cellValue >= 20 || cellValue <= 200) { // PURPLE cell.style.backgroundColor='#9933CC'; } else { // WHITE cell.style.backgroundColor='#FFFFFF'; } } break; default: return; } } } /* clear the status bar data */ function MouseUp(event) { window.status = ""; } /* handle the ContextMenu event for the GridView control - Don't show the default context menu */ function DontShowContext(event) { return false; } if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); // --></script>

You are going to need to save these styles and reinitalize them on the callback of the UpdatePanel e.g.:

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(EndRequestMethod);

prm.add_beginRequest(BeginRequestMethod);

...or you can add your logic to the server side. The problem is that you are rebinding the whole GridView and obviously client-side changes aren't persisted.

-Damien


Get the current PageRequestManager instane and

try to use beginRequest and endRequest


Thanks for both of your inputs. I don't know why I expected the JavaScript to make the changes to the GridView control. I forgot that the GridView control is rendered as a Table and that the JavaScript was simply modifying the Table. I actually took a little bit different route to solve the problem (I didn't see your replies until just a few minutes ago...had email notification turned off by mistake).

So, since the GridView is rendered as a Table and JavaScript cannot change GridView properties, I had to somehow track the changes that were made to the Table and propagate them to the GridView control.

I decided to create a TextBox control in which the JavaScript code would write the changes made to the cells (in the format "[row],[column]=[color]\n"). My VB code then parses out those changes and makes the corresponding change in the GridView control. I make the width and height of the TextBox zero so that it is not visible.

It may not be the best way to accomplish the task, but it is working. Since this is for an intranet application, and I have a ton more work to do, I am going to stick with it. The next time I run into a problem like this I will keep the other suggested solutions in mind.

Thanks,

Todd

Retaining ViewState in Atlas

On my real estate website I have a Search page and a Results page. The Search page contains three linked Atlas cascading dropdowns (CDDs). The user makes some selections on these and then clicks a <search> button to see the results. This opens the Results page, which displays a list of properties meeting the search criteria. I have this working fine, apart from one thing: when the user goes back to the Search page, the CDDs have been reset to their original values. I was wondering if there was an easy way for the Search page to retain its settings. I have a partial solution, but it's not very good. If I set AutoPostBack to "True" for each CDD, they retain their settings when I click the <back> button on the browser. This is pretty horrible though, as it means the whole page gets posted back - undermining the whole purpose of using Atlas in the first place! I have tried enclosing the CDDs in an UpdatePanel (so that only they get posted back and not the whole page), but they just end up flickering all the time as (I guess) they get stuck in a code loop.

Also, setting AutoPostBack to "True" is only effective when the user clicks the browser's <back> button. I want to offer my own button or link in case they arrived at the Results page from another route. I have tried creating a hyperlink to the Search page, adding a button with a PostBackUrl of "Search.aspx" and adding a Response.Redirect command to a button's on_click event. Even with AutoPostBack set to "True" (which I want to avoid), none of these methods works. The CDDs just get reset every time.

Also, the other server controls I have on the Search page (check boxes, conventional drop-down list boxes, etc.) get reset every time too. I've tried looking into ViewState, but I can't get it to work. It looks like I need a way to save the viewstate on exit and somehow retrieve it when I load the page again. I guess I'm missing something fundamental. Any suggestions? Am I barking up the wrong tree? I imagine this kind of thing must have been done many times before and would appreciate a few hints.Any ideas on this? I'd really appreciate some help on something that must be a fairly common problem - or will be as Atlas takes off. Thanks for your interest.

You could try saving the viewstate to sessionstate or to disk

An example of saving viewstate to disk can be found here :
http://aspalliance.com/911


That seems to be on the right track. I'm now saving the values of my page controls to Session State when I exit the page, and retrieving them when I load it. This works fine for all the controls except the CDDs. Since these are populated by WebMethods, I don't seem to have any opportunity to set their SelectedValue properties. To focus on the problem area, I have tried adding a button to my form with a single line of code reading:ddlRegion.SelectedValue = RegionIDWhere RegionID is a value retrieved from the session.This causes an argument out of range exception as follows:[ArgumentOutOfRangeException: 'ddlRegion' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value]I get the same error if I change the variable RegionID to the literal "1" (which is definitely in the list).What it boils down to is this:How can I programatically set the SelectedValue of an Atlas Cascading Dropdown?I know how to save the value to, and retrieve it from, session state, but I don't know how to assign it to an Atlas cascading dropdown list. Any ideas?

1. Perest the CCD by setting the selected value of the cascading extender control ( and not the drop-down) , as explained in the article ( link )

2. and you can retrieve the viewsate ... as explained on this link.http://gandhirohan.blogspot.com/2007/09/cascadingdropdown-control.html