ItemCommand problems

M

Mark Kelly

Hi, I don't seem to be able to get a imagebutton (within a template column)
to fire an itemcommand event when clicked. Looking through this NG, it seems
like the same problem has been raised many times, except in most cases the
problem occurs as the datagrid's databind method is being called on
Postback - something that definately isn't happening in my code.

I also have a standard ButtonColumn labled 'test' - which when clicked seems
to work fine, i.e the dgFileList_ItemCommand fucntion is executed.

Can anybody tell me why the event isn't being raised by a click of the
imagebutton (it's definately doing a postback, but thats all)? Is it
anything to do with the fact its a user control?

Any help is greatly appreciated

Here are the relevant sections of my code:
..ascx
<asp:datagrid id="dgFileList" Enabled="False" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:ButtonColumn Text="test"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "ThumbURL")%>' id="ImageSelectButton"
runat="server">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>


..ascx.cs
public abstract class FlieList : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.DataGrid dgFileList;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//this.ShowFiles();

}

public void ShowFiles()

{

//Datagrid - Get file list

Files files = new Files();

FileItem[] fileList = files.GetFileList();

dgFileList.DataSource = fileList;

dgFileList.DataBind();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dgFileList.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgFileList_ItemCo
mmand);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void dgFileList_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string s = "test";

string y = "anothertest";

if(e.CommandName=="ClickMe")

{

s = "test";

y = "anothertest";



}

s += y;

}
 
B

Bob Clegg

Hi Mark,
I have a similar problem which seemed to be solved by allowing databinding
in the Postback.
ie. Initially My pageload only had databinding for the grid in the not
ispostback section of an if statement.
Grid worked OK till I decided to programmatically add a template column with
a item button control.
Initial load was OK but press the button and the column disappeared and no
way was it firing the ItemCommand event.
In desperation I added a databind to the postback side of the If statement.
As soon as I did that the column stayed on screen and the test code in the
ItemCommand event handler fired.

I am new to Web development and Webforms so I don't know yet if this is a
fix or just a dead end that will fail with further testing.

FWIW.
regards
Bob

Mark Kelly said:
Hi, I don't seem to be able to get a imagebutton (within a template column)
to fire an itemcommand event when clicked. Looking through this NG, it seems
like the same problem has been raised many times, except in most cases the
problem occurs as the datagrid's databind method is being called on
Postback - something that definately isn't happening in my code.

I also have a standard ButtonColumn labled 'test' - which when clicked seems
to work fine, i.e the dgFileList_ItemCommand fucntion is executed.

Can anybody tell me why the event isn't being raised by a click of the
imagebutton (it's definately doing a postback, but thats all)? Is it
anything to do with the fact its a user control?

Any help is greatly appreciated

Here are the relevant sections of my code:
.ascx
<asp:datagrid id="dgFileList" Enabled="False" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:ButtonColumn Text="test"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "ThumbURL")%>' id="ImageSelectButton"
runat="server">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>


.ascx.cs
public abstract class FlieList : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.DataGrid dgFileList;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//this.ShowFiles();

}

public void ShowFiles()

{

//Datagrid - Get file list

Files files = new Files();

FileItem[] fileList = files.GetFileList();

dgFileList.DataSource = fileList;

dgFileList.DataBind();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dgFileList.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgFileList_ItemCo
mmand);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void dgFileList_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string s = "test";

string y = "anothertest";

if(e.CommandName=="ClickMe")

{

s = "test";

y = "anothertest";



}

s += y;

}
 
M

Mark Kelly

Hi Bob

Thanks for your response, I tried your suggestion, but unfortunately it
didn't work. I'm wondering if the problem is occuring because this DataGrid
is within a custom control and whether I may need to override the
CreateChildControls() method??? I really don't know enough about this so
I'll have to investigate further. If anybody has any suggestions I'd greatly
appreciate it.

Thanks

Mark


Bob Clegg said:
Hi Mark,
I have a similar problem which seemed to be solved by allowing databinding
in the Postback.
ie. Initially My pageload only had databinding for the grid in the not
ispostback section of an if statement.
Grid worked OK till I decided to programmatically add a template column with
a item button control.
Initial load was OK but press the button and the column disappeared and no
way was it firing the ItemCommand event.
In desperation I added a databind to the postback side of the If statement.
As soon as I did that the column stayed on screen and the test code in the
ItemCommand event handler fired.

I am new to Web development and Webforms so I don't know yet if this is a
fix or just a dead end that will fail with further testing.

FWIW.
regards
Bob

Mark Kelly said:
Hi, I don't seem to be able to get a imagebutton (within a template column)
to fire an itemcommand event when clicked. Looking through this NG, it seems
like the same problem has been raised many times, except in most cases the
problem occurs as the datagrid's databind method is being called on
Postback - something that definately isn't happening in my code.

I also have a standard ButtonColumn labled 'test' - which when clicked seems
to work fine, i.e the dgFileList_ItemCommand fucntion is executed.

Can anybody tell me why the event isn't being raised by a click of the
imagebutton (it's definately doing a postback, but thats all)? Is it
anything to do with the fact its a user control?

Any help is greatly appreciated

Here are the relevant sections of my code:
.ascx
<asp:datagrid id="dgFileList" Enabled="False" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:ButtonColumn Text="test"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "ThumbURL")%>' id="ImageSelectButton"
runat="server">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>


.ascx.cs
public abstract class FlieList : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.DataGrid dgFileList;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//this.ShowFiles();

}

public void ShowFiles()

{

//Datagrid - Get file list

Files files = new Files();

FileItem[] fileList = files.GetFileList();

dgFileList.DataSource = fileList;

dgFileList.DataBind();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dgFileList.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgFileList_ItemCo
mmand);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void dgFileList_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string s = "test";

string y = "anothertest";

if(e.CommandName=="ClickMe")

{

s = "test";

y = "anothertest";



}

s += y;

}
 
M

Mark Kelly

Sorry, I meant User Control, not Custom Control!

Mark Kelly said:
Hi Bob

Thanks for your response, I tried your suggestion, but unfortunately it
didn't work. I'm wondering if the problem is occuring because this DataGrid
is within a custom control and whether I may need to override the
CreateChildControls() method??? I really don't know enough about this so
I'll have to investigate further. If anybody has any suggestions I'd greatly
appreciate it.

Thanks

Mark


Bob Clegg said:
Hi Mark,
I have a similar problem which seemed to be solved by allowing databinding
in the Postback.
ie. Initially My pageload only had databinding for the grid in the not
ispostback section of an if statement.
Grid worked OK till I decided to programmatically add a template column with
a item button control.
Initial load was OK but press the button and the column disappeared and no
way was it firing the ItemCommand event.
In desperation I added a databind to the postback side of the If statement.
As soon as I did that the column stayed on screen and the test code in the
ItemCommand event handler fired.

I am new to Web development and Webforms so I don't know yet if this is a
fix or just a dead end that will fail with further testing.

FWIW.
regards
Bob

Mark Kelly said:
Hi, I don't seem to be able to get a imagebutton (within a template column)
to fire an itemcommand event when clicked. Looking through this NG, it seems
like the same problem has been raised many times, except in most cases the
problem occurs as the datagrid's databind method is being called on
Postback - something that definately isn't happening in my code.

I also have a standard ButtonColumn labled 'test' - which when clicked seems
to work fine, i.e the dgFileList_ItemCommand fucntion is executed.

Can anybody tell me why the event isn't being raised by a click of the
imagebutton (it's definately doing a postback, but thats all)? Is it
anything to do with the fact its a user control?

Any help is greatly appreciated

Here are the relevant sections of my code:
.ascx
<asp:datagrid id="dgFileList" Enabled="False" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:ButtonColumn Text="test"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "ThumbURL")%>' id="ImageSelectButton"
runat="server">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>


.ascx.cs
public abstract class FlieList : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.DataGrid dgFileList;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//this.ShowFiles();

}

public void ShowFiles()

{

//Datagrid - Get file list

Files files = new Files();

FileItem[] fileList = files.GetFileList();

dgFileList.DataSource = fileList;

dgFileList.DataBind();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dgFileList.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgFileList_ItemCo
 
Joined
Oct 24, 2011
Messages
2
Reaction score
0
Hi all,
I have this problem in a repeater when i use ImageButton ...
i have search the net for this solution when LinkButton work, but not ImageButton ...

then i think, LinkButton work? so i will use it :)
<asp:LinkButton CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>

</asp:LinkButton>
So, the image is inside the <A> tag

have fun :)

WerdDomain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top