ItemCommand not working when datagrid can page and sort

Discussion in 'ASP .Net Datagrid Control' started by pietaster, Apr 7, 2004.

  1. pietaster

    pietaster Guest

    I have a datagrid that pages and sorts. I need to use the itemcommand
    of the datagrid so when a user clicks the link I can do some code.
    Well Im sure there are people who know this. Please help and tell me
    what I need to do to get this to work.

    I know this because I created another datagrid that does not page or
    sort and the itemcommand is called everytime.


    What can I do?!?!??!?!?!??!?!?!?!??
    pietaster, Apr 7, 2004
    #1
    1. Advertising

  2. pietaster

    pietaster Guest

    Where is Microsoft? Can anyone answer this question? This is a big problem.
    pietaster, Apr 13, 2004
    #2
    1. Advertising

  3. pietaster

    pietaster Guest

    Let me give you some details about the code. The page loads and there is a radiobuttonlist and a textbox. The user enters an account number, selects a radio button, click on the go button. The datagrid loads with the data, The datagrid allows sorting and paging. A linkbutton gets created in the datagrid_itemdatabound along with formatting of data..

    Here is my Page_Load and btngo_Click

    private void Page_Load(object sender, System.EventArgs e
    {
    //Setting Sessions
    Session["AccountNumber"] = ""
    Session["AcctInfo"] = ""

    //javascript events loading..
    Page.RegisterStartupScript("focus", "<script language='javascript'>document.all['txtAccount'].focus()</script>")
    txtAccount.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.CDForm.btnGo.click();return false;} else return true;")
    rblelmr.Attributes.Add("onClick", "Input();"); //Used to adjust textbox length when rb is checked.


    public void btnGo_Click(object sender, EventArgs e
    {
    dg.AllowPaging = true
    dg.PageSize = 25
    dg.CurrentPageIndex = 0
    dg.PagerStyle.NextPageText = "<font face='webdings' size='2pt'>4</font>"
    dg.PagerStyle.PrevPageText = "<font face='webdings' size='2pt'>3</font>"
    dg.PagerStyle.Mode = PagerMode.NextPrev;
    dg.PagerStyle.Position = PagerPosition.TopAndBottom
    dg.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
    bool check = SearchBizLogic.CheckAccount(txtAccount.Text.Replace("-",""), rblelmr.SelectedItem.Value)
    if(check == true

    BindData(1, "asc")



    So, let me reiterate, The datagrid does NOT get loaded until btngo gets click

    Here is my OnInt

    override protected void OnInit(EventArgs e

    /
    // CODEGEN: This call is required by the ASP.NET Web Form Designer
    /
    InitializeComponent()
    EventLoader()
    LoadDataGrid()
    base.OnInit(e)


    private void InitializeComponent(
    {
    this.Load += new System.EventHandler(this.Page_Load)


    private void EventLoader(

    btnGo.Click += new EventHandler(btnGo_Click);


    private void LoadDataGrid(
    {
    dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChanged)
    dg.SortCommand +=new DataGridSortCommandEventHandler(dg_SortCommand)
    dg.ItemDataBound += new DataGridItemEventHandler(dg_ItemDataBound)
    dg.ItemCreated += new DataGridItemEventHandler(dg_ItemCreated)
    dg.AllowSorting = true

    ViewState["Account Number"] = "desc"
    ViewState["Customer"] = "desc"


    Please help with this....
    pietaster, Apr 13, 2004
    #3
  4. pietaster

    Scott Guest

    Is your problem that you want an event handler when the user clicks on an item in the datagrid?
    Then you might create a event handler for the ItemCommand.

    Also, 'document.all' is deprecated; you might try using getElementById to set the focus.

    Scott

    "pietaster" <> wrote in message
    news:D...
    > Let me give you some details about the code. The page loads and there is a radiobuttonlist and a

    textbox. The user enters an account number, selects a radio button, click on the go button. The
    datagrid loads with the data, The datagrid allows sorting and paging. A linkbutton gets created in
    the datagrid_itemdatabound along with formatting of data...
    >
    > Here is my Page_Load and btngo_Click:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > //Setting Sessions.
    > Session["AccountNumber"] = "";
    > Session["AcctInfo"] = "";
    >
    > //javascript events loading...
    > Page.RegisterStartupScript("focus", "<script

    language='javascript'>document.all['txtAccount'].focus()</script>");
    > txtAccount.Attributes.Add("onkeydown", "if ((event.which && event.which == 13) ||

    (event.keyCode && event.keyCode == 13)) {document.CDForm.btnGo.click();return false;} else return
    true;");
    > rblelmr.Attributes.Add("onClick", "Input();"); //Used to adjust textbox length when rb is

    checked.
    > }
    >
    > public void btnGo_Click(object sender, EventArgs e)
    > {
    > dg.AllowPaging = true;
    > dg.PageSize = 25;
    > dg.CurrentPageIndex = 0;
    > dg.PagerStyle.NextPageText = "<font face='webdings' size='2pt'>4</font>";
    > dg.PagerStyle.PrevPageText = "<font face='webdings' size='2pt'>3</font>";
    > dg.PagerStyle.Mode = PagerMode.NextPrev;
    > dg.PagerStyle.Position = PagerPosition.TopAndBottom;
    > dg.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
    > bool check = SearchBizLogic.CheckAccount(txtAccount.Text.Replace("-",""),

    rblelmr.SelectedItem.Value);
    > if(check == true)
    > {
    > BindData(1, "asc");
    > }
    > }
    >
    > So, let me reiterate, The datagrid does NOT get loaded until btngo gets click.
    >
    > Here is my OnInt:
    >
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > EventLoader();
    > LoadDataGrid();
    > base.OnInit(e);
    > }
    >
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    >
    > private void EventLoader()
    > {
    > btnGo.Click += new EventHandler(btnGo_Click);
    > }
    >
    > private void LoadDataGrid()
    > {
    > dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChanged);
    > dg.SortCommand +=new DataGridSortCommandEventHandler(dg_SortCommand);
    > dg.ItemDataBound += new DataGridItemEventHandler(dg_ItemDataBound);
    > dg.ItemCreated += new DataGridItemEventHandler(dg_ItemCreated);
    > dg.AllowSorting = true;
    >
    > ViewState["Account Number"] = "desc";
    > ViewState["Customer"] = "desc";
    > }
    >
    > Please help with this.....
    >
    Scott, Apr 13, 2004
    #4
  5. pietaster

    pietaster Guest

    I have an ItemCommand eventhandler, it is being ignored. I even tried the linkbutton click eventhandler and it is being ignored as well.
    pietaster, Apr 13, 2004
    #5
  6. pietaster

    Scott Guest

    In the code you attached there's no event handler for the item command; how about posting the
    declaration where you attach the ItemCommand event handler?

    Scott


    "pietaster" <> wrote in message
    news:...
    > I have an ItemCommand eventhandler, it is being ignored. I even tried the linkbutton click

    eventhandler and it is being ignored as well.
    Scott, Apr 13, 2004
    #6
  7. pietaster

    pietaster Guest

    private void LoadDataGrid(
    {
    dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChanged)
    dg.SortCommand +=new DataGridSortCommandEventHandler(dg_SortCommand)
    dg.ItemDataBound += new DataGridItemEventHandler(dg_ItemDataBound)
    dg.ItemCreated += new DataGridItemEventHandler(dg_ItemCreated)
    dg.ItemCommand += new DataGridCommandEventHandler(dg_ItemCommand)
    dg.AllowSorting = true

    ViewState["Account Number"] = "desc"
    ViewState["Customer"] = "desc"
    pietaster, Apr 13, 2004
    #7
  8. pietaster

    Scott Guest

    This works -- create an ASPX page and CnP this into it; seems like you are likely doing someting incorrect with your data binding.

    <%@ Import Namespace="System.Data" %>
    <%@ Page language="c#" AutoEventWireup="false" Trace="true" %>
    <html>
    <script language="C#" runat="server">
    protected override void OnLoad(EventArgs e)
    {
    if (!IsPostBack)
    {
    ViewState["Customer"] = "";
    ViewState["AccountNumber"] = "";
    SortExpression = "Customer";
    }
    }

    protected override void OnInit(EventArgs e)
    {
    EventLoader();
    LoadDataGrid();
    base.OnInit(e);
    }

    private void EventLoader()
    {
    this.Button1.Click += new EventHandler(Button1_Click);
    }

    public void Button1_Click(object sender, EventArgs e)
    {
    dg.AllowPaging = true;
    dg.PageSize = 10;
    dg.CurrentPageIndex = 0;
    dg.PagerStyle.NextPageText = "<font face='webdings' size='2pt'>4</font>";
    dg.PagerStyle.PrevPageText = "<font face='webdings' size='2pt'>3</font>";
    dg.PagerStyle.Mode = PagerMode.NextPrev;
    dg.PagerStyle.Position = PagerPosition.TopAndBottom;
    dg.PagerStyle.HorizontalAlign = HorizontalAlign.Center;

    dg.DataSource = BindData(1, "asc");
    dg.DataBind();
    }

    private DataView BindData(int foo, string bar)
    {
    DataSet dataSet = new DataSet();
    DataTable table = new DataTable();
    table.Columns.Add("AccountNumber");
    table.Columns.Add("Customer");
    for (int i = 100; i < 200; i++)
    {
    DataRow row = table.NewRow();
    row["AccountNumber"] = i.ToString();
    row["Customer"] = "Customer Name (" + i.ToString() + ")";
    table.Rows.Add(row);
    }
    dataSet.Tables.Add(table);
    DataView view = new DataView(dataSet.Tables[0]);
    view.Sort = this.SortExpression;
    return view;
    }

    private void LoadDataGrid()
    {
    dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChanged);
    dg.SortCommand +=new DataGridSortCommandEventHandler(dg_SortCommand);
    dg.ItemDataBound += new DataGridItemEventHandler(dg_ItemDataBound);
    dg.ItemCreated += new DataGridItemEventHandler(dg_ItemCreated);
    dg.ItemCommand += new DataGridCommandEventHandler(dg_ItemCommand);
    dg.AllowSorting = true;

    ViewState["Account Number"] = "desc";
    ViewState["Customer"] = "desc";
    }

    private void dg_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
    DataView view = BindData(1, "asc");
    view.Sort = this.SortExpression;
    dg.CurrentPageIndex = e.NewPageIndex;
    dg.DataSource = view;
    dg.DataBind();
    }

    private string SortExpression
    {
    get { return ViewState["SortExp"].ToString(); }
    set { ViewState["SortExp"] = value; }
    }

    private void dg_SortCommand(object sender, DataGridSortCommandEventArgs e)
    {
    DataView view = BindData(1, "asc");
    if (ViewState[e.SortExpression].ToString().Equals("desc"))
    {
    ViewState[e.SortExpression] = "asc";
    }
    else
    {
    ViewState[e.SortExpression] = "desc";
    }
    this.SortExpression = view.Sort = e.SortExpression + " " + ViewState[e.SortExpression];
    dg.DataSource = view;
    dg.DataBind();
    }

    private void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
    }

    private void dg_ItemCreated(object sender, DataGridItemEventArgs e)
    {
    }

    private void dg_ItemCommand(object sender, DataGridCommandEventArgs e)
    {
    if (e.Item.ItemIndex >= 0)
    {
    DataView view = BindData(1, "asc");
    view.Sort = this.SortExpression;
    int idx = (e.Item.ItemIndex + (dg.CurrentPageIndex * dg.PageSize));
    this.Trace.Warn("SELECTED = " + view[idx]["Customer"]);
    }
    }
    </script>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:textbox id="TextBox1" runat="server"></asp:textbox>&nbsp;
    <asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click"></asp:button>
    <asp:datagrid id="dg" runat="server" autogeneratecolumns="False">
    <columns>
    <asp:buttoncolumn text="Select" datatextfield="AccountNumber" sortexpression="AccountNumber" headertext="AccountNumber"
    commandname="Select"></asp:buttoncolumn>
    <asp:boundcolumn datafield="Customer" sortexpression="Customer" headertext="Customer"></asp:boundcolumn>
    </columns>
    </asp:datagrid>
    </form>
    </body>
    </html>


    "pietaster" <> wrote in message news:...
    > private void LoadDataGrid()
    > {
    > dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChanged);
    > dg.SortCommand +=new DataGridSortCommandEventHandler(dg_SortCommand);
    > dg.ItemDataBound += new DataGridItemEventHandler(dg_ItemDataBound);
    > dg.ItemCreated += new DataGridItemEventHandler(dg_ItemCreated);
    > dg.ItemCommand += new DataGridCommandEventHandler(dg_ItemCommand);
    > dg.AllowSorting = true;
    >
    > ViewState["Account Number"] = "desc";
    > ViewState["Customer"] = "desc";
    > }
    >
    Scott, Apr 14, 2004
    #8
  9. pietaster

    pietaster Guest

    The ItemCommand is fired when "paging". That is clicking the "next" button. The ItemCommand is NOT fired when clicking on the linkbutton.
    pietaster, Apr 14, 2004
    #9
  10. pietaster

    Scott Guest

    There's a test in dg_ItemCommand that checks the ItemIndex; one could also check the CommandName and CommandArgument to verify that
    a "Select" has occurred.

    If you ran the sample I posted you should see in the trace output when the item is selected, ie. clicking on the link button. The
    sorting and paging should also work.

    "pietaster" <> wrote in message news:...
    > The ItemCommand is fired when "paging". That is clicking the "next" button. The ItemCommand is NOT fired when clicking on the

    linkbutton.
    Scott, Apr 14, 2004
    #10
  11. pietaster

    pietaster Guest

    Can I email you my code so you can check it out and tell me what I am doing wrong?
    pietaster, Apr 14, 2004
    #11
  12. pietaster

    pietaster Guest

    I tried your code and it works, my code does not. Maybe you can look at it.
    pietaster, Apr 14, 2004
    #12
  13. pietaster

    Scott Guest

    If you post the code I'll look at it; before you do that -- compare what I posted to your code, in particular the ASP spec for the link column. Make sure that it contains the various command bits. Also make sure that you aren't doing a DataBind someplace that's shorting out the event.

    Scott
    "pietaster" <> wrote in message news:...
    I tried your code and it works, my code does not. Maybe you can look at it.
    Scott, Apr 15, 2004
    #13
  14. pietaster

    pietaster Guest

    I got it to work with your help. Thank You very much. How do I format my data before it gets rendered? I know there is a formatstring...... How do I format the data without using the 'Text format string' in the 'Property Builder'????
    pietaster, Apr 15, 2004
    #14
    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. Marina
    Replies:
    3
    Views:
    2,785
    Chad Crowder
    Feb 29, 2004
  2. =?Utf-8?B?ZGFuYw==?=

    DataGrid ItemCommand event not firing if not first page

    =?Utf-8?B?ZGFuYw==?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,150
    =?Utf-8?B?ZGFuYw==?=
    Oct 26, 2005
  3. Adam Knight
    Replies:
    1
    Views:
    2,795
    =?Utf-8?B?c2VyZ2UgY2FsZGVyYXJh?=
    Feb 6, 2006
  4. RCorona
    Replies:
    1
    Views:
    163
    Al Cadalzo
    Jan 1, 2005
  5. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page