Load a silverlight to aspx page through code behind - c#

I need to load a silverlight application in a portion of an aspx page on a button click on that page page. Some init parameters need to be passed to the silverlight application based on the user inputs on the host page on button click. How to do that?
I presume I need to create the silverlight object from code-behind to set custom InitParameters. Any idea how to do that?

Extending to what is mentioned here, you can do something like this:
HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");
myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";
myHtmlObject.Attributes["type"] = "application/x-silverlight-2";
myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
this.Page.Controls.Add(myHtmlObject);
HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApplication.xap";
myHtmlObject.Controls.Add(mySourceParam);
HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "onError";
myOnErrorParam .Attributes["value"] = "onSilverlightError";
myHtmlObject.Controls.Add(myOnErrorParam);
HtmlGenericControl myInputParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "InitParameters";
myOnErrorParam .Attributes["value"] = "param1=Hello,param2=World";
myHtmlObject.Controls.Add(myInputParam);
this.Page.Controls.Add(myHtmlObject);

Related

I Can't get a pic loaded onto my web form

I'm new to developing with C# and asp.net. I am trying to laod five images onto my web form I also need to do it code behind and not just drag and drop. I can't get them to work. I have tried:
dicePic[i] = new System.Web.UI.WebControls.Image();
//dicePic[0] = Image.FromFile(HttpContext.Current.Server.MapPath("/images/1.gif"));
I also set the properties like this:
dicePic[0].ID = "Dice1";
dicePic[0].Style["position"] = "absolute";
dicePic[0].Style["top"] = "80px";
dicePic[0].Style["left"] = "80px";
dicePic[0].Visible = true;
dicePic[0].Width = 50;
dicePic[0].Height = 50;
dicePic[0].ImageUrl = "~/images/1.gif";//HttpContext.Current.Server.MapPath("/images/1.gif");
dicePic[0].Controls.Add(dicePic[0]);
Any help would be great. Thanks
replace this line :dicePic[0].Controls.Add(dicePic[0]); by this.Controls.Add(dicePic[0]);
You should add it to a container in your page (or to the page itself).
if you are doing this inside a Page put
this.Controls.Add(dicePic[i]);
after
dicePic[i] = new System.Web.UI.WebControls.Image();

custom data-attribute in asp.net Linkbutton

can i use custom attributes with linkbutton?
like this:
var linkb= new LinkButton();
linkb.ID = "myLinkButton";
linkb.Text = "x";
linkb.Click += linkbuttonClicked;
linkb.Attributes["data-id"] = "DataId";
linkb.CssClass = "lb";
MyPanel.Controls.Add(linkb);
Can i do something like this onClick?
var thisButton= (LinkButton) sender;
var test = thisButton.Attributes["data-id"]);
As far as I know, you can do that in ASP.NET 4.0, the attribute and value will be saved in the viewstate and persist across postbacks.
But, as it persists only in the viewstate, if you change its value client-side with some js/jquery, the new value will not be sent to the server on postback and will not persist.

Tabs in WebForms

I'm trying to create a set of tabs at runtime and they were, for a time, working correctly- however now they just seem to display the contents of all tabs underneath the previously rendered tab. As silly as it seems, as far as I can tell, in terms of code nothing has changed since the code was working so I am a little confused as to what is going on...
For the purpose of this example I will not use a loop, although this seems irrespective as the loop doesn't appear to be the problem.
<asp:Panel runat="server" ID="pnlTabs">
<ul runat="server" id="ulSections" />
</asp:Panel>
I've come to the conclusion that my code must be at fault so here is the C#:
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = Tab1;
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);
I place my controls and what not on the "pnl". Can anyone please tell me what my mistake is?
//THIS ADDS THE <A> to the <LI>
liTab.Controls.Add(anTab);
//THIS ADDS THE <LI> to the <UL>
ulSections.Controls.Add(liTab);
//THIS CODE Appear to have nothing to do with anything.
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);
Can you confirm if the HTML is even being rendered in the html when you View Source.
Where in your code behind is this code... in a button click, on Page Load, On Init ?
I discovered what the problem was, it was basically that the InnerText of "anTab" cannot be the same as the href attribute of "anTab" as this causes confusion and results in the content from all tabs to appear only in the first (selected) tab, while the remaining tabs don't function.
For example, the following code works correctly as the InnerText of "anTab" is "Tab 1" and the href is looking for a control with the ID "Tab1", which is the ID of the panel.
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab 1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);
Below we can see that the InnerText of "anTab" is the same as the href and the ID of the panel we wish to use as the tab, this causes the confusion as the href now assumes you want the "tab click" to point to itself instead of the actual panel.
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);

creating RadEditor dynamically asp.net

I want to add RadEditor dynamically. It is getting added to the page but as soon as postback occurs i get Multiple controls with the same ID 'EditorRibbonBarResourcesHolder' were found. Below is the code that i am using to add control dynamically.
RadEditor editor = new RadEditor();
editor.ID = "editor_" + itemTypeattribute.ItemAttributeID + rand.Next();
cellAttributeValue.Controls.Add(editor);
editor.DialogOpener.ID = "editor_dialopOpener_" + itemTypeattribute.ItemAttributeID;
editor.DialogOpener.Window.ID = "editor_dialopOpener_window_"+ ItemTypeattribute.ItemAttributeID;
editor.ClientIDMode = ClientIDMode.AutoID;
editor.EnableEmbeddedScripts = true;
editor.Height = 200;
Any help is appreciated. Thanks
Whenever I need to use a radeditor "dynamically" I have it on the page from the start with visible=false and then show it when I need it.

How to Dynamically Create Tabs

This is in C#
I Need to basically make TabPages from a textbox.Text so for example:
textBox1.Text = "test";
TabPage textBox1.Text = new TabPage();
That is what i want to do.. i know that won't work directly, but that should give you the idea of how i want to create the tabPages.. then i want to be able to call them later on too so for example:
String browser = "browser 1";
(textBox1.Text as TabPage).Controls.Add(WebBrowser browser)
I need all the names to be dynamic because what this will be is a program that can run tests for customer accounts There would be a TabControl which has the "Account Number as the tabPage control name and then inside each of those tabPages would be another TabControl with a set up tabs with each invidivual test in it's own tab. So Tabs within Tabs basically.
Make it look similar to this:
var page = new TabPage(textBox1.Text);
var browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
page.Controls.Add(browser);
tabControl1.TabPages.Add(page);
browser.Navigate("http://stackoverflow.com");
page.Select();
Actually one other thing i want to know, How can i call on this Tab # another time outside of this function?
This is basically what i turned out to look like.
String browserName = "Test Check";
var tabPageName = new TabPage(textBox1.Text);
var tabPageBrowser = new TabPage(browserName);
var tabPageTabControl = new TabControl();
var browser = new WebBrowser();
tabPageName.Controls.Add(tabPageTabControl);
tabPageTabControl.TabPages.Add(tabPageBrowser);
tabPageBrowser.Controls.Add(browser);
mainTabControl.TabPages.Add(tabPageName);
mainTabControl.SelectedTab = tabPageName;

Categories

Resources