I have 2 buttons. The first button gets drawn from the code behind. The second button gets drawn when the first button is clicked. This works perfectly. When I add a event when the second button gets clicked this event won't be triggered. As you can see in the code below there is a btnTwo_click function. If I put a breakpoint on this function the program won't even break. Is there a way to trigger this event from the code behind (not using java script)?
Here is the code, I actually use this system in a table. But this simple code has the same problem in the end.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += new CommandEventHandler(btnOne_Click);
form1.Controls.Add(btn);
}
void btnOne_Click(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += new CommandEventHandler(btnTwo_Click);
form1.Controls.Add(btn);
}
void btnTwo_Click(object sender, EventArgs e)
{
Label lblTest = new Label(); //BREAKPOINT
form1.Controls.Add(lblTest);
}
}
You have to add the second button on the Page_Load or Page_Init method of Page life cycle:
e.g.
protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += btnOne_Click;
form1.Controls.Add(btn);
ImageButton btn2 = new ImageButton();
btn2.Command += btnTwo_Click;
btn2.Visible = false;
form1.Controls.Add(btn2);
}
void btnOne_Click(object sender, EventArgs e)
{
// Your second button
form1.Controls[2].Visible = true;
}
void btnTwo_Click(object sender, EventArgs e)
{
ImageButton btn2 = (ImageButton)sender;
// Do something
}
Just move this line to Page_Load event and it will work ;)
btn.Command += new CommandEventHandler(btnTwo_Click);
Related
I create a button dynamically and I want when my button click and onclick event fired, a variable pass with this event.
Button btn = new Button();
btn.Click += new EventHandler(Button_Click);
//btn.Click += new EventHandler(Button_Click(32)); <-- I want something like this
divUserUploadedList.Controls.Add(btn);
And this is Onclick event:
protected void Button_Click(object sender, EventArgs e)
{
//I want to access that value here
}
You can do this by using CommandArgument property
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += btn_Click;
btn.CommandArgument = "12"; //<-- you can pass argument like this
divUserUploadedList.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string value = btn.CommandArgument;
}
What you are looking for is probably the CommandArgument property.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument%28v=vs.110%29.aspx
I want to create an imagebutton in code behind like this:
ImageButton img1 = new ImageButton();
img1.CssClass = "stylImage";
img1.ImageUrl = #"~/images/Workflow/esign.jpg";
img1.AlternateText = "Signature";
img1.CommandName = "edit";
img1.Click += new ImageClickEventHandler(Image_OnClientClick);
img1.Command += new CommandEventHandler(Image_OnCommand);
workdesk.Controls.Add(img1);
The button create and display on page, but none of the click or command event fire.
There is no error or exception too.
Any ideas?!?
Your code works fine and both the events fires perfectly as I tested.
There may be a chance that you have not specified the Event Definition correctly.
Please recheck the below code which works like a charme..!!
protected void Page_Load(object sender, EventArgs e)
{
ImageButton img1 = new ImageButton();
img1.CssClass = "stylImage";
img1.ImageUrl = #"~/images/Workflow/esign.jpg";
img1.AlternateText = "Signature";
img1.CommandName = "edit";
img1.Click += new ImageClickEventHandler(Image_OnClientClick);
img1.Command += new CommandEventHandler(Image_OnCommand);
form1.Controls.Add(img1);
}
protected void Image_OnClientClick(object sender, ImageClickEventArgs e)
{
//some code...
}
protected void Image_OnCommand(object sender, CommandEventArgs e)
{
//some code...
}
This may help you..!!
I am stuck on something: I am creating a hyperlink at runtime that has a navigation URL. I need to define its click event so that I can save a few values to the database. I did something like below but without success.
Could you please suggest an alternative?
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
HyperLink link = (HyperLink)gridDataItem["ContentTitle"].Controls[0];
link.ForeColor = System.Drawing.Color.Navy;
link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
link.Target = "_blank";
link.Attributes.Add("onclick", "document.getElementById('" +
dummyBtn.ClientID + "').click();");
}
}
protected void dummyBtn_Click(object sender, EventArgs e) {
}
But the button click event is not firing, it simply navigates to the URL. What to do please?
For a server side event to fire you would need a LinkButton and not a HyperLink
LinkButton has a Click event handler which you can use.
HyperLink only redirects and has no corresponding Click event handler associated for server side code
You want a LinkButton, not a HyperLink.
Here's some sample code to get you started (not tested)
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
LinkButton link = (LinkButton)gridDataItem["ContentTitle"].Controls[0];
link.Click += dummyBtn_Click;
}
}
protected void dummyBtn_Click(object sender, EventArgs e)
{
Response.Write("dummyBtn_Click");
}
You should be using Link Button. Just replace your Hyperlink with LinkButton in your code.It should work.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
LinkButton link = (LinkButton )gridDataItem["ContentTitle"].Controls[0];
link.ForeColor = System.Drawing.Color.Navy;
link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
link.Target = "_blank";
link.Click += dummyBtn_Click;
}
}
protected void dummyBtn_Click(object sender, EventArgs e) {
}
I add buttons dynamically.. e.g. Button newButton=new Button();
Now i want each button to be triggered. So i wrote them the following events:
public void response_Click(object sender, EventArgs e)
{
}
public void edit_Click(object sender, EventArgs e)
{
}
public void quote_Click(object sender, EventArgs e)
{
}
Button quote = new Button();
Button reply = new Button();
Button edit = new Button();
quote.ID = "quote";
reply.ID = "reply";
edit.ID = "edit";
How do i trigger them, as soon as the user clicks on the button..will my functions above be triggered? do i need to do the following:
this.Clicked+=quote;
this.Clicked+=reply;
this.Clicked+=edit;
if i do need to do that..where do i put those lines of code?
i use visual studio 1010. asp.net
You can do like..
quote.Click += new EventHandler(quote_Click);
reply.Click += new EventHandler(response_Click);
edit.Click += new EventHandler(edit_Click);
Yes, you would need to register the event of the button and associate it with particular method.
for example.
myButton.Click += new EventHandler(Button_Click);
void Button_Click(object sender, EventArgs e)
{
}
I am creating one button on a page dynamically. Now I want to use the button click event on that button.
How can I do this in C# ASP.NET?
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);
//protected void button_Click (object sender, EventArgs e) { }
The easier one for newbies:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
Simply add the eventhandler to the button when creating it.
button.Click += new EventHandler(this.button_Click);
void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}
It is much easier to do:
Button button = new Button();
button.Click += delegate
{
// Your code
};
You can create button in a simple way, such as:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.
I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,
for example, imagine you create your button on an event click:
protected void Button_Click(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
CreateDynamicElements();
}
}
on postback, for example on page load, you should do this:
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true") {
CreateDynamicElements();
}
}
In CreateDynamicElements() you can put all the elements you need, such as your button.
This worked very well for me.
public void CreateDynamicElements(){
Button button = new Button();
button.Click += new EventHandler(button_Click);
}
Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.
public form1()
{
foreach (Panel pl in Container.Components)
{
pl.Click += Panel_Click;
}
}
private void Panel_Click(object sender, EventArgs e)
{
// Process the panel clicks here
int index = Panels.FindIndex(a => a == sender);
...
}