jeditable in an ASP.NET web form - c#

Can someone explain to me how you would use jeditable with an ASP.NET web form (and C# codebehind). I've got a bit of experience with web forms but not very complicated stuff, and haven't used much jquery before, and this is just puzzling me. I understand how to put it in and attach it to the element you want to be editable, it's what jeditable does when you submit the text field that I don't get. How do you handle that in the webform in order to save the changed text? Hope someone understands my issue... Cheers!

There are lots of ways to handle the POST that jEditable sends. I went with a very simple one. I made a new .aspx file and pointed jEditable to that. In there, you can access the form's POSTed fields with this.Request.Form["..."] to do whatever you intend to do. Here's a snippet:
protected override void OnLoad(EventArgs e)
{
this.Response.Clear();
this.Response.Cache.SetNoStore();
this.Response.Cache.SetExpires(DateTime.Now);
this.Response.StatusCode = 200;
try
{
var postId = this.Request.Form["id"];
var value = this.Request.Form["value"];
this.Response.Write(value);
switch (postId)
{
case "id1":
// write 'value' to DB or whatever
break;
case "id2":
// write 'value' to DB or whatever
break;
default:
this.Response.StatusCode = 501; // Not Implemented
}
this.Response.End();
}
}

Related

Telerik - Can I load a report definition from Database

I am working on a WebApplication, and including a Telerik-Report in it. The normal way as described works well.
Now I'm trying to load the report definition from a Database, instead of having a file (request from the boss). So far, I've made it work with a temp-file, code is below. But this is far from nice coding.
My question: can I somehow give a string or stream to the report (instead of a file)?
My current code looks like this:
private readonly string __path = "C:\\my-temp-directory\\Reports\\";
protected void Page_Load(object sender, EventArgs e) {
string contents = /** Get XML from Database **/;
File.WriteAllText(__path + "temp.trdx", contents); // <-- This file I want to omit.
if (!IsPostBack) {
this.reportViewer1.ReportSource.Identifier = "temp.trdx";
this.reportViewer1.ReportSource.IdentifierType = IdentifierType.TypeReportSource;
}
}
Thanks.
Well, the report definition is just an XML so it doesn't really matter where you will obtain it from. Looking at the code I think it won't work, because you have a file, but are using TypeReportSource instead of UriReportSource when setting the IdentifierType.
In this scenario I think you should go with the CustomReportSource. The IdentifierType can be TypeReportSource, UriReportSource and CustomReportSource. The first two won't work in your case, because you don't know the report type and you also do not want to save it to a file. A CustomReportSource will allow you to put your own logic that will fetch the report from the database and send it to the engine. Actually there is a docs article that fits exactly your scenario:
How to Implement a Custom Report Source Resolver

What is the correct way to set up and display a JavaScript alert message in ASP .NET?

I'm completely new to working with JavaScript in ASP .NET so bear with me.
Say I have the following:
protected void btnCreateReplication_Click(object sender, EventArgs e)
{
try
{
doSomething();
}
catch (Exception ex)
{
//How do I display the error? I know if I were in WinForms, I could just do this:
MessageBox.Show(ex.Message);
}
}
First question:
Should I put all of my JavaScript code in a .js file in my ASP .NET solution?
If so, how do I call an alert message from the .js file?
If not, how do I call an alert message instead of the MessageBox.Show?
You'll have to use the Page.ClientScript.RegisterClientScriptBlock method.
string script = "alert('We had an error')";
Page.ClientScript.RegisterClientScriptBlock(GetType(),
"myErrorKey", script, true);
RegisterClientScriptBlock takes a type, a key to see if the script block is already registered, the script, and a boolean indicating whether the method should add the script tags, or if you're string script variable already includes them.
as Dave said, using the tScript.RegisterClientScriptBlock method, you can inject the javascript into your page and have it run.
If you want to make the alert a bit more complex, it is cleaner to use a StringBuilder to set up the script.
Here, a link! http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
Another helpful way to show JavaScript alerts would be to use OnClientClick in .ASPX page. This is similar to OnClick for ASP Classic if you are familiar with ASP.
OnClientClick="alert('Are you sure you want to delete this user?');"
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx
This may not be helpful for you for above code but can use to show other simple alerts.

I must be a heretic for wanting a C# browser with both NewWindow2 and GetElementsByTagName

You can't have your cake and eat it too, apparently.
I'm currently using the System.Windows.Forms.WebBrowser in my application. The program currently depends on using the GetElementsByTagName function. I use it to gather up all the elements of a certain type (either "input"s or "textarea"s), so I can sort through them and return the value of a specific one. This is the code for that function (my WebBrowser is named web1):
// returns the value from a element.
public String FetchValue(String strTagType, String strName)
{
HtmlElementCollection elems;
HtmlDocument page = web1.Document.Window.Frames[1].Document;
elems = page.GetElementsByTagName(strTagType);
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("name") == strName ||
elem.GetAttribute("ref") == strName)
{
if (elem.GetAttribute("value") != null)
{
return elem.GetAttribute("value");
}
}
}
return null;
}
(points to note: the webpage I need to pull from is in a frame, and depending on circumstances, the element's identifying name will be either in the name or the ref attribute)
All of that works like a dream with the System.Windows.Forms.WebBrowser.
But what it is unable to do, is redirect the opening of a new window to remain in the application. Anything that opens in a new window shoots to the user's default browser, thus losing the session. This functionality can be easily fixed with the NewWindow2 event, which System.Windows.Forms.WebBrowser doesn't have.
Now forgive me for being stunned at its absence. I have but recently ditched VB6 and moved on to C# (yes VB6, apparently I am employed under a rock), and in VB6, the WebBrowser possessed both the GetElementsByTagName function and the NewWindow2 event.
The AxSHDocVw.WebBrowser has a NewWindow2 event. It would be more than happy to help me route my new windows to where I need them. The code to do this in THAT WebBrowser is (frmNewWindow being a simple form containing only another WebBrowser called web2 (Dock set to Fill)):
private void web1_NewWindow2(
object sender,
AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
frmNewWindow frmNW = new frmNewWindow();
e.ppDisp = frmNW.web2.Application;
frmNW.web2.RegisterAsBrowser = true;
frmNW.Visible = true;
}
I am unable to produce on my own a way to replicate that function with the underwhelming regular NewWindow event.
I am also unable to figure out how to replicate the FetchValue function I detailed above using the AxSHDocVw.WebBrowser. It appears to go about things in a totally different way and all my knowledge of how to do things is useless.
I know I'm a sick, twisted man for this bizarre fantasy of using these two things in a single application. But can you find it in your heart to help this foolish idealist?
I could no longer rely on the workaround, and had to abandon System.Windows.Forms.WebBrowser. I needed NewWindow2.
I eventually figured out how to accomplish what I needed with the AxWebBrowser. My original post was asking for either a solution for NewWindow2 on the System.Windows.Forms.WebBrowser, or an AxWebBrowser replacement for .GetElementsByTagName. The replacement requires about 4x as much code, but gets the job done. I thought it would be prudent to post my solution, for later Googlers with the same quandary. (also in case there's a better way to have done this)
IHTMLDocument2 webpage = (IHTMLDocument2)webbrowser.Document;
IHTMLFramesCollection2 allframes = webpage.frames;
IHTMLWindow2 targetframe = (IHTMLWindow2)allframes.item("name of target frame");
webpage = (IHTMLDocument2)targetframe.document;
IHTMLElementCollection elements = webpage.all.tags("target tagtype");
foreach (IHTMLElement element in elements)
{
if (elem.getAttribute("name") == strTargetElementName)
{
return element.getAttribute("value");
}
}
The webbrowser.Document is cast into an IHTMLDocument2, then the IHTMLDocument2's frames are put into a IHTMLFramesCollection2, then I cast the specific desired frame into an IHTMLWindow2 (you can choose frame by index # or name), then I cast the frame's .Document member into an IHTMLDocument2 (the originally used one, for convenience sake). From there, the IHTMLDocument2's .all.tags() method is functionally identical to the old WebBrowser.Document.GetElementsByTagName() method, except it requires an IHTMLElementCollection versus an HTMLElementCollection. Then, you can foreach the collection, the individual elements needing to be IHTMLElement, and use .getAttribute to retrieve the attributes. Note that the g is lowercase.
The WebBrowser control can handle the NewWindow event so that new popup windows will be opened in the WebBrowser.
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
// navigate current window to the url
webBrowser1.Navigate(webBrowser1.StatusText);
// cancel the new window opening
e.Cancel = true;
}
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/361b6655-3145-4371-b92c-051c223518f2/
The only solution to this I have seen was a good few years ago now, called csExWb2, now on Google code here.
It gives you an ExWebBrowser control, but with full-on access to all the interfaces and events offered by IE. I used it to get deep and dirty control of elements in a winforms-hosted html editor.
It may be a bit of a leap jumping straight into that, mind.

C# MultiView SetActiveView by ID not working properly

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;
}
}

Using PlotKit (javascript) through C#

I'm relatively new to Javascript, and although I know how to use it, I don't really understand the mechanics behind it. Bear with me here.
I need to write a small app that creates a chart (in SVG) based on data I take in as an XML file. I found PlotKit, which does exactly what I need, except that it's written in Javascript, while my current program is written in c#. I did some googling and found a few articles which explain how to evaluate simple Javascript code using the .NET VsaEngine class. Unfortunately, I have absolutely no idea how to use the VsaEngine to execute more complicated Javascript that requires references to other files. Basically, all I want is for c# to be able to call something like this as Javascript:
var layout = new PlotKit.Layout("bar", {});
layout.addDataset("data", [[0, 0], [1, 1], [2, 2]]);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SVGRenderer(canvas, layout, {});
var svg = SVGRenderer.SVG();
And get back the SVG string for the chart. I have no idea how to make it so that the above script knows where to look for all of the necessary objects. If I were to make a web page to do this, I would just add a few script headers referencing /plotkit/Layout.js, /plotkit/Canvas.js, etc., the Javascript would work fine.
If anyone could explain exactly how I would use PlotKit through C#, or could explain a more effective way to do this, I would really appreciate it.
EDIT: I realize I wasn't too clear with this question - I need my c# program to emulate a Javascript engine and use the PlotKit library without actually running a web browser. Is there any way to do this?
PlotKit is a JavaScript library that is intended to execute in the Client's Web Browser. C# is executed on the Server. To go about communicating between the two, you would render whatever data you wish to pass to PlotKit on the server and then output it in the HTML you send to the client.
So in your C# codebehind you would construct the JSON object that would be passed to PlotKit's addDataset method.
...
public partial class Default : System.Web.UI.Page
{
protected string PlotKitData = "[]";
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) PlotKitData = GenerateJSON();
...
Then in your ASPX codefront you would have something like this.
<script>
var layout = new PlotKit.Layout("bar", {});
layout.addDataset("data", <%=PlotKitData%>);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SVGRenderer(canvas, layout, {});
var svg = SVGRenderer.SVG();
</script>
Perhaps ZedGraph might suit your needs instead?

Categories