Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > ItemCommand not working when datagrid can page and sort

Reply
Thread Tools

ItemCommand not working when datagrid can page and sort

 
 
pietaster
Guest
Posts: n/a
 
      04-07-2004
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?!?!??!?!?!??!?!?!?!??
 
Reply With Quote
 
 
 
 
pietaster
Guest
Posts: n/a
 
      04-13-2004
Where is Microsoft? Can anyone answer this question? This is a big problem.
 
Reply With Quote
 
 
 
 
pietaster
Guest
Posts: n/a
 
      04-13-2004
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.Replac e("-",""), 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_PageIndexChange d)
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....

 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      04-13-2004

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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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.Replac e("-",""),

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_PageIndexChange d);
> 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.....
>



 
Reply With Quote
 
pietaster
Guest
Posts: n/a
 
      04-13-2004
I have an ItemCommand eventhandler, it is being ignored. I even tried the linkbutton click eventhandler and it is being ignored as well.
 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      04-13-2004

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have an ItemCommand eventhandler, it is being ignored. I even tried the linkbutton click

eventhandler and it is being ignored as well.


 
Reply With Quote
 
pietaster
Guest
Posts: n/a
 
      04-13-2004
private void LoadDataGrid(
{
dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChange d)
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"


 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      04-14-2004
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_PageIndexChange d);
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> private void LoadDataGrid()
> {
> dg.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChange d);
> 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";
> }
>

 
Reply With Quote
 
pietaster
Guest
Posts: n/a
 
      04-14-2004
The ItemCommand is fired when "paging". That is clicking the "next" button. The ItemCommand is NOT fired when clicking on the linkbutton.
 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      04-14-2004

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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> The ItemCommand is fired when "paging". That is clicking the "next" button. The ItemCommand is NOT fired when clicking on the

linkbutton.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Image Button Not Working With DataGrid ItemCommand? Adam Knight ASP .Net 1 02-06-2006 07:49 AM
DataGrid ItemCommand event not firing if not first page =?Utf-8?B?ZGFuYw==?= ASP .Net 3 10-26-2005 05:24 PM
ItemIndex is -1 in ItemCommand after selecting page in DataGrid Alan Eilers ASP .Net Datagrid Control 1 05-12-2004 11:17 PM
Re: Datalist itemcommand not working via usercontrol Marina ASP .Net 3 02-29-2004 05:14 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments