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);
}
Related
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!
I have a number of radio buttons created dynamically inside a loop and added to a div inside update panel. On each postback request triggered by checked change event, I recreate the radio buttons in my page_init method. My problem is the radio button I selected is not checked and the checked changed event is not firing on first click. But on subsequent clicks, it works normally and the checked changed event is fired. Only the first click is not firing. What could be the issue?
Simple dynamic radio button.
RadioButton btn2 = new RadioButton();
btn2.Text = "TEST";
btn2.CheckedChanged += Btn2_CheckedChanged; ;
btn2.AutoPostBack = true;
pricetbldiv.Controls.Add(btn2);
private void Btn2_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn = (RadioButton)sender;
string text = btn.Text;
}
Try to assign group and ID
btn2.ID = "Text";
btn2.Text = "Text";
btn2.GroupName = "RB";
btn2.CheckedChanged += new EventHandler(Btn2_CheckedChanged);
I am generating wpf form dynamically. All the controls are generated dynamically as follows
A sample code snippet
String tbname = name;
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = tbname;
Grid.SetRow(txtBlock1, count);
Grid.SetColumn(txtBlock1, icount);
SampleGrid.Children.Add(txtBlock1);
TextBox txtBox = new TextBox();
txtBox.Text = ptiAttribute.description;
txtBox.Name = tbname.Replace(" ", "");
DynamicGrid.RegisterName(txtBox.Name, txtBox);
Grid.SetRow(txtBox, count);
Grid.SetColumn(txtBox, icount+1);
SampleGrid.Children.Add(txtBox);
Attaching a button click event as follows
var Button = CreateButton("Save", 15, 3);
Button.Click += new RoutedEventHandler(button_Click);
SampleGrid.Children.Add(Button);
I would like to get all the control values (For example: The above text box has value Book), I have to get it after button click. I am not only having text box. I have combo box, date picker too. I don't know which name is registered (RegisterName). Every thing dynamic.
private static void button_Click(object sender, RoutedEventArgs e)
{
# How to get dynamic values here (text bx value, date picker value, combo box value)
}
Simply, how to get values from dynamically generated controls. I have gone through a lot of Visual tree links but I don't know how it works on button click.
Any simple code snippet will help me to move ahead. Thanks
I'm not used to WPF but try this approach(inspired by this):
private static void button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
int row = Grid.GetRow(btn);
TextBox txtBox = SampleGrid.Children
.OfType<TextBox>()
.First(txt => txt.Name == name && Grid.GetRow(txt) == row);
// ...
}
I'm creating buttons dynamically. How can I select a specific button using its name (ex: in the following code using "i" ) from remaining part of the code.
for(int i = 0; i < 5; i++)
{
button b = new button();
b.name = i.ToString();
}
First - its not enough to just create buttons. You need to add them to some control:
for(int i = 0; i < 5; i++)
{
Button button = new Button();
button.Name = String.Format("button{0}", i); // use better names
// subscribe to Click event otherwise button is useless
button.Click = Button_Click;
Controls.Add(button); // add to form's controls
}
Now you can search child controls of buttons container for some specific button:
var button = Controls.OfType<Button>().FirstOrDefault(b => b.Name == "button2");
NOTE: If you'll use buttonN name pattern then make sure you don't have other buttons with same names, because this pattern is used by VS designer.
UPDATE: If you will use same event handler for all dynamic buttons Click event (actually you should), then you can easily get button which raised even in event handler:
private void Button_Click(object sender, EventArgs e)
{
// that's the button which was clicked
Button button = (Button)sender;
// use it
}
A simpler solution is search the button in the Controls collection.
Button btn = (Button) this.Controls["nameButton"];
//...DO Something
The problem of this solution is that if there isn't a button with the nameButton the JIT will throw an exception. If you want prevent this you must insert the code in a try catch block or, if you prefer, you can use Sergey Berezovskiy solution(he uses Linq and I think it's more clear)
You have to place your button somewhere:
for (int i = 0; i < 5; i++) {
// Mind the case: Button, not button
Button b = new Button();
// // Mind the case: Name, not name
b.Name = i.ToString();
//TODO: place your buttons somewhere:
// on a panel
// myPanel.Controls.Add(b);
// on a form
// this.Controls.Add(b);
// etc.
//TODO: it seems, that you want to add Click event, something like
// b.Click += MyButtonClick;
}
then you can just query appropriate Controls for the Button:
Button b = myPanel.Controls["1"] as Button;
if (b != null) {
// The Button is found ...
}
You could just put them in a Button[] or you could iterate over the Controls collection of your Form.
I create some dynamic textbox's and a button in a placeholder and would like to save info in textbox's when button is clicked but not sure how to retrieve data from the textbox
LiteralControl spacediv3 = new LiteralControl("  ");
Label lblComText = new Label();
lblComTitle.Text = "Comment";
TextBox txtComment = new TextBox();
txtComment.Width = 200;
txtComment.TextMode = TextBoxMode.MultiLine;
phBlog.Controls.Add(lblComText);
phBlog.Controls.Add(spacediv3);
phBlog.Controls.Add(txtComment);
Button btnCommentSave = new Button();
btnCommentSave.ID = "mySavebtnComments" ;
btnCommentSave.Text = "Save ";
phBlog.Controls.Add(btnCommentSave);
btnCommentSave.CommandArgument = row["ID"].ToString();
btnCommentSave.Command += new CommandEventHandler(btnSave_Click);
protected void btnSave_Click(object sender, CommandEventArgs e)
{
firstelement.InnerText = txtComment.text // this gives error on txtComment.text
}
You need to get a reference to your control in btnSave_Click. Something like:
protected void btnSave_Click(object sender, CommandEventArgs e)
{
var btn = (Button)sender;
var container = btn.NamingContainer;
var txtBox = (TextBox)container.FindControl("txtComment");
firstelement.InnerText = txtBox.text // this gives error on txtComment.text
}
You also need to set the ID on txtComment and recreate any dynamically created controls at postback.
You will need some mechanism to create a relation between the Button and the TextBox obviously. In winforms this would be easy, where each control has a Tag property, which can contain a reference to pretty much anything. The web controls don't have such a property (that I know of), but it is still easy to maintain such relations. One approach would be to have Dictionary in the page storing button/textbox relations:
private Dictionary<Button, TextBox> _buttonTextBoxRelations = new Dictionary<Button, TextBox>();
When you create the button and textbox controls, you insert them in the dictionary:
TextBox txtComment = new TextBox();
// ...
Button btnCommentSave = new Button();
// ...
_buttonTextBoxRelations.Add(btnCommentSave, txtComment);
...and then you can look up the text box in the button's click event:
protected void btnSave_Click(object sender, CommandEventArgs e)
{
TextBox commentTextBox = _buttonTextBoxRelations[(Button)sender];
firstelement.InnerText = txtComment.text // this gives error on txtComment.text
}
Try during postback to load txtComment (with the same ID) in overridden LoadViewState method after calling base.LoadViewState. In this case you do load it before postback data are handled and txtComment control comes loaded.
Add an 'ID' to the textbox
txtComment.ID = "txtComment"
Request the information from the submitted form (provided you have a form on the page)
comment = Request.Form("txtComment")