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

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();

Related

UserControl is blank when added to panel (sometimes)

I have an App written in .Net Framework 4.7.2 that sporadically, when a new user control is needed to be added to a flow panel, it's added successfully (and bumped to the top of the panel), but the user control is blank. Here's how I create it:
int shortcutNumber = NextShortcutNumber();
ctrlIssue newIssueCtrl = new ctrlIssue(issueData, shortcutNumber);
newIssueCtrl.IssueDeleted += new System.EventHandler(this.ctrlIssue_IssueDeletedHandler);
newIssueCtrl.Visible = false;
panIssues.Controls.Add(newIssueCtrl);
panIssues.Controls.SetChildIndex(newIssueCtrl, 0); // Always put it at the top
newIssueCtrl.Visible = true;
newIssueCtrl.Target();
I've diagnosed it down to that Control.Created is set to false. So ControlCreated is not called? I'm trying a workaround now like so:
if (!newIssueCtrl.Created)
{
newIssueCtrl.CreateControl();
}
We'll see if it works, but it's weird. Does anyone have any insight on why ControlCreated wouldn't be called? It seems sporadic and inconsistent, when it starts happening, it happens about 80% of the time.
Thanks!
EDIT: There was an ask to see the ctrlIssue constructor. There's fancy in it:
public ctrlIssue(Issue data, int shortcut)
{
InitializeComponent();
mIssueData = data;
chkPlayPause.TabIndex = 2;
lblProject.Text = mIssueData.Fields.Project.Name;
chkPlayPause.Text = mIssueData.Key;
lblSummary.Text = mIssueData.Fields.Summary;
InitializeToolTips();
UpdateShortcut(shortcut);
mSummaryFontRegular = lblSummary.Font;
mSummaryFontHover = new Font(mSummaryFontRegular, FontStyle.Underline);
mProjectFontRegular = lblProject.Font;
mProjectFontHover = new Font(mProjectFontRegular, FontStyle.Underline);
}

Load a silverlight to aspx page through code behind

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

Controls added dynamically don't display on my form

I´m adding controls dynamically onto my form, but I can't see those new controls.
My code is:
frmFormulario myform = new frmFormulario();
for (int i = 0; i < elements.Count; i++)
{
String nm = elements[i].name;
String chk = "chk"+nm;
CheckBox checkboxWS = new CheckBox();
checkboxWS.Name = chk;
checkboxWS.Checked = true;
checkboxWS.Visible = true;
checkboxWS.Width.Equals(40);
myform.Controls.Add(checkboxWS);
myform.Controls.SetChildIndex(checkboxWS, 0);
}
Can anybody spot the problem?
Thanks
My environment is C# Visual Studio 2010
Try adding
myform.Show(); //or myform.ShowDialog;
after the for loop. If you want to completely work on this new window (myform) and discard the former or parent then try creating it in a new thread.
Hope it helps.
You are creating a new form inside of the loop as well as the controls. Once the loop is done it falls out of scope. I'm sure you didn't want to do that, rather add them to an existing form.
Where do you show myform, the instance of frmFormulario?
Currently you are creating a new form and you don't display it.

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.

Creating Images as buttons in code in C# WPF

I want to create an image as a button in code in C# WPF (not a button with BG image but an actual image). I read on this site to use a PictureBox for the image, and I've found that the WPF equivalent is Image. The problem is, that while i've found PictureBox has a .Click that you can set, Image does not. The two things I want to do are:
Create an array of buttons that are images and can be clicked.
Have an image for the unclicked and clicked states of the button.
Is there anything right in front of me I'm missing?
Here is my loop creating the buttons:
sideBarButtons = new Button[infoLoader.categoriesLength];
sideButtons = new Image[infoLoader.categoriesLength];
ImageBrush[] myBg = new ImageBrush[infoLoader.categoriesLength];
for (int i = 0; i < sideBarButtons.Length; i++)
{
myBg[i] = new ImageBrush();
myBg[i].ImageSource = new BitmapImage(graphicLoader.buttonUnselected[(i % myBg.Length)]);
/*sideBarButtons[i] = new Button();
sideBarButtons[i].Content = infoLoader.categories[i].name;
sideBarButtons[i].Background = myBg[i];
//sideBarButtons[i].BorderThickness = ;
sideBarButtons[i].Width = 155;
sideBarButtons[i].Height = 46;
Canvas.SetLeft(sideBarButtons[i], 30);
Canvas.SetTop(sideBarButtons[i], 10 + (46 * i));
sideBarButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideBarButtons[i]);*/
BitmapImage myBmp = new BitmapImage();
myBmp.BeginInit();
myBmp.UriSource = myBg[i];
myBmp.EndInit();
sideButtons[i] = new Image();
sideButtons[i].Source = myBmp;
sideButtons[i].Width = 155;
sideButtons[i].Height = 46;
Canvas.SetLeft(sideButtons[i], 30);
Canvas.SetTop(sideButtons[i], 10 + (46 * i));
sideButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideButtons[i]);
}
The first commented out area is when I was creating buttons with buttons and not images, while the second is images and it doesn't work. Thanks in advance.
Two options here:
1.) Instead of using the Click event, which doesn't exist on Image, use MouseDown, which does.
2.) Instead of using Images and repurposing them, use Buttons with a custom style on it. Then you can handle the button's click.
Personally, I'd use the latter, but really either works.

Categories

Resources