dynamic imagebutton click event not getting fired - c#

dynamic imagebutton click event not getting fired please help
i have created dynamic buttons in the asp.net oninit method
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ImageButton img = new ImageButton();
img.ID = "first_button";
img.Click += new ImageClickEventHandler(first_Click);
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.first.gif");
img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
p1.Controls.Add(img);
img.Dispose();
img = new ImageButton();
img.ID = "previous_button";
img.Click += new ImageClickEventHandler(previous_Click);
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.previous.gif");
img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
p1.Controls.Add(img);
img.Dispose();
t1.Attributes.Add("style", "color:#666666;");
// t1.Text = "Page " + current_page + " of " + total_pages;
t1.ID = "text_box1";
t1.Attributes.Add("onclick", "textbox_enable('" + t1.ClientID + "')");
p1.Controls.Add(t1);
img = new ImageButton();
img.ID = "go_button";
img.Click += new ImageClickEventHandler(go_Click);
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.go.GIF");
img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
p1.Controls.Add(img);
img.Dispose();
ImageButton img1 = new ImageButton();
img1.ID = "next_button";
img1.CommandName = "next_button";
img1.CommandArgument = "next1";
img1.Click += new ImageClickEventHandler(next_Click);
img1.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.next.gif");
img1.Attributes.Add("onmouseover", "onmousehand(this,'over')");
img1.Attributes.Add("onmouseout", "onmousehand(this,'out')");
p1.Controls.Add(img1);
//img.Dispose();
img = new ImageButton();
img.ID = "last_button";
img.Click += new ImageClickEventHandler(last_Click);
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.last.gif");
img.Attributes.Add("onmouseover", "onmousehand(this,'over')");
img.Attributes.Add("onmouseout", "onmousehand(this,'out')");
p1.Controls.Add(img);
img.Dispose();
}
private void next_Click(object sender, ImageClickEventArgs e)
{
ImageButton next = (ImageButton)sender;
string value = next.CommandArgument;
current_page++;
t1.Text = "Page " + current_page + "of" + total_pages;
}
protected override void Render(HtmlTextWriter writer)
{
t1.Text = "Page " + current_page + " of " + total_pages;
p1.RenderControl(writer);
base.Render(writer);
}

Try this
protected void Page_Load(object sender, EventArgs e)
{
ImageButton img = new ImageButton();
img.ID = "SampleImage";
img.Click += new ImageClickEventHandler(img_Click);
this.form1.Controls.Add(img);
}
void img_Click(object sender, ImageClickEventArgs e)
{
Response.Write("Hello World");
}

You can define your dynamic buttons functionality in Page_Init instead of Oninit.
protected void Page_Init(object sender, EventArgs e)
{
ImageButton imagebutton = new ImageButton();
imagebutton.ID = "myID";
imagebutton.Attributes.Add("runat", "server");
imagebutton.Click += new ImageClickEventHandler(image_Click);
this.form1.Controls.Add(imagebutton);
}
void image_Click(object sender, ImageClickEventArgs e)
{
//Your code here
}

Related

How to assign methods to a dynamic created Gridview?

I have the following class which is the class for creating a gridview dynamic:
public class DynamicGridViewImageButtonTemplate : ITemplate
{
string _ColName;
DataControlRowType _rowType;
int _Count;
int _Kind;
public DynamicGridViewImageButtonTemplate(string ColName, DataControlRowType RowType, int Kind)
{
_ColName = ColName;
_rowType = RowType;
_Kind = Kind;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.DataRow:
if (_Kind == 0)
{
Label lbl = new Label();
lbl.DataBinding += new EventHandler(this.lbl_DataBind);
container.Controls.Add(lbl);
}
else
if (_Kind == 1)
{
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
container.Controls.Add(vImageButton);
}
break;
default:
break;
}
}
and the following c# code for assigning methods runtime to the imagebutton which I have created in a column:
protected void btnGetVoucher_Command(object sender, CommandEventArgs e)
{
ImageButton btn = sender as ImageButton;
}
protected void gridview_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton vImageButton = (ImageButton)e.Row.FindControl("btnGetVoucher");
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
}
}
and the creating of the gridcolumn is as follows:
GridView vGridView = new GridView();
vGridView.RowCreated += new GridViewRowEventHandler(gridview_RowCreated);
vTemplateField = new TemplateField();
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header, 0);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow, 1);
vGridView.Columns.Add(vTemplateField);
And at last the databind here:
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, "Voucher").ToString();
}
But I can't step into the method btnGetVoucher_Command :-( I think that everything has been made correctly - but there must be something I have missed somewhere...
I can access the button in my Row_Created routine - but afterwards not use the GetVoucherCommand :-( the event is not fired :-(
So my question is why can't I step into that routine?
Thanks in advance,
Michael
Update:
The full class is here...
public class DynamicGridViewTextTemplate : ITemplate
{
string _ColNameText;
DataControlRowType _rowType;
public DynamicGridViewTextTemplate(string ColNameText, DataControlRowType RowType)
{
_ColNameText = ColNameText;
_rowType = RowType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "<b>" + _ColNameText + "</b>";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.ImageUrl = "~/Images/Icons/icons8-download-from-the-cloud-50.png";
vImageButton.PostBackUrl = "#DownloadVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.ToolTip = "Se og download bilag";
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
vImageButton.CommandName = "DownloadVoucher";
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
container.Controls.Add(vImageButton);
break;
default:
break;
}
}
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, _ColNameText).ToString();
}
}
public class DynamicGridViewImageButtonTemplate : ITemplate
{
string _ColNameText;
DataControlRowType _rowType;
public DynamicGridViewImageButtonTemplate(string ColNameText, DataControlRowType RowType)
{
_ColNameText = ColNameText;
_rowType = RowType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "<b>" + _ColNameText + "</b>";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.ImageUrl = "~/Images/Icons/icons8-download-from-the-cloud-50.png";
vImageButton.PostBackUrl = "#DownloadVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.ToolTip = "Se og download bilag";
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
vImageButton.CommandName = "DownloadVoucher";
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
container.Controls.Add(vImageButton);
break;
default:
break;
}
}
protected void btnGetVoucher_Command(object sender, CommandEventArgs e)
{
ImageButton btn = sender as ImageButton;
}
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, _ColNameText).ToString();
}
}
The events is here...
protected void gridview_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton vImageButton = (ImageButton)e.Row.FindControl("btnGetVoucher");
}
}
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton vImageButton = (ImageButton)e.Row.FindControl("btnGetVoucher");
}
}
protected void gridview_RowCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "DownloadVoucher")
{
int index = Convert.ToInt32(e.CommandArgument);
GridView grid = sender as GridView;
GridViewRow row = grid.Rows[index];
ImageButton vImageButton = (ImageButton)row.FindControl("btnGetVoucher");
}
}
The creating of the gridview is here...
GridView vGridView = new GridView();
vGridView.AutoGenerateColumns = false;
vGridView.ShowHeaderWhenEmpty = true;
vGridView.ShowHeader = true;
vGridView.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#C6E0B4");
vGridView.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
vGridView.RowCreated += new GridViewRowEventHandler(gridview_RowCreated);
vGridView.RowDataBound += new GridViewRowEventHandler(gridview_RowDataBound);
vGridView.RowCommand += new GridViewCommandEventHandler(gridview_RowCommand);
vTemplateField = new TemplateField();
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow);
vGridView.Columns.Add(vTemplateField);
micheal.
in this code, i can't see event handling code in InstantiateIn method.
ex) like this.
lbl.DataBinding += new EventHandler(this.btnGetVoucher_Command);
DynamicGridViewTextTemplate class constructor args are 3 count.
but, when using DynamicGridViewTextTemplate constructor,
used only 2 count args.
where is kind?
try it.
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header, 0);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow, 1);
best example in msdn
https://learn.microsoft.com/ko-kr/dotnet/api/system.web.ui.webcontrols.templatefield.-ctor?view=netframework-4.8
I hope this answer helps you.

Dynamic controls dissapearing on postback

I am creating dynamic textboxes when clicking on a set of different radio button. Below is an example of two radio button onclick event.
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
string servicename = "service1";
if (checkbox1.Checked)
{
InputParameters.InputParameters aa= new InputParameters.InputParameters();
textbox = aa.GetInputFields(servicename);
for (int i=0;i<textbox.Count;i++)
{
// declare a textbox
TextBox CPDT = new TextBox();
CPDT.ID = servicename + i.ToString();
CPDT.CssClass = "form-control";
CPDT.EnableViewState = true;
Label lblCPD=new Label();
lblCPD.ID = "txtDynamiclbl" + servicename+ i.ToString();
lblCPD.CssClass= "form-control-label";
lblCPD.Text= textbox[i].ToString();
lblCPD.EnableViewState = true;
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
//this.NumberOfControls++;
}
Button callSoap = new Button();
callSoap.ID = "txtDynamicSearch" + servicename;
callSoap.Text = "Search";
callSoap.CssClass = ".btn-info";
callSoap.CommandArgument = "test";
callSoap.Click += new EventHandler(btnsoap);
callSoap.EnableViewState = true;
CPDPlaceHolder.Controls.Add(callSoap);
}
else
{
}
}
protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
string servicename = "service2";
if (checkbox2.Checked)
{
InputParameters.InputParameters aa = new InputParameters.InputParameters();
List<String> textbox = aa.GetInputFields("test1");
// textboxs.AddRange(textbox);
for (int i = 0; i < textbox.Count; i++)
{
// declare a textbox
TextBox CPDT = new TextBox();
CPDT.ID = servicename + i.ToString();
CPDT.CssClass = "form-control";
Label lblCPD = new Label();
lblCPD.ID = "txtDynamiclbl" + servicename + i.ToString();
lblCPD.CssClass = "form-control-label";
lblCPD.Text = textbox[i].ToString();
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
}
Button callSoap = new Button();
callSoap.ID = "txtDynamicSearch" + servicename;
callSoap.Text = "Search";
callSoap.CssClass = ".btn-info";
callSoap.CommandArgument = "test1";
callSoap.Click += new EventHandler(btnsoap);
callSoap.EnableViewState = true;
CPDPlaceHolder.Controls.Add(callSoap);
}
else
{
}
}
The textboxes and search button appears as needed. The problem now is when i clicked on the search button a post back occur and all the controls are gone. I have been reading a lot about initialising the controls in page_preinit and i tried the code below.
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
try
{
foreach (string key in keys)
{
TextBox CPDT = new TextBox();
CPDT.ID = "test" + i.ToString();
CPDT.CssClass = "form-control";
Label lblCPD = new Label();
lblCPD.ID = "txtDynamiclbl" + "test" + i.ToString();
lblCPD.CssClass = "form-control-label";
lblCPD.Text = textbox[i].ToString();
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
i++;
}
}
catch
{
}
}
In the above function this line only returns the search button and not the texboxes. I am stuck on this issue.
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
T

Passing Class as an argument to an image click event Handler

I have a Trip object which I am getting from list TripsByTripIds which I want to pass as a parameter to image click event handler down below. how do I pass it?
foreach (Tripclass Trip in TripsByTripIds )
{
ImageButton imageButton = new ImageButton();
imageButton.ImageUrl = "~/" +Trip.CorridorName+"/"+Trip.Time+"/"+Trip.ImgFileName;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
AMSPanel1.Controls.Add(imageButton);
AMSPanel1.Height = Unit.Pixel(860);
}
protected void imageButton_Click(object sender, ImageClickEventArgs fi)
{
testimage.ImageUrl = ((ImageButton)sender).ImageUrl;
lblTime.Text = Trip.Time;
lblLocation.Text = Trip.Location; //can't access trip object here
}
I found answer from one of posts in stack overflow the other day.i haven't saved that link though. but the solution below solves my problem
foreach (Tripclass Trip in TripsByTripIds )
{
ImageButton imageButton = new ImageButton();
imageButton.ImageUrl = "~/" + Trip.CorridorName + "/" + Trip.Time + "/" + Trip.ImgFileName;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler((a, b) => imageButton_Click(a, b,Trip));
AMSPanel1.Controls.Add(imageButton);
AMSPanel1.Height = Unit.Pixel(860);
}
protected void imageButton_Click(object sender, ImageClickEventArgs e, Tripclass Trip)
{
testimage.ImageUrl = ((ImageButton)sender).ImageUrl;
lblTime.Text = Trip.Time;
lblLocation.Text = Trip.Location; //I can access trip object here
}
If it can be represented as text, you can add your value to CommandArgument property

Need to add TreeListCommandEventArgs in radbutton eventargs

protected void RadButton1_Click(object sender, EventArgs e)
{
RadTreeList1.ItemCommand -= new EventHandler<TreeListCommandEventArgs>(RadTreeList1_ItemCommand);
RadButton radbutton1 = (RadButton)Form.FindControl("RadButton1");
TreeListDataItem dataItem = e.Item as TreeListDataItem;
Hashtable table = new Hashtable();
table["RowId"] = (dataItem.FindControl("Label1") as Label).Text;
table["Alias"] = (dataItem.FindControl("Label2") as Label).Text;
}
There I am getting error at Item as i dont have TreeListCommandEventArgs in radbutton pls help me in this
If I replace EventArgs with TreeListCommandEventArgs that doesnt meaningul and becomes error at runtime.....
protected void RadButton1_Click(object sender, EventArgs e)
{
RadTreeList1.ItemCommand -= new EventHandler<TreeListCommandEventArgs>(RadTreeList1_ItemCommand);
ContentPlaceHolder contentPage = this.Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
RadButton R = sender as RadButton;
RadButton radbutton1 = R.Parent.FindControl("RadButton1") as RadButton;
CommandEventArgs e2 = new CommandEventArgs(null, radbutton1.CommandArgument);
TreeListCommandEventArgs e1 = new TreeListCommandEventArgs(null, radbutton1.CommandArgument, e2);
TreeListDataItem dataItem = e1.Item as TreeListDataItem;
Hashtable table = new Hashtable();
table["RowId"] = (dataItem.FindControl("Label1") as Label).Text;
table["Alias"] = (dataItem.FindControl("Label2") as Label).Text;
}
protected void RadButton1_Click(object sender, EventArgs e)
{
ContentPlaceHolder contentPage = Page.Master.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
RadButton radbutton1 = (RadButton)contentPage.FindControl("RadButton1");
object mysender = (object)radbutton1;
CommandEventArgs e2 = new CommandEventArgs(null, radbutton1.CommandArgument);
RadButton1_Click(mysender, e2);
TreeListCommandEventArgs e1 = new TreeListCommandEventArgs(null,radbutton1.CommandArgument,e2);
TreeListDataItem dataItem = e1.Item as TreeListDataItem;
Hashtable table = new Hashtable();
table["RowId"] = (dataItem.FindControl("Label1") as Label).Text;
table["Alias"] = (dataItem.FindControl("Label2") as Label).Text;
}

C# class to play a video file

I have this class,
public class ImageBox : Grid
{
Image imgclose; public String path;
List<ImageBox> ImageBoxes;
public ImageBox(string label,List<ImageBox> ImBox)
{
this.ImageBoxes = ImBox;
imgclose = new Image();
imgclose.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Close.ico"));
imgclose.Width = 20; imgclose.Height = 20; imgclose.Cursor = Cursors.Hand; imgclose.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; imgclose.VerticalAlignment = System.Windows.VerticalAlignment.Top;
imgclose.Visibility = System.Windows.Visibility.Hidden;
imgclose.MouseLeftButtonDown += new MouseButtonEventHandler(imgclose_MouseLeftButtonDown);
this.MouseEnter += new MouseEventHandler(Blank_MouseEnter);
this.MouseLeave += new MouseEventHandler(Blank_MouseLeave);
this.Height = 100; this.Width = 100;
try
{
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
path = open.FileName.Replace(Directory.GetCurrentDirectory(), "");
this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
path = label;
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
Grid.SetColumn(imgclose, 0); Grid.SetRow(imgclose, 1);
this.Children.Add(imgclose);
ContextMenu contextMenu1 = new ContextMenu();
MenuItem conitem1 = new MenuItem() { Header = "Send to back" }; conitem1.Click += new System.Windows.RoutedEventHandler(conitem1_Click);
MenuItem conitem2 = new MenuItem() { Header = "Bring to Front" }; conitem2.Click += new System.Windows.RoutedEventHandler(conitem2_Click);
contextMenu1.Items.Add(conitem1); contextMenu1.Items.Add(conitem2);
this.ContextMenu = contextMenu1;
}
void conitem1_Click(object sender, EventArgs e)
{
Canvas.SetZIndex(this, (Canvas.GetZIndex(this) - 1));
}
void conitem2_Click(object sender, EventArgs e)
{
Canvas.SetZIndex(this, (Canvas.GetZIndex(this) + 1));
}
void Blank_MouseEnter(object sender, MouseEventArgs e)
{
imgclose.Visibility = System.Windows.Visibility.Visible;
}
void Blank_MouseLeave(object sender, MouseEventArgs e)
{
imgclose.Visibility = System.Windows.Visibility.Hidden;
}
void imgclose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (System.Windows.MessageBox.Show("Are you sure?", "Confirm", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
{
ImageBoxes.Remove(this);
(this.Parent as System.Windows.Controls.Canvas).Children.Remove(this);
}
else
{
}
}
}
This class displays an image (chosen from a dialog box).
How can I modify it to make it play a video file ?
More precisely,
How should I modify the line
this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
path = label;
so that it plays a video file.
The Background property has to have some sort of Brush in it. A quick Google search came up with this: VideoBrush. I hope this is what you were looking for.

Categories