Datagrid ItemCommand problems

M

Mark Kelly

(originally posted on framework.aspnet.datagridcontrol)

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;

}
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top