Creating Dynamic Menus - calling extern Function - c#

I got a requirement to create a dynamic menus using extern function so, we can consume it anywhere, following is the complete requirement :
Please create a dynamic horizontal link menu in the top of the Master Page. This will be the menu that is displayed on every page. We need to be able to set the links on this page from the code behind. The information we want to set is the link text, and the link path (href). The idea behind this is that based on who is logged into the application, and what page they are on, there will be different link possibilities. We should make this code reusable. The procedure to actually lay out the links would look something like this:
public static extern void SetDynamicLinks(Control ContainingControl, string[] arLinkTitles, string[] arLinks) { …code… }
We would call a procedure from the Page_Load of the Master Page that would decide what links we need to display. This procedure would be application dependant. This procedure would then call the "SetDynamicLinks" procedure mentioned above passing it the required parameters to make the correct links in the passed container control.
Any help for above, will be most appreciated. Thanks in advance.

The question does not make sense, if that is what you have been asked to do, then you would assume that either:
that there is already a SetDynamicLinks method developed by somebody else and lives in an unmanaged external dll, hence you would use extern to call that..
or, they are asking you to create that unmanaged dll in say C++, that will contain the SetDynamicLinks method and then can be called by other people using extern..

Related

using static slideshowextender method, for multiple users

I am basically trying to display a set of images stored on a webserver to users via Ajax SlideShowExtender.
I am just wondering how best to approach this, since the function it uses to retrieve the images to display, is a static method. But this site can be used concurrently by 3 different accounts, each of which will have their own set of images to display.
I have always stayed away from static variables and methods whenever doing websites if possible when working with users and individual data.I am worried there will be issues with the slides returned to each account being mixed up due to method being static.
Now obviously I will have to test this fully and check if this will be happening, but is there a better approach to this, or a way to ensure each user is always working with their own set of slides? I was thinking of passing an argument to the method perhaps for each user, but not sure how I would use this to ensure each user gets correct images? See below for basic function that returns slides to slideshowextender:
[WebMethod]
[ScriptMethod]
public static Slide[] GetImages(int userID)
{
List<Slide> slides = new List<Slide>();
/* here there is a try catch to read all images from specific directory and add to slides[] */
return slides.ToArray()
}

Any way to use object code in a User Control as a template for a new Object in a Sharepoint 2010 web part?

I'm trying to do something my teacher says can't be done; I would like to prove him wrong.
In the CreateChildControls method of my SharePoint 2010 webpart, I am referencing a User Control file called "ChartUserControl.ascx" in my project that contains the ASP.NET code for a WebChartControl object configured just the way I want it. WebChartControl has an ID of "OrderQtyChart".
What I want to do is take the code from that UserControl and use it create a new WebChartControl, called "chart", with matching configuration. I'm trying to do this because there are callbacks etc. that need to be performed on the chart after it's created to actually populate it with chart-stuff.
So, my code:
WebChartControl chart;
protected override void CreateChildControls()
{
ChartUserControl userControl = new ChartUserControl();
// referencing file ChartUserControl.ascx as an object
chart = userControl.FindControl("OrderQtyChart") as WebChartControl;
// or
chart = (WebChartControl)userControl.FindControl("OrderQtyChart");
// Trying to tell the code to create 'chart' using the code defined in object
"OrderQtyChart" located in ChartUserControl.ascx
}
Or something like that. In either instance above, 'chart' will return null.
I'm trying to use the front end code of OrderQtyChart as a template for 'chart'; they're both the same type of object and I don't get any errors until I try to create 'chart' on my page, at which point I'm told it's null.
Is there a way to do this? It would save me a ton of time not to have to configure 'chart' completely at creation time. Even if I have to reference my front-end code for OrderQtyChart a different way.
Thanks.
[Edited 7/9 for clarity]
What you are trying to do seems very well possible and I assume your teacher did not understand your question correctly. Here are a few tips on how this is done:
Object A could be one of these:
A visual control such as label or textbox. In this case your will have to traverse the visual controls from parent to child by doing direct parent.FindControl("ObjectA");
It is an instance of a class. This might be a MyClass or a new textbox that is created by code. In this case you'll have to create a public property that has a getter which returns ObjectA. although you can use FindControl in case ObjectA is a UI component that is created and added dynamically at run-time. Otherwise, you'll have to stick with property.
FindControl will not traverse the parent to child hierarchy, so you'll have to do a recursive method in order to successfully find the ObjectA or if you have access to its direct parent, call FindControl on that. More info here: http://geekswithblogs.net/QuandaryPhase/archive/2009/05/06/asp.net-recursive-findcontrol-amp-extension-methods.aspx
Page life cycle plays an important role here, so make sure that you keep it in mind or you'll end up with a null reference that is not really caused by FindControl
Gah, never mind. I realized I can just call the user control directly and I'm seriously overcomplicating this.
That's a whole new question, so I'll just start a different thread.

Get elements by name IE8 on different tab (Javascript)

I am developing on a program in C# that automates inputting information into a website. This program uses a library (Coypu) that allows this and for the execution of Javascript to manipulate further.
The problem comes in two forms:
The website only uses names (not ids) for its objects and I must use IE8 for them. Therefore I cannot use document.getElementsByName('name'); because it's not supported in earlier versions of IE.
I found a workaround by utilizing the form that's on the site but am having trouble using it because the site has to click a link to create a second tab for itself, leaving me helpless in grabbing the form in the second tab. Using something like document.forms[formIndex].elements['elementsName']; (although the tab appears to have focus) only gives me the forms from the main tab.
Has anyone ever fixed this problem or found a workaround in Javascript? Also, I'm not sure if jQuery can be used in my scenario as the Javascript is a string passed in to a C# method. However, it may or may not work.
If the website you are testing has access to jQuery you can use Coypu to populate specific fields in their respective forms:
e.g.
void PopulateInputField(int formName, string fieldName, string fieldValue)
{
browser.ExecuteScript(string.Format("$('form[name='{0}'] > input[name='{1}']).val('{2}')", formName, fieldName, fieldIndex));
}
Note how the C# string passed to Coypu's ExecuteScript contains the jQuery.

(.net) Kind of design question

My typical application has a couple of textboxes, checkbuttons, radiobuttons, and so. I always want to load the settings the user used the last time when the program starts, and also want to save the settings as the users clicks "Save settings" or closes the application. When the user attempts to save the settings, I'll have to check each of the controls for input errors (sometimes they have to have a max length, other times only caps, other times other things, there isn't a rule for them all, everytime it'll be different), and only if everything's OK i'll let him save the options. If there is something wrong, no option is saved and my errorcontrol provider will pop up a description of the input type info that should be put in that control.
I've been designing this from scratch for all my projects, but it's being a pain to do it. So I'd thought maybe now was the time to do some library to help me. I thought initially that maybe it'd be a good idea to have all the controls on my form that are going to be part of this save/load process to have an attribute associated with them, something like this
public delegate bool InputIsOkHandler();
public class OptionsAttribute : Attribute {
public Control controlRef;
public InputIsOkHandler IsInputOk;
public string errorMessageToShowOnErrorProvider;
public OptionsAttribute(Control controlRef, InputIsOkHandler inputHandler, string errMessage) {
...
}
}
The main problem here is that when I declare the attribute on a given var:
[Options(...)]
TextBox textBox1 = new TextBox();
I'll get
Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.
So I guess this approach isn't the best one. What would you guys do in this situation? Would you use attributes? Would you use other mechanisms?
Thanks
Do you know that .NET already includes such a system since 2.0? See MSDN, CodeProject and this white paper from WestWind.
The Personalization and User Profiles supported in ASP.NET 2.0 can be a nice way to achieve your goal.
You can check this MSDN article for a overview Personalization in ASP.NET 2.0

ASP.NET MVC Data Feedback through User Controls

In our new product we have a few data entry fields that require you to search for the correct value to place in this field.
When you enter this data entry field I would like to provide a search option via something on the view (perhaps an Ajax implementation) and have it allow searching of the database for the value, but when the result returns I want to keep the existing values in place for other fields not affected by the search.
But I have no idea how to do this with MVC. Does anyone have any suggestions on how this should be done?
Write your data entry page in the
usual way for an ASP.NET MVC view.
Get it working without the Ajax
(e.g., submit works correctly when
you just type in the values, without
auto complete).
Write the prototype for a JavaScript method you'll called when the user performs a certain action (e.g. presses a key inside of a certain control). But this inside a script tag in your aspx page. Unfortunately, stack overflow seems to "sanitize" script tags in my example, so I can't demonstrate that part. But you're JavaScript prototype will look something like this:
function startAutoComplete() {
}
Now hook up the event handlers on the user interface control. You need to call the function you've just prototyped an appropriate event handlers for your application. From your description, it sounds like you might want to use onkeydown, but there are lots of events to choose from. You probably need to handle more than one event, as appropriate for your application.
So far, everything that we've done has been standard aspx and JavaScript. In this step, we'll do the only part of the whole process which is really different for ASP.NET MVC. You need to add an action to your controller which will be called (indirectly) by the JavaScript prototype you've just written. The action should accept appropriate input (e.g., a string representing the text from control, or something like that, as appropriate for your application) and return its results in JSON format. I'm going to show a really simple example here; feel free to substitute more complex code in your real application.
public ActionResult GetSuggestions(string searchText)
{
return Json(new {SearchText = searchText});
}
This example just returns a JavaScript object containing one property, which contains the value passed to the function. Like you said, you can write something more useful for your application.
Now you need to call this function in JavaScript. The URI will look something like:
http://localhost/mycontroller/GetSuggestions?searchText=Foo
It is possible to make Ajax calls without a JavaScript library, but much easier if you use jQuery or some other library which handles cross-browser compatibility issues for you. Since I happen to like jQuery, I'll demonstrate that. Let's update the startAutoComplete method we prototyped earlier:
function startAutoComplete() {
var searchText = $("#myeditorid").text();
$.getJSON("/mycontroller/GetSuggestions?searchText=" + searchText,
null,
autoCompleteResponse);
}
The first line uses jQuery to get the text in the control with the ID myeditorid. We'll pass this to the ASP.NET MVC action as the searchText argument, by appending it as a query string parameter.
The next line, which starts with $.getJSON calls a jQuery function which makes an Ajax call to the URI you specify. We pass an argument, autoCompleteResponse, which is the name of a JavaScript method to be called if the response from the Ajax call is successful. Now we have to write autoCompleteResponse.
function autoCompleteResponse(data) {
if (data.SearchText)
{
$("#myeditorid").text(data.SearchText);
}
}
This says, "If the data returned has a SearchText property, set the text of the control to that value." Again, do whatever is appropriate for your application with the data returned.

Categories

Resources