Asp.Net Change a value from another page - asp.net

Hi
I have 2 webpage in my asp.net project.
I defined a int value in second page.
i want to change int value from first page. How can I make this?

You can't change the variable directly. But you can use Session to achieve this.
In Page 1
Session["session-name"] = "5";
In Page 2
if (Session["session-name"] != null) // check the session if it is exist
{
int a;
try
{
a = int.Parse(Session["session-name"].ToString());
}
catch
{
a = 0; // you can set null too. Whatever you want
}
}

You likely want to use Session Variables. You can start learning here.

Related

Multiple OnSaving event in DevExpress XAF

Im working on a piece of code using DevExpress XAF, I noticed that if im using the event OnSaving that the code executes 2 times, how can i prevent that
protected override void OnSaving()
{
if (PrestamoP != null)
{
PrestamoP.Prestado -= Monto;
PrestamoP.Save();
}
else if (PrestamoG != null)
{
PrestamoG.Prestado -= Monto;
PrestamoG.Save();
}
base.OnSaving();
}
XPO does not guarantee that the OnSaving method is called once. See the corresponding note in the XPO Best Practices article.
I can see that you are changing the PrestamoP.Prestado property based on the value of the Monto property. This code is fine if you execute it only once and only when the Monto property is specified for the first time. This code is not fine if you:
Save this object without changing the Monto property;
Update the early specified Monto value.
So, it appears that a more complex logic is required for the PrestamoG.Prestado property. First, I would move it to the Monto property setter and take the previous value into account (do not forget to check the IsLoading property in this case). Second, I would consider calculating the Prestado value dynamically instead of storing its value. This will allow you to resolve issues with the duplicate business logic execution. See an example here: How to: Calculate a Property Value Based on Values from a Detail Collection.
I can offer different methods for CRUD functions on onSaving method.
IsNewObject, IsDeleted.
// insert
if (Session.IsNewObject(this))
{
a = new a(Session);
a.CreateReferencedProperties(this);
}
// delete
else if (IsDeleted)
{
a= Session.FindObject<A>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("A=?", this));
if (a!= null)
a.Delete();
}
// update
else
{
a= Session.FindObject<A>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("A=?", this));
if (a!= null)
a.CreateReferencedProperties(this);
}
You can use the code below to prevent xaf from entering on saving twice.
base.OnSaving();
SessionObjectLayer sessionObjectLayer = this.Session.ObjectLayer as SessionObjectLayer;
if (sessionObjectLayer == null || sessionObjectLayer.ParentSession == null)
{
//Enter only once
}

How to refresh Factbox

I have a form, when i click on my button.It adds to my table A (what my factbox shows)is it possible to refresh the factbox with X++ code? I can't figure out how to refresh my infopart or query which factbox uses.
For an infopart you can call an update of the data source of the infopart's form run:
void clicked()
{
PartList partList;
int i;
FormRun infoPartFormRun;
FormDataSource infoPartDataSource;
super();
partList = new PartList(element);
for (i = 1; i <= partList.partCount(); i++)
{
infoPartFormRun = partList.getPartById(i);
if (infoPartFormRun.name() == identifierStr(MyInfoPart))
{
infoPartDataSource = infoPartFormRun.dataSource();
if (infoPartDataSource)
{
infoPartDataSource.research();
}
}
}
}
I added the check for the infoPartDataSource because I first tested this with a cue group fact box, which does not have a data source (or at least I could not figure out how to get the data source of one of the cues in the cue group and since you asked for an infopart fact box, I did not investigate further).
Update: The issue seems to be popular at the moment, Martin DrĂ¡b also wrote in his blog about it: Refreshing form parts

ASP.net Sessions, references or not?..and how to write a session holding a list?

I have this property in my code:
public List<TreeViewNodeContentHolder> NodeContentHolder
{
get
{
if (Session["NodeContainerSession"] == null)
{
var tempSessionVar = new List<TreeViewNodeContentHolder>();
Session["NodeContainerSession"] = tempSessionVar;
return (List<TreeViewNodeContentHolder>)Session["NodeContainerSession"];
}
else
return (List<TreeViewNodeContentHolder>)Session["NodeContainerSession"];
}
}
and the thing I want to achive is that I want to be able to add objects and linq-query the list/session.
So basicly I whould like to be able to write something like:
NodeContentHolder.Add(new TreeViewNodeContentHolder{prop1=1,prop2=2});
I also want to be able to do this:
var someNode = NodeContentHolder.Where(e=>e.prop1 == x).FirstOrDefault();
And then another question aswell..if I whould do the thing above this line...whould I then be able to update the object in the session by doing:
someNode.prop1 = 12345;
??
Or whould I have to first "pull" the object and then remove it from the list/session and then add it back in to make sure that it gets updated?..
Thanks in advance!
All of that you say here is correct, all the list you mention here are save by reference, and if you keep on session the reference then the session is saved last, on page unload, so anything you change on the list is reflected on the object that session will be save.
The only think that I will change on your code is if Session["NodeContainerSession"] != null did not mean that is also a List<TreeViewNodeContentHolder>, this is a check that you must make.
public List<TreeViewNodeContentHolder> NodeContentHolder
{
get
{
object oSessionNode = Session["NodeContainerSession"] as List<TreeViewNodeContentHolder>;
if (oSessionNode == null)
{
oSessionNode = new List<TreeViewNodeContentHolder>();
Session["NodeContainerSession"] = oSessionNode;
}
return (List<TreeViewNodeContentHolder>)oSessionNode;
}
}

Flex 3 : Easy Deep Linking Question

I'm having a problem with deeplinking on my Flex 3 site. I want people to be able to link to different parts of the site. I also want to be able to type a url into the browser bar and be taken to a particular part of my site. Also, I need the default to open to #view=2.
I'm having problems setting the default #view=2. So, it's supposed to check the browser fragment to see whether its a valid section of the site. If it is then it should call parseUrl() and open that section of the site. So far, so good. The problem is how do I set the default to view=#2, if the loop doesn't find a valid view number?
Here's my code:
private function initBrowserManager(): void {
browserManager = BrowserManager.getInstance();
browserManager.addEventListener(BrowserChangeEvent.BROWSER_URL_CHANGE, parseUrl);
browserManager.init("","My Website");
if(browserManager.fragment != null){
for (var j:uint = 0; j < ComboBoxDP.length; j++){
if(browserManager.fragment == "view="+ComboBoxDP[j].series){
parseUrl();
break;
}
}
}
}
I've tried to add this line: else{browserManager.setFragment("view="+ 2); parseUrl();} everywhere I could think of, but no luck so far. I know that the answer will be really simple. Any suggestions?
Thank you.
-Laxmidi
I'm assuming some of the intent of the code, but I'd use a boolean:
var initialFragmentValid:Boolean = false;
if(browserManager.fragment != null){
for (var j:uint = 0; j < ComboBoxDP.length; j++){
if(browserManager.fragment == "view="+ComboBoxDP[j].series){
initialFragmentValid = true;
break;
}
}
}
if (!initialFragmentValid) {
// set the default
browserManager.setFragment("view=2");
}
// always parse initially because we'll have a fragment regardless
parseUrl();

Accessing the object/row being edited in Dynamic Data

I'm modifying the "Edit.aspx" default page template used by ASP.NET Dynamic Data and adding some additional controls. I know that I can find the type of object being edited by looking at DetailsDataSource.GetTable().EntityType, but how can I see the actual object itself? Also, can I change the properties of the object and tell the data context to submit those changes?
Maybe you have found a solution already, however I'd like to share my expresience on this.
It turned out to be a great pita, but I've managed to obtain the editing row. I had to extract the DetailsDataSource WhereParameters and then create a query in runtime.
The code below works for tables with a single primary key. If you have compound keys, I guess, it will require modifications:
Parameter param = null;
foreach(object item in (DetailsDataSource.WhereParameters[0] as DynamicQueryStringParameter).GetWhereParameters(DetailsDataSource)) {
param = (Parameter)item;
break;
}
IQueryable query = DetailsDataSource.GetTable().GetQuery();
ParameterExpression lambdaArgument = Expression.Parameter(query.ElementType, "");
object paramValue = Convert.ChangeType(param.DefaultValue, param.Type);
Expression compareExpr = Expression.Equal(
Expression.Property(lambdaArgument, param.Name),
Expression.Constant(paramValue)
);
Expression lambda = Expression.Lambda(compareExpr, lambdaArgument);
Expression filteredQuery = Expression.Call(typeof(Queryable), "Where", new Type[] { query.ElementType }, query.Expression, lambda);
var WANTED = query.Provider.CreateQuery(filteredQuery).Cast<object>().FirstOrDefault<object>();
If it's a DD object you may be able to use FieldTemplateUserControl.FindFieldTemplate(controlId). Then if you need to you can cast it as an ITextControl to manipulate data.
Otherwise, try using this extension method to find the child control:
public static T FindControl<T>(this Control startingControl, string id) where T : Control
{
T found = startingControl.FindControl(id) as T;
if (found == null)
{
found = FindChildControl<T>(startingControl, id);
}
return found;
}
I found another solution, the other ones did not work.
In my case, I've copied Edit.aspx in /CustomPages/Devices/
Where Devices is the name of the table for which I want this custom behaviour.
Add this in Edit.aspx -> Page_Init()
DetailsDataSource.Selected += entityDataSource_Selected;
Add this in Edit.aspx :
protected void entityDataSource_Selected(object sender, EntityDataSourceSelectedEventArgs e)
{
Device device = e.Results.Cast<Device>().First();
// you have the object/row being edited !
}
Just change Device to your own table name.

Resources