Event does not fire? - c#

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..!!

Related

dynamic linkbutton not calling command when clicked

New to dynamic controls, but until now I have been creating them successfully in a
template field in my gridview, recently switched from a hyperlink to a link button
and had to make some changes but still not working
In my page I have the following code (abridged to salient parts)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
...
...
TemplateField tf = new TemplateField();
tf.HeaderText = "Action";
tf.ItemTemplate = new AssignPage.MyTemplate(..., mylb);
GridView1.Columns.Add(tf);
protected void Page_PreInit(object sender, EventArgs e)
{
LinkButton lb = new LinkButton();
lb.Text = "AssignAll";
lb.Command += new CommandEventHandler(AssignAll_Click);
lb.CommandName = "XXX";
this.mylb = lb;
protected void AssignAll_Click(object sender, CommandEventArgs e)
{
string[] arg = new string[2]; // BREAK POINT HERE
arg = e.CommandArgument.ToString().Split(';');
...
...
Response.Redirect("BaseAndRepeats.aspx?id=" + r.Event.ID);
In the template class I have I have
LinkButton lb;
public MyTemplate(..., LinkButton _lb)
{
...
lb = _lb;
...
public void InstantiateIn(System.Web.UI.Control container)
{
...
...
// various conditional statements
lb.CommandArgument = mylist[rowCount].ReqtID.ToString() + ";" + mylist[rowCount].RotaUser;
container.Controls.Add(lb);
...
The break point in the handler is never reached
I thought I was creating the linkbutton in the right place
the linkbutton appears in the grid quite happily
when I click on the linkbutton there is a call
to Page_PreInit and to Page_Load and as I expected
it is a postback.
But AssignAll_Click is never called.
In the browser footer it shows "javascript: __dooPsotabck(..." when you hover
over the buttonlink
I believe the problem is this line:
lb.Command += new CommandEventHandler(AssignAll_Click);
Change it to:
lb.Command += AssignAll_Click;
Edit:
Also you want to move it from Page_PreInit to Page_Init but you may find more success with it in Page_Load (outside the IsPostBack). Here's an example that works for me:
On the ASPX page:
<asp:Panel ID="TestPanel" runat="server" />
Codebehind:
protected void Page_Init(object sender, EventArgs e)
{
//Init used because TestPanel doesn't exist yet
CreateTestButton();
}
private void CreateTestButton()
{
var lb = new LinkButton();
lb.Text = "hello";
lb.Command += lb_Command;
TestPanel.Controls.Add(lb);
}
void lb_Command(object sender, CommandEventArgs e)
{
throw new NotImplementedException();
}
Going over your code it looks like your adding another column into your GridView and creating a button inside that column for each row - but you've got the column being added on Page_Load and the button being created and bound to it before in Page_PreInit

A little help in asp.net C# jumping from method to method

My problem is that I can get from page_load to show_books function
but I cant get from show_books to book_detail.
Every time I try to fire book_details it gets me to page_load.
my code is this:
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(book_show);
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(book_details);
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
I'm sorry if my question is annoyingly newbie
but I would like some help .
I just started to learn asp.net
well i don't understand why you need to do this,but code below works,hope it helps
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(this.book_show);
lnk_button.CommandName = "testClick";
lnk_button.CommandArgument = "1";
lnk_button.ID = "11";
lnk_button.Text = "blah";
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
public void book_show()
{
book_details() ;
}
public void book_details()
{
}

Second eventhandler won't trigger from button

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);

Dynamically create an ImageButton

I’m trying to dynamically declare an ImageButton.
I declare it and assign an ID and Image to it as follows:
ImageButton btn = new ImageButton();
btn.ImageUrl = "img/Delete.png";
btn.ID = oa1[i] + "_" + i;
btn.OnClick = "someMethod";
But when I try to assign an OnClick handler for the button it throws the following exception:
System.Web.UI.WebControls.ImageButton.OnClick is inaccessible due to protection level
You couldn't assign a value to a method like that, even if it were accessible. You need to subscribe to the event:
btn.Click += ClickHandlingMethod;
Take a look at this answer, it is related with dynamic controls and events
As Jon commented you cannot add a string to the event, in this case you need to add a handler for the event:
protected void Page_Init(object sender, EventArgs e)
{
var i = new ImageButton();
i.Click += new ImageClickEventHandler(i_Click);
this.myPanel.Controls.Add(i);
}
void i_Click(object sender, ImageClickEventArgs e)
{
// do something
}
Alternativeley
protected void Page_Init(object sender, EventArgs e)
{
var i = new ImageButton();
i.Click += (source, args) =>
{
// do something
};
this.myPanel.Controls.Add(i);
}
An example:
private void CreateAButton()
{
var button = new ImageButton();
button.ImageUrl = "yourimage.png";
button.ID = "Button1";
button.Click += ButtonClick;
Page.Form.Controls.Add(button);
}
private void ButtonClick(object sender, ImageClickEventArgs e)
{
// Do stuff here
// ...
}
You can use this code (one significant change) :
private void CreateAButton()
{
var button = new ImageButton();
button.ImageUrl = "yourimage.png";
button.ID = "Button1";
button.PostBackUrl = "http://www.towi.lt";
Page.Form.Controls.Add(button);
}
Trick is in "PostBackUrl". If you write correct link it will redirects to it (as in example). In other cases this will add original server name, '/' and text you entered. For example 'xxx' will be turned to "http://yourservername/xxx". It is very useful, when you working with redirects to same ISS, but different sites and dynamically creating buttons for users.

Adding button events!

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)
{
}

Categories

Resources