How do I simply open an ASP page and possibly pass it data from a C# form

  • Thread starter Thread starter Brian F
  • Start date Start date
B

Brian F

Exactly what the subject says.
I have an ASP page that I want my C# windows application to open in
Internet Explorer, and if possible have it send along a list of values
from a list box. Thank you.
 
That would mostly depend on the page.

Does it take parameters, or are you trying to fill in textboxes on the page?

If the page take paramters build a string with the values and pass it to
System.Diagnostics.Process.Start something like this:

System.Diagnostics.Process.Start("http://www.myhost.com?parm1=value1&parm2=v
alue2");

HTH
Brian W
 
heres a few classes i wrote a while back,

it probably needs a bit of tidying up, but it works

some nice functions for setting the values of an HTML form once the page has
loaded + submitting a button from a windows forms app.

The main one you'll be interested in though is...
public void Navigate(string _url, string _valuedatapairs)..

this method you just call like :

webcontrol.Navigate("www.yahoo.co.uk","param1=one&param2=two&etc=easyasthis"
);

if you want to pass values to an array from a list box, use a for loop, and
make each parameter the same name.... but you probably
know what to do from here.

also started doing some work on showing a custom context menu -

anyway, hope this helps

(you'll need to get VS.NET to create a wrapper for the WebControl) - i
haven't included it here

sam martin


/////////////////////////////////////////////////////////////////////////

public class DocUIResponseArgs : System.EventArgs
{

private string _appref;
private string _appdata;

public DocUIResponseArgs(string _r, string _d)
{
this._appdata = _d;
this._appref = _r;
}

public string AppRef
{
get { return this._appref; }
}

public string AppData
{
get { return this._appdata; }
}
}

public class DocUIHandler
{
private string _appref;
private DocUIResponseHandler _handler;

public DocUIHandler(string _r, DocUIResponseHandler _h)
{
this._appref = _r;
this._handler = _h;
}

public string AppRef
{
get { return this._appref; }
}

public DocUIResponseHandler Handler
{
get { return this._handler; }
}
}

public delegate void DocUIResponseDelegate(object sender, DocUIResponseArgs
e);
public delegate void DocumentCompleteDelegate(object sender,
DocUIResponseArgs e);
public delegate void DocUIResponseHandler(object sender, DocUIResponseArgs
e);

/// <summary>
/// Summary description for WebControl.
/// </summary>
public class WebControl : System.Windows.Forms.UserControl,
IDocHostUIHandler
{
private string _urlbase;
private AxSHDocVw.AxWebBrowser axWebBrowser;

private System.Collections.ArrayList _delegatemap;

public event DocumentCompleteDelegate DocumentCompleteEvent;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public WebControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

this._delegatemap = new ArrayList();

_urlbase =
System.Configuration.ConfigurationSettings.AppSettings["Accuman.Web.Server"]
;


}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

public void Navigate(string _url, string _valuedatapairs)
{
//_delegatemap.Clear();

object _ourl = this._urlbase+_url;
object _onull = new object();

object _header = "Content-Type: application/x-www-form-urlencoded" + "\n"
+ "\r";

string _data =_valuedatapairs;

object _odata = System.Text.ASCIIEncoding.ASCII.GetBytes(_data);

this.axWebBrowser.Navigate2(ref _ourl,ref _onull,ref _onull,ref
_odata,ref _header);

}

public void RefreshPage()
{
object _onull = null;

this.axWebBrowser.Refresh2(ref _onull);

}

public void SetFormFieldValue(string _fieldid, string _value)
{
IHTMLDocument2 doc = (IHTMLDocument2)this.axWebBrowser.Document;
mshtml.HTMLInputElementClass _f =
(mshtml.HTMLInputElementClass)doc.all.item(_fieldid,null);

_f.value = _value;
}

public void SubmitForm(string _formid)
{
IHTMLDocument2 doc = (IHTMLDocument2)this.axWebBrowser.Document;
mshtml.HTMLFormElementClass _f =
(mshtml.HTMLFormElementClass)doc.forms.item(null,0);

object _oevt = new object();

_f.submit();
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(WebControl));
this.axWebBrowser = new AxSHDocVw.AxWebBrowser();

((System.ComponentModel.ISupportInitialize)(this.axWebBrowser)).BeginInit();
this.SuspendLayout();
//
// axWebBrowser
//
this.axWebBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.axWebBrowser.Enabled = true;
this.axWebBrowser.Location = new System.Drawing.Point(0, 0);
//this.axWebBrowser.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser.OcxSt
ate")));
this.axWebBrowser.Size = new System.Drawing.Size(150, 150);
this.axWebBrowser.TabIndex = 0;
this.axWebBrowser.DocumentComplete += new
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentCom
plete);
//
// WebControl
//
this.Controls.Add(this.axWebBrowser);
this.Name = "WebControl";

((System.ComponentModel.ISupportInitialize)(this.axWebBrowser)).EndInit();
this.ResumeLayout(false);

}
#endregion

// IDocHostUIHandler Implementations
void IDocHostUIHandler.EnableModeless(int fEnable)
{

}

void IDocHostUIHandler.FilterDataObject(MsHtmHstInterop.IDataObject pDO,
out MsHtmHstInterop.IDataObject ppDORet)
{
ppDORet = null;
}

void IDocHostUIHandler.GetDropTarget(IDropTarget pDropTarget, out
IDropTarget ppDropTarget)
{
ppDropTarget = null;
}

void IDocHostUIHandler.GetExternal(out object ppDispatch)
{
//ppDispatch = new PopulateClass(this);
ppDispatch = null;
}

void IDocHostUIHandler.GetHostInfo(ref _DOCHOSTUIINFO pInfo)
{

}

void IDocHostUIHandler.GetOptionKeyPath(out string pchKey, uint dw)
{
pchKey = null;
}

void IDocHostUIHandler.HideUI()
{

}

void IDocHostUIHandler.OnDocWindowActivate(int fActivate)
{

}

void IDocHostUIHandler.OnFrameWindowActivate(int fActivate)
{

}

void IDocHostUIHandler.ResizeBorder(ref MsHtmHstInterop.tagRECT prcBorder,
IOleInPlaceUIWindow pUIWindow, int fRameWindow)
{

}

void IDocHostUIHandler.ShowContextMenu(uint dwID, ref
MsHtmHstInterop.tagPOINT ppt, object pcmdtReserved, object pdispReserved)
{
throw new COMException("", 0);
}

void IDocHostUIHandler.ShowUI(uint dwID, IOleInPlaceActiveObject
pActiveObject, IOleCommandTarget pCommandTarget, IOleInPlaceFrame pFrame,
IOleInPlaceUIWindow pDoc)
{

}

void IDocHostUIHandler.TranslateAccelerator(ref tagMSG lpmsg, ref Guid
pguidCmdGroup, uint nCmdID)
{

}

void IDocHostUIHandler.TranslateUrl(uint dwTranslate, ref ushort pchURLIn,
IntPtr ppchURLOut)
{

}

void IDocHostUIHandler.UpdateUI()
{

}

private void OnDocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{

this.Parent.Cursor = System.Windows.Forms.Cursors.Default;

ICustomDoc cDoc = (ICustomDoc)this.axWebBrowser.Document;
cDoc.SetUIHandler((IDocHostUIHandler)this);

// Get the document.
IHTMLDocument2 doc = (IHTMLDocument2)this.axWebBrowser.Document;

if (DocumentCompleteEvent!=null)
DocumentCompleteEvent(this, new
DocUIResponseArgs(doc.body.getAttribute("appref",0).ToString(),doc.body.getA
ttribute("appdata",0).ToString()));

foreach (DocUIHandler _h in this._delegatemap)
{
object _name = _h.AppRef;

mshtml.HTMLButtonElement _a =
(mshtml.HTMLButtonElement)doc.all.item(_name,null);
((mshtml.HTMLButtonElementEvents2_Event)_a).onclick += new
HTMLButtonElementEvents2_onclickEventHandler(this.Button_onclick);

}
}

public void RegisterUIHandler(string _appref,DocUIResponseHandler
_handler)
{
this._delegatemap.Add(new DocUIHandler(_appref,_handler));
}

public void RemoveUIHandlers()
{

this._delegatemap.Clear();

}

public delegate bool BrowserClickHandler( IHTMLEventObj e );

private bool Button_onclick(IHTMLEventObj e)
{
if (this.InvokeRequired == true)
{
this.BeginInvoke( new BrowserClickHandler(
this.Button_onclick ), new object[] { e } );

}
else
{
string _appref = e.srcElement.id;
if (_appref.Length>0)
{
foreach (DocUIHandler _h in this._delegatemap)
{
if (_h.AppRef == _appref)
{
e.cancelBubble = true;
_h.Handler(this,new
DocUIResponseArgs(e.srcElement.getAttribute("appref",0).ToString(),e.srcElem
ent.getAttribute("appdata",0).ToString()));
break;
}
}


}
}

return false;
}

}
 
Brian,

In order to do this, you are going to have to create an instance of
InternetExplorer. Once you have that, you will have to connect to the
IWebBrowser2 interface, and then get the IHTMLDocument2 instance of the
document hosted in the browser. With that, you can access elements of the
DOM for the page that is being viewed, and subsequently manipulate it, etc,
etc.

To access internet explorer, you have to set a reference in COM to
SHDocVw.dll (Microsoft Internet Controls). Also, you will have to set a
reference to MSHTML.tlb, which there should already be a managed wrapper for
under the .NET tab in the references dialog, under Microsoft.mshtml.


Hope this helps.
 
Nicholas,

Why?

The OP said he just wants to open his page in IE passing in the parameters,
he said nothing about the browser being imbedded on his form.

My solution may not specifically open IE but it will open the default
browser.


Regards
Brian W


Nicholas Paldino said:
Brian,

In order to do this, you are going to have to create an instance of
InternetExplorer. Once you have that, you will have to connect to the
IWebBrowser2 interface, and then get the IHTMLDocument2 instance of the
document hosted in the browser. With that, you can access elements of the
DOM for the page that is being viewed, and subsequently manipulate it, etc,
etc.

To access internet explorer, you have to set a reference in COM to
SHDocVw.dll (Microsoft Internet Controls). Also, you will have to set a
reference to MSHTML.tlb, which there should already be a managed wrapper for
under the .NET tab in the references dialog, under Microsoft.mshtml.


Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian F said:
Exactly what the subject says.
I have an ASP page that I want my C# windows application to open in
Internet Explorer, and if possible have it send along a list of values
from a list box. Thank you.
 
Brian,

My assumption is that when the OP said "pass it data" it meant to pass
it data from the app to a form on the page. If it is a form, then more
likely than not, it is going to be submitted with a POST, and not a get, in
which case, you can not just append the values to the URL. You would have
to automate IE in order to populate the form and submit it through an
external instance of IE.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian W said:
Nicholas,

Why?

The OP said he just wants to open his page in IE passing in the parameters,
he said nothing about the browser being imbedded on his form.

My solution may not specifically open IE but it will open the default
browser.


Regards
Brian W


message news:[email protected]...
Brian,

In order to do this, you are going to have to create an instance of
InternetExplorer. Once you have that, you will have to connect to the
IWebBrowser2 interface, and then get the IHTMLDocument2 instance of the
document hosted in the browser. With that, you can access elements of the
DOM for the page that is being viewed, and subsequently manipulate it, etc,
etc.

To access internet explorer, you have to set a reference in COM to
SHDocVw.dll (Microsoft Internet Controls). Also, you will have to set a
reference to MSHTML.tlb, which there should already be a managed wrapper for
under the .NET tab in the references dialog, under Microsoft.mshtml.


Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian F said:
Exactly what the subject says.
I have an ASP page that I want my C# windows application to open in
Internet Explorer, and if possible have it send along a list of values
from a list box. Thank you.
 
Hi Nicholas,

I think that Brian's assumption is the correct one, the OP said an ASP
page, not an ASP.NET page, in ASP is usually used request["VARNAME"] and the
request object iterate in both the GET and POST collection ( do not remember
if those are the correct name ) so I think that his solution will do the
trick here.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nicholas Paldino said:
Brian,

My assumption is that when the OP said "pass it data" it meant to pass
it data from the app to a form on the page. If it is a form, then more
likely than not, it is going to be submitted with a POST, and not a get, in
which case, you can not just append the values to the URL. You would have
to automate IE in order to populate the form and submit it through an
external instance of IE.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian W said:
Nicholas,

Why?

The OP said he just wants to open his page in IE passing in the parameters,
he said nothing about the browser being imbedded on his form.

My solution may not specifically open IE but it will open the default
browser.


Regards
Brian W


message news:[email protected]...
Brian,

In order to do this, you are going to have to create an instance of
InternetExplorer. Once you have that, you will have to connect to the
IWebBrowser2 interface, and then get the IHTMLDocument2 instance of the
document hosted in the browser. With that, you can access elements of the
DOM for the page that is being viewed, and subsequently manipulate it, etc,
etc.

To access internet explorer, you have to set a reference in COM to
SHDocVw.dll (Microsoft Internet Controls). Also, you will have to set a
reference to MSHTML.tlb, which there should already be a managed
wrapper
for
under the .NET tab in the references dialog, under Microsoft.mshtml.


Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Exactly what the subject says.
I have an ASP page that I want my C# windows application to open in
Internet Explorer, and if possible have it send along a list of values
from a list box. Thank you.
 
Ignacio,

Perhaps we should ask the OP? If they know that the request mechanism
uses GET in the HTTP protocol, then Brian's suggestion will work. However,
if it requires a POST, then my suggestion will have to be used, as the OP
will have to call the Navigate method to pass the POST parameters involved.

Also, whether or not it is an ASP page, ASP.NET, PERL, etc, etc, doesn't
matter. ASP doesn't lock you into using GET or POST exclusively for getting
data to the server from the client.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Ignacio Machin ( .NET/ C# MVP ) said:
Hi Nicholas,

I think that Brian's assumption is the correct one, the OP said an ASP
page, not an ASP.NET page, in ASP is usually used request["VARNAME"] and the
request object iterate in both the GET and POST collection ( do not remember
if those are the correct name ) so I think that his solution will do the
trick here.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

message news:[email protected]...
Brian,

My assumption is that when the OP said "pass it data" it meant to pass
it data from the app to a form on the page. If it is a form, then more
likely than not, it is going to be submitted with a POST, and not a get, in
which case, you can not just append the values to the URL. You would have
to automate IE in order to populate the form and submit it through an
external instance of IE.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian W said:
Nicholas,

Why?

The OP said he just wants to open his page in IE passing in the parameters,
he said nothing about the browser being imbedded on his form.

My solution may not specifically open IE but it will open the default
browser.


Regards
Brian W


"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message Brian,

In order to do this, you are going to have to create an instance of
InternetExplorer. Once you have that, you will have to connect to the
IWebBrowser2 interface, and then get the IHTMLDocument2 instance of the
document hosted in the browser. With that, you can access elements
of
the
DOM for the page that is being viewed, and subsequently manipulate it,
etc,
etc.

To access internet explorer, you have to set a reference in COM to
SHDocVw.dll (Microsoft Internet Controls). Also, you will have to
set
a
reference to MSHTML.tlb, which there should already be a managed wrapper
for
under the .NET tab in the references dialog, under Microsoft.mshtml.


Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Exactly what the subject says.
I have an ASP page that I want my C# windows application to open in
Internet Explorer, and if possible have it send along a list of values
from a list box. Thank you.
 
I didn't mean to start a holy-war. ;)

The only thing that is clear here is that we both interpreted the OP
differently, as such, we both answered appropriately given our differing
interpretations.

I know better that to second-guess Nicholas, I was just trying to understand
why he answered the way he did, and now I know ;)

Regards
Brian W

Nicholas Paldino said:
Brian,

My assumption is that when the OP said "pass it data" it meant to pass
it data from the app to a form on the page. If it is a form, then more
likely than not, it is going to be submitted with a POST, and not a get, in
which case, you can not just append the values to the URL. You would have
to automate IE in order to populate the form and submit it through an
external instance of IE.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian W said:
Nicholas,

Why?

The OP said he just wants to open his page in IE passing in the parameters,
he said nothing about the browser being imbedded on his form.

My solution may not specifically open IE but it will open the default
browser.


Regards
Brian W


message news:[email protected]...
Brian,

In order to do this, you are going to have to create an instance of
InternetExplorer. Once you have that, you will have to connect to the
IWebBrowser2 interface, and then get the IHTMLDocument2 instance of the
document hosted in the browser. With that, you can access elements of the
DOM for the page that is being viewed, and subsequently manipulate it, etc,
etc.

To access internet explorer, you have to set a reference in COM to
SHDocVw.dll (Microsoft Internet Controls). Also, you will have to set a
reference to MSHTML.tlb, which there should already be a managed
wrapper
for
under the .NET tab in the references dialog, under Microsoft.mshtml.


Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Exactly what the subject says.
I have an ASP page that I want my C# windows application to open in
Internet Explorer, and if possible have it send along a list of values
from a list box. Thank you.
 
Back
Top