I want to assign events to each ImageButton from code behind, but i can not find out how to write a proper one.
foreach (string one in urls)
{
ImageButton temIBTN = new ImageButton();
temIBTN.Attributes.Add("Width","265px");
temIBTN.Attributes.Add("Width", "144px");
temIBTN.ImageUrl = one;
temIBTN.Click +=
new EventHandler(setBigPic(sender, e, one));//<---don't know how...
}
protected void setBigPic(object sender, ImageClickEventArgs e,string url)
{
img_Big.ImageUrl = url;
}
Your method signature for the event handler was wrong, and you need to get the ImageUrl from the button that's firing the event. This should do it:
foreach (string one in urls)
{
ImageButton temIBTN = new ImageButton();
temIBTN.Attributes.Add("Width","265px");
temIBTN.Attributes.Add("Width", "144px");
temIBTN.ImageUrl = one;
temIBTN.Click += setBigPic;
}
protected void setBigPic(object sender, ImageClickEventArgs e)
{
img_Big.ImageUrl = ((ImageButton)sender).ImageUrl;
}
Related
I have two FlowLayoutPanel controls on the same form with some controls on both of them. What O want is that if FlowLayoutPanel1 controls are clicked, I want to change label1.Text and if FlowLayoutPanel2 controls are clicked, I want to change label2.Text.
Here is my code to add controls in both FlowLayoutPanel.
public void Load_DFlavours(FlowLayoutPanel FLP)
{
try
{
FLP.Controls.Clear();
using (SQLiteConnection con = new SQLiteConnection(AppSettings.ConnectionString()))
{
con.Open();
using (SQLiteDataAdapter sda = new SQLiteDataAdapter("Select distinct(Flavour_Name) From Flavours Where Category_Name = 'Flavours' Order By Flavour_Name", con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
RadioButton rb2 = new RadioButton();
rb2.AutoSize = true;
rb2.Font = new Font("Segoe UI Semilight", 10F);
rb2.Margin = new Padding(2);
rb2.Text = dr["Flavour_Name"].ToString();
rb2.UseVisualStyleBackColor = true;
rb2.Tag = dr["Flavour_Name"].ToString();
FLP.Controls.Add(rb2);
rb2.CheckedChanged += Rb2_CheckedChanged;
}
}
con.Close();
}
}
catch (SQLiteException se)
{
MessageBox.Show(se.Message);
}
}
Clickevent Code:
private void Rb2_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb2 = (RadioButton)sender;
string flavour = rb2.Tag.ToString();
//I want to do something here if flowlayoutPanel 1 control is
clicked change the label1.Text and if flowlayoutPanel 2 control is
clicked change the label2.text
//I have tried this
if(rb2.Checked)
{
label1.text = flavour;
}
}
How to know which FlowLayoutPanel Controls are clicked?
I can do this by creating multiple methods but I want to do this work on the same method.
For more clarification, see this image:
Don't have a computer to check right now, but I assume this should work
if (rb2.Checked)
{
if (rb2.Parent.Name == "flowlayoutPanel1")
{
label1.Text = flavour;
}
else if (rb2.Parent.Name == "flowlayoutPanel2")
{
label2.Text= flavour;
}
}
You can add more information to Tag property of the RadioButton. For example, you can:
rb2.Tag = $"{dr["Flavour_Name"]}|{FLP.Name}";
This way, you can use string.Split() to take the Flavour_name and FlowLayoutPanel name. But since the Tag property accept object, you can create new class to hold the information.
Using this approach, you can use:
RadioButton rb2 = (RadioButton)sender;
string[] splits = rb2.Tag.ToString().Split('|');
string flavour = splits[0];
string flowPanelName = splits[1];
You can use the ControlAdded and ControlRemoved events of the FlowLayoutPanel to subscribe/unsubscribe to an event which changes the text of the label based on the clicked control:
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.Click += flowLayoutPanel1_ControlClicked;
}
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
e.Control.Click -= flowLayoutPanel1_ControlClicked;
}
private void flowLayoutPanel1_ControlClicked(object sender, EventArgs e)
{
var control = (Control)sender;
label1.Text = control.Text;
}
private void flowLayoutPanel2_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.Click += flowLayoutPanel2_ControlClicked;
}
private void flowLayoutPanel2_ControlRemoved(object sender, ControlEventArgs e)
{
e.Control.Click -= flowLayoutPanel2_ControlClicked;
}
private void flowLayoutPanel2_ControlClicked(object sender, EventArgs e)
{
var control = (Control)sender;
label2.Text = control.Text;
}
And of course, you need to subscribe to these events first, either by selecting the event from the properties window, or by calling the following:
flowLayoutPanel1.ControlAdded += flowLayoutPanel1_ControlAdded;
flowLayoutPanel1.ControlRemoved += flowLayoutPanel1_ControlRemoved;
flowLayoutPanel2.ControlAdded += flowLayoutPanel2_ControlAdded;
flowLayoutPanel2.ControlRemoved += flowLayoutPanel2_ControlRemoved;
Whenever DropDownList SelectedIndexChanged, I am adding LinkButtons as ul-li list in codebehind. Each linkbuttons were assigned with IDs and a common Click event. Problem is code in Click event is not executed or maybe event is not triggered. My code below: [Edit] I tried like this as suggested in other posts (dynamically created list of link buttons, link buttons not posting back)
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
populate();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
populate();
}
void populate()
{
HtmlGenericControl ulList = new HtmlGenericControl("ul");
panel.Controls.Add(ulList);
foreach (DataRow dr in drc)
{
HtmlGenericControl liList = new HtmlGenericControl("li");
ulList.Controls.Add(liList);
var lnk = new LinkButton();
lnk.ID = dr["col1"].ToString();
lnk.Text = dr["col1"].ToString();
lnk.Click += Clicked;
liList.Controls.Add(lnk);
}
}
private void Clicked(object sender, EventArgs e)
{
var btn = (LinkButton)sender;
label1.Text = btn.ID.ToString();
}
Im missing something. Any help please.
Here the issue is with the ViewState. When the selected index of the dropdownlist changes there is a postback which takes place and the previous state is lost, so at this point you have to maintain the state of the controls.
Now in your code actually the state of the control is lost and so the click event does not fire. So the solution is to maintain the state of the controls.
The Below is a working example, you can just paste it and try.
This is my page load.
protected void Page_Load(object sender, EventArgs e)
{
for (var i = 0; i < LinkButtonNumber; i++)
AddLinkButton(i);
}
Similarly you have to maintain the state of the previously added control like this.
private int LinkButtonNumber
{
get
{
var number = ViewState["linkButtonNumber"];
return (number == null) ? 0 : (int)number;
}
set
{
ViewState["linkButtonNumber"] = value;
}
}
The below is my SelectedIndexChanged Event for the DropDownList
protected void Example_SelectedIndexChanged(object sender, EventArgs e)
{
AddLinkButton(LinkButtonNumber);
LinkButtonNumber++;
}
I have a function which dynamically creates the controls, which is called on the page load and on SelectedIndexChanged.
private void AddLinkButton(int index)
{
LinkButton linkbutton = new LinkButton { ID = string.Concat("txtDomain", index) };
linkbutton.ClientIDMode = ClientIDMode.Static;
linkbutton.Text = "Link Button ";
linkbutton.Click += linkbutton_Click;
PanelDomain.Controls.Add(linkbutton);
PanelDomain.Controls.Add(new LiteralControl("<br />"));
}
And this is the Click event for the LinkButton
void linkbutton_Click(object sender, EventArgs e)
{
//You logic here
}
I solved it using brute code lol.
Since controls' event were not bound on postback then we recreate them on postback. So in my Page_Load I called the module that re-creates the controls, thus binding them to corresponding event. This works, but...
Re-creating these controls create duplicates (Multiple Controls with same ID were found) and you will get into trouble in instances of finding a control by ID like using panel.FindControl.
To remedy this scenario, I put a check if same control ID already existed before recreating them, and voila! It works.
protected void Page_Load(object sender, EventArgs e)
{
populate();
}
void populate()
{
HtmlGenericControl ulList = new HtmlGenericControl("ul");
panel.Controls.Add(ulList);
foreach (DataRow dr in drc)
{
HtmlGenericControl liList = new HtmlGenericControl("li");
ulList.Controls.Add(liList);
if (liList.FindControl(dr["col1"].ToString()) == null)
{
var lnk = new LinkButton();
lnk.ID = dr["col1"].ToString();
lnk.Text = dr["col1"].ToString();
lnk.Click += Clicked;
liList.Controls.Add(lnk);
}
}
}
I have the following code:
protected void Page_Load(object sender, EventArgs e)
{
using (ImageButton _btnRemoveEmpleado = new ImageButton())
{
_btnRemoveEmpleado.ID = "btnOffice_1";
_btnRemoveEmpleado.CommandArgument = Guid.NewGuid().ToString();
_btnRemoveEmpleado.Height = 15;
_btnRemoveEmpleado.Width = 15;
_btnRemoveEmpleado.ImageUrl = "cross-icon.png";
_btnRemoveEmpleado.Click += new ImageClickEventHandler(_btnRemoveEmpleado_Click);
this.phPartesPersonal.Controls.Add(_btnRemoveEmpleado);
}
}
void _btnRemoveEmpleado_Click(object sender, ImageClickEventArgs e)
{
try
{
string s = "";
}
catch (Exception ex)
{
}
finally { }
}
When I click on _btnRemoveEmpleado, the postback is executed but I never reach the string s = ""; line. How could I execute the _btnRemoveEmpleado_Click code, please?
Remove the using, controls are disposed automatically by ASP.NET, they have to live until the end of the page's lifecycle. Apart from that create your dynamic control in Page_Init, then it should work.
protected void Page_Init(object sender, EventArgs e)
{
ImageButton _btnRemoveEmpleado = new ImageButton();
_btnRemoveEmpleado.ID = "btnOffice_1";
_btnRemoveEmpleado.CommandArgument = Guid.NewGuid().ToString();
_btnRemoveEmpleado.Height = 15;
_btnRemoveEmpleado.Width = 15;
_btnRemoveEmpleado.ImageUrl = "cross-icon.png";
_btnRemoveEmpleado.Click += new ImageClickEventHandler(_btnRemoveEmpleado_Click);
this.phPartesPersonal.Controls.Add(_btnRemoveEmpleado);
}
I have a string in my main body that takes from an array. I want to pass that to the click event on a linkbutton. Is there any way to do this? Any help much appreciated.
Example code below:
(main body)
string myLabelsName = column.ToString();
LinkButton myButton = new LinkButton();
myButton.Text = ("THIS IS MY BUTTON");
myButton.Click += new System.EventHandler(myButton_Click);
(event)
protected void myButton_Click(object sender, EventArgs e)
{
I WANT THE STRING 'myLabelsName' Here <<
Response.Redirect(myLabelsName + ".aspx");
}
You can use fields/properties or more appropriate here: the LinkButton's CommandName property.
string myLabelsName = column.ToString();
LinkButton myButton = new LinkButton();
myButton.Text = "THIS IS MY BUTTON";
myButton.CommandName = myLabelsName;
myButton.Click += new System.EventHandler(myButton_Click);
// ...
protected void myButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton) sender;
string myLabelsName = btn.CommandName;
Response.Redirect(myLabelsName + ".aspx");
}
By the way, you're using a LinkButton, why don't you use it's PostBackUrl property directly?
protected void Button1_Click(object sender, EventArgs e)
{
TableRow tb = new TableRow();
TableCell tc = new TableCell();
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
tc.Controls.Add(db);
tb.Controls.Add(tc);
Table1.Controls.Add(tb);
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
}
private void db_SelectedIndexChanged(object sender, EventArgs e)
{
label.text = "welcome";
}
When this code executes in the Button1_Click event, db_SelectedIndexChanged doesn't execute. However, when I place the same Button1_Click code block in the Page_Load event, db_SelectedIndexChanged executes.
What may be the reason behind this?
Try to put
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
In the Page_Load event.
Don't wrap db.SelectedIndexChanged += db_SelectedIndexChanged; in !Page.IsPostBack as the events need to be wired up on each load
You are creating a dynamic control. The event will not fire unless you create the control in the PreInit method of the page.
protected void Page_PreInit(object sender, EventArgs e)
{
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
tc.Controls.Add(db);
}
Check Page Life cycle for more info.
protected void Button1_Click(object sender, EventArgs e)
{
TableRow tb = new TableRow();
TableCell tc = new TableCell();
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");`
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
tc.Controls.Add(db);
tb.Controls.Add(tc);
Table1.Controls.Add(tb);
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
db_SelectedIndexChanged(null,null); // use this line, i hope it will work now.
}
private void db_SelectedIndexChanged(object sender, EventArgs e)
{
label.text = "welcome";
}
You can try this one.