ItemCommand problems

Discussion in 'ASP .Net Datagrid Control' started by Mark Kelly, Jan 23, 2004.

  1. Mark Kelly

    Mark Kelly Guest

    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;

    }
     
    Mark Kelly, Jan 23, 2004
    #1
    1. Advertising

  2. Mark Kelly

    Bob Clegg Guest

    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" <> wrote in message
    news:...
    > 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;
    >
    > }
    >
    >
    >
    >
    >
    >
     
    Bob Clegg, Jan 23, 2004
    #2
    1. Advertising

  3. Mark Kelly

    Mark Kelly Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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;
    > >
    > > }
    > >
    > >
    > >
    > >
    > >
    > >

    >
    >
     
    Mark Kelly, Jan 26, 2004
    #3
  4. Mark Kelly

    Mark Kelly Guest

    Sorry, I meant User Control, not Custom Control!

    "Mark Kelly" <> wrote in message
    news:OKPg$...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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;
    > > >
    > > > }
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Mark Kelly, Jan 26, 2004
    #4
  5. Mark Kelly

    werddomain

    Joined:
    Oct 24, 2011
    Messages:
    2
    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
     
    werddomain, Feb 16, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    583
    Natty Gur
    Jun 30, 2003
  2. Jonas Nilsson

    ItemCommand Event fires twice

    Jonas Nilsson, Aug 1, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,071
    Jonas Nilsson
    Aug 1, 2003
  3. Mike

    DataGrid ItemCommand Event

    Mike, Oct 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    498
    Patrik Löwendahl
    Oct 24, 2003
  4. Steven
    Replies:
    4
    Views:
    1,547
    Steven
    Oct 25, 2003
  5. Mark Kelly

    Datagrid ItemCommand problems

    Mark Kelly, Jan 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,097
    Mark Kelly
    Jan 29, 2004
Loading...

Share This Page