Create dynamic dragpanel in asp.net with c# - c#

I want to create ajax dragpanel dynamically and add the controls and panel dynamically.I used the below code for create the control dynamically.But I can`t able to drag it.
enter code here
protected void Page_PreInit(object sender, EventArgs e)
{
DragPanelExtender drg = new DragPanelExtender();
drg.ID = "drg_1";
drg.DragHandleID = "pnl_1";
drg.TargetControlID = "pnl_2";
Panel p1 = new Panel();
p1.ID = "pnl_1";
Panel p2 = new Panel();
p2.ID = "pnl_2";
Label l1 = new Label();
l1.Text = "First";
TextBox t = new TextBox();
t.Text = "Enter text";
p2.Controls.Add(l1);
p2.Controls.Add(t);
p1.Controls.Add(p2);
Page.Controls.Add(p1);
}
How to I create drag controls inside the panel

Related

How to retain new controls after PostBack?

I am creating a web app that allows the user to create their own forms. but once a control is added (for example a new label with its control) it will be deleted upon me trying to add another object to the form.
this is the code for the button that creates the new item.
protected void createFormButton_Click(object sender, EventArgs e)
{
////titles is the div id of where i want to insert the title label
var titlelabel = new Label();
titlelabel.Text = textboxForTitle.Text;
titles.Controls.Add(titlelabel);
controls.Add(titlelabel);
if (optionsDropdown.SelectedValue == "Checkbox")
{
//elements is the div id of where i want to insert the control
var newControl = new CheckBox();
newControl.CssClass = "checkbox";
newControl.Checked = true;
elements.Controls.Add(newControl);
controls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Textbox")
{
//elements is the div id of where i want to insert the control
var newControl = new TextBox();
newControl.Text = "this is some text on the new box";
newControl.CssClass = "form-control";
elements.Controls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Dropdown")
{
//elements is the div id of where i want to insert the control
var newControl = new DropDownList();
newControl.Items.Add("one");
newControl.Items.Add("two");
newControl.Items.Add("three");
newControl.CssClass = "form-control";
elements.Controls.Add(newControl);
}
}
how can I save the new controls, so with each button click they previous control does not get deleted on postback?
When adding controls dynamically they are deleted on each postback. Now if you follow the lifecycle of an asp page you will notice that the viewstate ( the variable that holds all of your data from the client side objects) comes after the page.init. So when working with dynamically added controls in asp you need to recreate then on each postback in the page.init event. Then the viewstate is loaded into thoses controls. The way i do it is i keep everycontrol created in a list(of control) in session and add them in the placeholder at the page.init
You could create a class-level variable which is a List of controls, rather than adding an item to the built-in controls collection. In your case you would need two, since you are adding controls both to the page and the elements div. These lists will persist across postbacks, so each time you can repopulate the page controls accordingly at the end of your method via AddRange.
NOTE: code untested.
List<Control> pageControls = new List<Control>();
List<Control> elementControls = new List<Control>();
protected void createFormButton_Click(object sender, EventArgs e)
{
////titles is the div id of where i want to insert the title label
var titlelabel = new Label();
titlelabel.Text = textboxForTitle.Text;
titles.Controls.Add(titlelabel);
pageControls.Add(titlelabel);
if (optionsDropdown.SelectedValue == "Checkbox")
{
//elements is the div id of where i want to insert the control
var newControl = new CheckBox();
newControl.CssClass = "checkbox";
newControl.Checked = true;
elements.Controls.Add(newControl);
pageControls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Textbox")
{
//elements is the div id of where i want to insert the control
var newControl = new TextBox();
newControl.Text = "this is some text on the new box";
newControl.CssClass = "form-control";
elementControls.Add(newControl);
}
else if (optionsDropdown.SelectedValue == "Dropdown")
{
//elements is the div id of where i want to insert the control
var newControl = new DropDownList();
newControl.Items.Add("one");
newControl.Items.Add("two");
newControl.Items.Add("three");
newControl.CssClass = "form-control";
elementControls.Add(newControl);
}
Controls.AddRange(pageControls);
elements.Controls.AddRange(elementControls);
}

create popup using silverlight and add a data grid to the popup

I need to add a grid to a popup in Silverlight project. I have added this code in the mainpage.xaml.cs file I have created a popup.
but I need to add a grid showing a table from the database.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
popup p = new Popup();Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5.0);
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.LightGray);
Button button1 = new Button();
button1.Content = "Close";
button1.Margin = new Thickness(2.0);
button1.Click += new RoutedEventHandler(button1_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "The popup control";
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
Grid grid1 = new Grid();
// i need to bind this grid to data from the sql database and attach this grid to the popup
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
p.DataContext =
// Set where the popup will show up on the screen.
p.VerticalOffset = 200;
p.HorizontalOffset = 300;
// Open the popup.
p.IsOpen = true;
}
The Grid is a layout control, and not for displaying tables. For tabular data, you'll need a DataGrid control.
List<Customer> customerList = new List<Customer>();
var dataGrid = new DataGrid();
dataGrid.ItemsSource = customerList;
Popup popup = new Popup();
popup.Child = dataGrid;
popup.IsOpen = true;

How to create drop down information box in C# Winforms?

I want to make a button that can drop down a multi-line label or form which contains help documentation for the user.
I have searched and I can't find anything that is for C# Winforms. Do any free controls out there exist for this or will I have to create it myself?
Many thanks,
Richard
Using ToolStripControlHost and ToolStripDropDown controls can provide this for you:
private void button1_Click(object sender, EventArgs e) {
var helpInfo = new StringBuilder();
helpInfo.AppendLine("This is line one.");
helpInfo.AppendLine("This is line two.");
var textHelp = new TextBox() { Multiline = true,
ReadOnly = true,
Text = helpInfo.ToString(),
MinimumSize = new Size(100, 100)
};
var toolHost = new ToolStripControlHost(textHelp);
toolHost.Margin = new Padding(0);
var toolDrop = new ToolStripDropDown();
toolDrop.Padding = new Padding(0);
toolDrop.Items.Add(toolHost);
toolDrop.Show(button1, button1.Width, 0);
}
Result:
I think it will be a bad user experience to see a tooltip on click of a button. However, you can use this if you really want to
var b = new Button();
b.Click += (sender, args) => new ToolTip().Show("Help documentation", b.Parent, new Point(b.Location.X, b.Location.X + 10));

Dynamcially Creating a table inside a panel in C#.Net

Here is my code
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
}
I cant see the my label text when i click the button
Any help appreciate
Thanks
You must add your panel inside of any control which exists on your page.
You have to add the Panel to some control in your web page or your top level form element if you don't have anywhere else to put it.
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
this.Form.Controls.Add(panel1); // YOU ARE MISSING THIS
}
You need to add the Panel to the page:
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
//Do this
SomeControlOnYourPage.Controls.Add(panel1);
}

How to declare Balloon popup extender Dynamically using C#?

Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Button";
btn.ID = "Button1";
pnlMain.Controls.Add(btn);
Panel pnl = new Panel();
pnl.ID = "pnl";
Label lbl = new Label();
lbl.ID = "lbl";
lbl.Text = "Hi this is my Balloon popup";
pnl.Controls.Add(lbl);
pnlMain.Controls.Add(pnl);
BalloonPopupExtender balloonPopupExtender1= new BalloonPopupExtender();
balloonPopupExtender1.TargetControlID = btn.ID;
balloonPopupExtender1.BalloonPopupControlID = pnl.ID;
balloonPopupExtender1.BalloonSize = BalloonPopupSize.Small;
balloonPopupExtender1.BalloonStyle = BalloonPopupStyle.Rectangle;
balloonPopupExtender1.DisplayOnMouseOver = true;
balloonPopupExtender1.DisplayOnClick = true;
balloonPopupExtender1.DisplayOnFocus = false;
}
This code executes correctly but balloon popup does not show up...
I have got the Solution
Panel pnlBalloon = new Panel();
pnlBalloon.ID="pnlBalloon";
Label LblBalloon = new Label();
LblBalloon.ID="LblBalloon";
pnlBalloon.Controls.Add(LblBalloon);
pnl_Message.Controls.Add(pnlBalloon);
LblBalloon.Text = "This is Balloon Popup";
AjaxControlToolkit.BalloonPopupExtender BalloonPopupExtender1 = new AjaxControlToolkit.BalloonPopupExtender();
BalloonPopupExtender1.ID = "BalloonPopupExtender1";
BalloonPopupExtender1.TargetControlID = labelShow.ID;
BalloonPopupExtender1.BalloonPopupControlID = pnlBalloon.ID;
BalloonPopupExtender1.BalloonSize = AjaxControlToolkit.BalloonPopupSize.Small;
BalloonPopupExtender1.BalloonStyle = AjaxControlToolkit.BalloonPopupStyle.Rectangle;
BalloonPopupExtender1.Position = AjaxControlToolkit.BalloonPopupPosition.BottomRight;
BalloonPopupExtender1.DisplayOnClick = false;
BalloonPopupExtender1.DisplayOnMouseOver = true;
BalloonPopupExtender1.DisplayOnFocus = false;
pnlConnection.Controls.Add(BalloonPopupExtender1);

Categories