I wanted to ask you. I am writing a WPF program where I had a problem and had to ask you for advice. How can I dynamically add a button to a WPF window? Again, how can I assign the value of button_click inside the button by giving it a function. Thanks everyone in advance
You can use this
Button b = new Button();
b.Content = "some text"
//set other properties in a similar way
b.Click += (sender,e) => {/* your click handeler*/}
container.Children.Add(b);
where container is a Grid or something similar that you want to add your button to.
You can follow the steps below.
private void SomeEvent(object sender, RoutedEventArgs e)
{
//Your code here
}
Button newBtn = new Button(); //Initialize the button object
newBtn.Content = "Name"; //Give the poor button some text
newBtn.Click += SomeEvent; //Add a click event handler. It is a function which the button will perform when clicked.
MainGrid.Children.Add(newBtn); //Now add the button to your container. It could be a grid panel, stack panel or any other container.
You can also apply padding or margin to further control how the button appears. But that's optional. Cheers!
Related
i want to add the button like plus beside the dropdown button like the picture
I don't have a problem writing the problematic code. I want to add a button in the outline design of the TreeListLookUpEdi. When I click on this button, which resembles the plus icon in the image, I add code.
Like opening a new window and adding new items to TreeListLookUpEdit
In the Runtime you can doing the following:
//add Plus Button
treeListLookUpEdit1.Properties.Buttons.Add(new EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Plus));
//add ButtonPressed Event
treeListLookUpEdit1.Properties.ButtonPressed += new ButtonPressedEventHandler(this.treeListLookUpEdit1_Properties_ButtonPressed);
private void treeListLookUpEdit1_Properties_ButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (e.Button.Kind == ButtonPredefines.Plus)
{
//here your code
MessageBox.Show("Hello!");
}
}
I make a dynamic table with buttons in they cells, but when i click some button show me this error in console:
Uncaught ReferenceError: bttn_Click is not defined
at HTMLUnknownElement.onclick (VM51 panel.aspx:1)
this is how i make the button in html and asp.net :
StringBuilder htmlStr = new StringBuilder();
htmlStr.Append("<asp:button onclick='bttn_Click' class='btn btn-warning btn-xs' ID='Bttn_PDF'>PDF</asp:button>");
PlaceHolder1.Controls.Add(new Literal { Text = htmlStr.ToString() });
and the C#
protected void bttn_Click(object sender, CommandEventArgs e)
{
Button btn = (Button)sender;
string btnid = btn.CommandArgument;
test.Text = btnid;
}
This is because the onclick event actually expects a Javascript-specific event to be called and you are currently trying to call a server-side event instead. When defining such an event on an actual ASP.NET Button control, you instead want to use the OnClientClick event instead. But this doesn't really matter as you want to target a server-side event and not a client-side one.
Build Your Button Server-side
What you'll likely want to do is create an actual Button control, define a click event handler that points to your method, and then add that control to the page as opposed to building a string:
// Build your button with its specific properties
Button button = new Button();
button.ID = "Bttn_PDF";
button.CssClass = "btn btn-warning btn-xs";
// Wire up its click event
button.Click += bttn_Click;
// Add it to the form
PlaceHolder1.Controls.Add(button);
I Have a textbox which is disabled and has value .And i want to enable right click option to copy the disabled value from textbox (Windows application).Pls help me to do this.
Try this, keeping in mind that you have to have your contextmenustrip added:
private void YourFormName_Load(object sender, EventArgs e)
{
ContextMenu mnu = new ContextMenu();
MenuItem mnuCopy = new MenuItem("Copy");
mnuCopy.Click += (sen, ev) =>
{
System.Windows.Forms.Clipboard.SetText(YourTextBoxName.Text);
};
mnu.MenuItems.AddRange(new MenuItem[] { mnuCopy });
YourTextBoxName.ContextMenu = mnu;
}
private void YourFormName_MouseUp(object sender, MouseEventArgs e)
{
Control ctl = this.GetChildAtPoint(e.Location);
if (ctl != null && !ctl.Enabled && ctl.ContextMenu != null)
ctl.ContextMenu.Show(this, e.Location);
}
When you click on a disabled element in a page, the event is handled by the parent element of the disabled element.
for example if your textbox is in a page then the page handles it. if the text box is in a different container like div, then that container will handle the mouse click event.
for your situation, you could write a handler on the parent element.
a javascript function that will catch the event and it can read the value for you. for instance, the JS function can change the disabled property to false, read the value, and then disable the textbox again.
Coming back to Vijaya's answer, I handled the problem by just placing the control with Dock=Fill into a panel control with zero padding and margin. So you would do your things in the MouseUp event of the panel instead.
I am dynamically building some HTML content on a WebForms page, using HtmlGenericControl().
HtmlGenericControl p = new HtmlGenericControl("p");
// ...
ListItems.Controls.Add(p);
But now I need to add a button to the paragraph created above, and the button needs to do a postback when clicked.
Must I completely rework this code to load child controls, which can contain real server-side buttons? Or is there a way to inject buttons capable of some type of postback?
Here's a test method and test EventHandler that, if called, will add a button control with a server-side Click event to ListItems. The commented-out code will change the markup of the ListItems object if InnerHtml is available but I rethought that approach and assumed the existence of a textbox on the page that would show the results of the click event firing. There are a number of concerns that you'll want to handle if you need those child controls to be present from one postback to another, though- for instance, if Test() isn't called every time the page loads that button won't be created and there will be no way to call its EventHandler unless other controls also call it.
public void Test()
{
System.Web.UI.HtmlControls.HtmlGenericControl p = new System.Web.UI.HtmlControls.HtmlGenericControl("p");
p.InnerHtml = #"<strong>Test</strong>";
// ...
ListItems.Controls.Add(p);
Button b = new Button();
b.ID = "cmdTest";
b.Text = "Test";
b.Click += new EventHandler(test_Click);
p.Controls.Add(b);
}
protected void test_Click(object sender, EventArgs e)
{
// ListItems.InnerHtml = "Test button clicked";
txtTestResults.Text = "Test button clicked at " + DateTime.Now.ToShortTimeString();
}
I created a page where I give admin's a way to change photos info (e.g. Title, Description, etc) All the controls on the page are added dynamically because I have more than one gallery of photos.
panel --> parent.
button .
title text box.
description text box.
In every panel, I have button that when clicked, sends the changed information to the server where the photo info is stored (Flickr). The click event for this button is added dynamically, and I want to know if is possible to get the parent of the Button I just clicked on.
Here is the code where I add all my controls:
//global veriables (this is only part of the code)
Panel panel;
Button button;
for (int i = 0; i < photo.Length; i++) {
photo[i] = new FlickerImages(photoSet.MediumURLS[i], photoSet.ThumbnailURLS[i], photoSet.Titles[i], photoSet.Descreption[i]);
panel = new Panel();
panel.ID = "panel" + i;
button = new Button();
button.ID = "sendDataButton" + i;
button.Text = "send data";
button.Click += button_Click; //adding the event
label = new Label();
label.ID = "editLabel" + i;
panel.Controls.Add(label);
panel.Controls.Add(photo[i].CurrentImage(i)); //Image control
panel.Controls.Add(photo[i].EditTitleTextBox(i)); //TextBox control
panel.Controls.Add(photo[i].EditCommentTextBox(i)); //TextBox control
panel.Controls.Add(button);
Form.Controls.Add(panel);
}
Here is the click event I add to all the buttons:
void button_Click(object obj, EventArgs e) {
Response.Write(button.Parent.ID); // i get panel10 every time this get fired.
}
I know this is possible with jQuery but is it possible to get the button ID in ASP.NET?
Sorry for my English and thanks for the help.
Not sure, but are you looking for the ClientID property? (button.Parent.ClientID)
Edit:
You should reference the sending button in the event handler:
void button_Click(object obj, EventArgs e)
{
Response.Write(((Button)obj).Parent.ID);
}