C# MultiView SetActiveView by ID not working properly - c#

I'm currently trying to figure out what's wrong with my code:
Doesn't work
if(...){
...
}else{
someVariableAsString = "myValue123";
MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);
}
Works
if(...){
...
}else{
//someVariableAsString = "myValue123";
MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);
}
.. why and any solutions for this?

Because you are attempting to act on the INIT rather than the load, the data has not yet been attached at the server.
You should find this review of the life cycle of a web request in ASP.NET useful: MSDN ASP.NET Page Life Cycle
Here is the relevant extract:
Initialization
During page initialization, controls on the page are available and
each control's UniqueID property is
set. A master page and themes are also
applied to the page if applicable. If
the current request is a postback, the
postback data has not yet been loaded
and control property values have not
been restored to the values from view
state.
Load
During load, if the current request is a postback, control
properties are loaded with information
recovered from view state and control
state.
Move the code you are trying to execute into (or after) the page load handler (remember to test for IsPostBack) and see if that doesn't get what you want.
Something New to try:
try changing your doesn't work to:
if(...){
...
}else{
string someVariableAsString = "myValue123";
MultiView1.SetActiveView(My3thView_ID_In_MultiViewControl);
}

It sounds like someVariableAsString is possibly throwing an exception to cause the code not to reach the next line. Check your variable type.

I was able to get a solution to my case:
I changed someVariableAsString to a Property as View.
Created a session variable to Gobal.asax and now I get correct result (one page load later). :-)
but in my case this will do.
Problem solved.
onInit{
m_myVariable;
myFunction();
...
}
void myFunction(){
// if clause described up
}
public View myVariable
{
get { return m_myVariable = Session["myVariableAtSession"] as View; }
set { m_myVariable = value;
Session["myVariableAtSession"] = m_myVariable;
}
}

Related

MVC create function does not add created track to list

I have a problem with my MVC application. The application is about a tracklist where there is a function called create track.
I have created a dataprovider class which adds dummy data to a list. Then the list gets show on the page. The list itself works and it show the dummy data. Now i have created a create action in my controller like this:
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
int hours = int.Parse(collection["Hours"]);
int minutes = int.Parse(collection["Minutes"]);
int seconds = int.Parse(collection["Seconds"]);
Track track = new Track();
track.Name = collection["Name"];
track.Artist = collection["Artist"];
track.AlbumSource = collection["AlbumSource"];
track.length = new AudioDevices.Time(hours, minutes, seconds);
track.Style = (Category)Enum.Parse(typeof(Category), collection["style"]);
trackList.Add(track);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Now... When i start my application I click create. There i enter information for my track and click save. Now when it saves i go back to my list of tracks, but the track doesnt get added to my list.
My Visual Studio shows that the code has handled 2 requests without any errors. It just doesnt get added to the list.
I understand that i am not working with a database and i dont want to work with a database since its not needed in this excercise. I understand that the track will dissapear after restarting the program.
I have instantiaded the tracklist like this at the end of my trackcontroller.cs:
private static List<Track> trackList;
public TrackController()
{
if(trackList == null)
{
trackList = DataProvider.GenerateDefaultTracks();
}
}
If your code follows the standard pattern, every time your Create function is called in the controller, a new controller is created. If your object trackList is created in the constructor, then a fresh one will be created for each call to Create giving the impression that nothing has happened.
Your data is stored in a variable. This variable is not public so it is not obtainable for the view unless you pass it along.
I suggest you store the tracklist in the ViewBag. this way it's accessible from the view.
Now I do not know how you implemented the tracklist so I will just give an example.
define when the page is created.
ViewBag.tracklist = new List<Track>();
after creating the track you put it in the viewbag like this:
ViewBag.trackList.Add(track);
You can access this data in the view or pass it along in the action index parameters like:
return View(ViewBag.tracklist);
I have never actually closed this, but it turned out my visual studio was corrupt. Everything worked fine as i did not received any erros. Reinstalling visual studio worked out.

How to get Type when i using navigation in Silverlight 5

I have a Frame is defined in MainPage:
<navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed" Navigating="ContentFrame_Navigating">
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/Function/{pageName}" MappedUri="/Views/Function/{pageName}.xaml"/>
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>
and in code behind:
string uriStr = "/Function/Page1";
//check it here
if(uriStr is available) //available mean can navigate to uriStr
{
ContentFrame.Navigate(new Uri(uriStr,UriKind.Relative));
}
1./ How can i check "uriStr" is available before call method .Navigate()?
2./ How can i get Type of page if "uriStr" is available?
Please, help me!!!
Thank!!!
For the first question:
How can i check "uriStr" is available before call method .Navigate()?
I don't know the answer, but I am sure there is some way through reflection. What I do is handle the ContentFrame_NavigationFailed event which depending on why you want to know if it exists, should work for you.
For the second question:
How can i get Type of page if "uriStr" is available?
Not sure where in your code your trying to get this but what I do is grab the page in the ContentFrame_Navigated event using this code:
If TypeOf e.Content Is Page Then
_CurrentPage = e.Content
Else
_CurrentPage = Nothing
End If
And then I can use it where ever I like. If you want this in the ContentFrame_Navigating event, you'll have to look into reflection but I don't think it will help much because you won't have the instance of the page that has not been created yet since this event is before that happens.

Silverlight passing an array to a web page's dialog arguments

I have the following line of code to open a web page modal dialog in C# (Silverlight):
var so = (ScriptObject)HtmlPage.Window.Invoke(
"showModalDialog",
modalWindowUrl,
dialogArgs,
"dialogWidth:600px;dialogHeight:600px;");
Now, code similar to the following is being called on the page I am displaying, and I need to make sure it gets the values I'm trying to pass in (this is a MSCRM web page I don't have control over):
dialogArgs.items <-- will be an array I pass in
dialogArgs.items[i].getAttribute("oid") <-- will return something
dialogArgs.items[i].getAttribute("otype") <-- will return something
dialogArgs.items[i].values <-- will return something
What I have tried to send in (from my C# code) is this:
dialogArgs = #"{items:[{oid:" + id + ",otype:" + type + "}]}";
which will result in a JSON string... but I'm guessing this just ends up as a string within the JavaScript and not a JSON object.
Any ideas how I get this to work?
A few side notes:
I can't get IE to debug the modal dialog that results from this call. I can get the debugging tools displaying, but it won't attach to the page because it cannot refresh it.
I don't have control over this modal dialog. It's a page that is displayed using MS Dynamics CRM. For that reason I cannot mess with the JavaScript or anything to test stuff.
Looks like I won the tumbleweed award for this one! Can't believe how uncommon this scenario seems to be. The solution ended up being quite simple, but not very documented so took me a while to track down. Thought I would share here.
Firstly, a quick search across the internet reveals that we can set this up using the following:
var dialogArgs = HtmlPage.Window.CreateInstance("Object");
Which gives you a ScriptObject back. For properties:
dialogArgs.SetProperty("items", items);
Some code for setting up an array and an item should look something like this (I have just created a new GUID for the purpose of this example):
var item = HtmlPage.Window.CreateInstance("Object");
item.SetProperty("oid", Guid.NewGuid());
item.SetProperty("otype", "account");
var items = HtmlPage.Window.CreateInstance("Object");
items.SetProperty(0, item);
And finally, just pass that object straight into your dialog window like this:
var so = (ScriptObject)HtmlPage.Window.Invoke("showModalDialog", lookUpWindow, dialogArgs, "dialogWidth:600px;dialogHeight:600px;");

Webbrowser control is not showing Html but shows webpage

I am automating a task using webbrowser control , the site display pages using frames.
My issue is i get to a point , where i can see the webpage loaded properly on the webbrowser control ,but when it gets into the code and i see the html i see nothing.
I have seen other examples here too , but all of those do no return all the browser html.
What i get by using this:
HtmlWindow frame = webBrowser1.Document.Window.Frames[1];
string str = frame.Document.Body.OuterHtml;
Is just :
The main frame tag with attributes like SRC tag etc, is there any way how to handle this?Because as i can see the webpage completely loaded why do i not see the html?AS when i do that on the internet explorer i do see the pages source once loaded why not here?
ADDITIONAL INFO
There are two frames on the page :
i use this to as above:
HtmlWindow frame = webBrowser1.Document.Window.Frames[0];
string str = frame.Document.Body.OuterHtml;
And i get the correct HTMl for the first frame but for the second one i only see:
<FRAMESET frameSpacing=1 border=1 borderColor=#ffffff frameBorder=0 rows=29,*><FRAME title="Edit Search" marginHeight=0 src="http://web2.westlaw.com/result/dctopnavigation.aspx?rs=WLW12.01&ss=CXT&cnt=DOC&fcl=True&cfid=1&method=TNC&service=Search&fn=_top&sskey=CLID_SSSA49266105122&db=AK-CS&fmqv=s&srch=TRUE&origin=Search&vr=2.0&cxt=RL&rlt=CLID_QRYRLT803076105122&query=%22LAND+USE%22&mt=Westlaw&rlti=1&n=1&rp=%2fsearch%2fdefault.wl&rltdb=CLID_DB72585895122&eq=search&scxt=WL&sv=Split" frameBorder=0 name=TopNav marginWidth=0 scrolling=no><FRAME title="Main Document" marginHeight=0 src="http://web2.westlaw.com/result/dccontent.aspx?rs=WLW12.01&ss=CXT&cnt=DOC&fcl=True&cfid=1&method=TNC&service=Search&fn=_top&sskey=CLID_SSSA49266105122&db=AK-CS&fmqv=s&srch=TRUE&origin=Search&vr=2.0&cxt=RL&rlt=CLID_QRYRLT803076105122&query=%22LAND+USE%22&mt=Westlaw&rlti=1&n=1&rp=%2fsearch%2fdefault.wl&rltdb=CLID_DB72585895122&eq=search&scxt=WL&sv=Split" frameBorder=0 borderColor=#ffffff name=content marginWidth=0><NOFRAMES></NOFRAMES></FRAMESET>
UPDATE
The two url of the frames are as follows :
Frame1 whose html i see
http://web2.westlaw.com/nav/NavBar.aspx?RS=WLW12.01&VR=2.0&SV=Split&FN=_top&MT=Westlaw&MST=
Frame2 whose html i do not see:
http://web2.westlaw.com/result/result.aspx?RP=/Search/default.wl&action=Search&CFID=1&DB=AK%2DCS&EQ=search&fmqv=s&Method=TNC&origin=Search&Query=%22LAND+USE%22&RLT=CLID%5FQRYRLT302424536122&RLTDB=CLID%5FDB6558157526122&Service=Search&SRCH=TRUE&SSKey=CLID%5FSSSA648523536122&RS=WLW12.01&VR=2.0&SV=Split&FN=_top&MT=Westlaw&MST=
And the properties of the second frame whose html i do not get are in the picture below:
Thank you
I paid for the solution of the question above and it works 100 %.
What i did was use this function below and it returned me the count to the tag i was seeking which i could not find :S.. Use this to call the function listed below:
FillFrame(webBrowser1.Document.Window.Frames);
private void FillFrame(HtmlWindowCollection hwc)
{
if (hwc == null) return;
foreach (HtmlWindow hw in hwc)
{
HtmlElement getSpanid = hw.Document.GetElementById("mDisplayCiteList_ctl00_mResultCountLabel");
if (getSpanid != null)
{
doccount = getSpanid.InnerText.Replace("Documents", "").Replace("Document", "").Trim();
break;
}
if (hw.Frames.Count > 0) FillFrame(hw.Frames);
}
}
Hope it helps people .
Thank you
For taking html you have to do it that way:
WebClient client = new WebClient();
string html = client.DownloadString(#"http://stackoverflow.com");
That's an example of course, you can change the address.
By the way, you need using System.Net;
This works just fine...gets BODY element with all inner elements:
Somewhere in your Form code:
wb.Url = new Uri("http://stackoverflow.com");
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbDocumentCompleted);
And here is wbDocumentCompleted:
void wb1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var yourBodyHtml = wb.Document.Body.OuterHtml;
}
wb is System.Windows.Forms.WebBrowser
UPDATE:
The same as for the document, I think that your second frame is not loaded at the time you check for it's content...You can try solutions from this link. You will have to wait for your frames to be loaded in order to see its content.
The most likely reason is that frame index 0 has the same domain name as the main/parent page, while the frame index 1 has a different domain name. Am I correct?
This creates a cross-frame security issue, and the WB control just leaves you high and dry and doesn't tell you what on earth went wrong, and just leaves your objects, properties and data empty (will say "No Variables" in the watch window when you try to expand the object).
The only thing you can access in this situation is pretty much the URL and iFrame properties, but nothing inside the iFrame.
Of course, there are ways to overcome teh cross-frame security issues - but they are not built into the WebBrowser control, and they are external solutions, depending on which WB control you are using (as in, .NET version or pre .NET version).
Let me know if I have correctly identified your problem, and if so, if you would like me to tell you about the solution tailored to your setup & instance of the WB control.
UPDATE: I have noticed that you're doing a .getElementByTagName("HTML")(0).outerHTML to get the HTML, all you need to do is call this on the document object, or the .body object and that should do it. MyDoc.Body.innerHTML should get the the content you want. Also, notice that there are additional iFrames inside these documents, in case that is of relevance. Can you give us the main document URL that has these two URL's in it so we / I can replicate what you're doing here? Also, not sure why you are using DomElement but you should just cast it to the native object it wants to be cast to, either a IHTMLDocument2 or the object you see in the watch window, which I think is IHTMLFrameElement (if i recall correctly, but you will know what i mean once you see it). If you are trying to use an XML object, this could be the reason why you aren't able to get the HTML content, change the object declaration and casting if there is one, and give it a go & let us know :). Now I'm curious too :).

session value lost in page load

i have converted a web application project from 2003 to 2005.everything works fine in 2003,but the converted web application project in 2005 has some problem, problem is in session values,initially the session value works fine(for the first time),but if the page is loaded for the second time the session value becomes empty.
in first page session value is set and in second page the session value is received then i click the button the page will reloaded now the session value is empty..
please get me some answers or links to refer.
Check if your application doesn't alter anything in the folder structure, like creating new files or folders. That often causes the application to be reset, which causes the loss of the Session information. Especially some special folders and files, like the App_Code folder and the Web.Config, cause an immediate application reset when modified.
If this is not the case, then it might be a code logic problem. Try to refactor the session variable's read/writes into using a property:
private string MySessionVar {
get { return (string)Session["MySessionVar"]; }
set { Session["MySessionVar"] = value; }
}
Then add breakpoints to the getter and setter and run your code to check what's causing the session variable to be overwritten. Be sure to check usercontrols if you use them.
Also, if the variable is only used on the current page, you might consider using a Viewstate variable instead.

Categories