GataGrid Command Link Problem

G

Guest

I have a DataGrid where all the columns can be clicked to sort by that column.

A very strange thing is happening: When the page is first displayed with my
default sort order, a click on one of the ButtonColumn LinkButtons works just
fine.

But if I click on one of the column headings and now my control has been
sorted, if I click on one of the ButtonColumn LinkButtons, I get the wrong
row of data back in the "DataGridCommandEventArgs" parameter to DG_Command().
I get the data from some other row.

Any idea why this might be happening?

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your post.

Based on my understanding, the question is:
1) You are using DataGrid in ASP.NET 1.1 and it's bound to some data
source;
2) It's set to sortable and you're using the SortCommand to sort it based
on the clicked column's SortExpression, and rebind it after sort.
3) You have some ButtonColumn which are associated with the row index in
the CommandArgument
4) After sort, the ButtonColumn's associate row index is not correct
If I've misunderstood anything, please feel free to post here.

I think the most possible cause of this problem would be: when you rebind
the DataGrid after sorting, the ButtonColumn's associated row index might
not be updated.

If this is not the case or you need more help on this, would you mind
posting some code here so that we can work on it more closely? Thanks.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Walter -

I think you have this exactly right - Except that I'm working in ASP.NET
2.0. As to Source code, well, I guess I can give you a stripped down version
that's simpler than the real thing and maybe that'll still help you help me!

SAMPLE CODE:
(See comments that start with "//**" below...

protected void Page_Load(object sender, EventArgs e)
{
LoadIssuesDG(m_FilterName, m_SearchStr, m_SortBy);
}

protected void LoadIssuesDG(string FilterName, string SearchStr, string
SortBy)
{
string SQL = BuildQuery(FilterName, SearchStr, SortBy);

OleDbDataReader DR = DB.DoParamSQLGetDataReader(SQL);
if (DR != null)
{
IssuesDG.DataSource = DR;
IssuesDG.DataBind();
}
else
{
ListDescripLB.Text += "<br/>NO ISSUES RETURNED FOR THIS SEARCH.";
}
}


protected void IssuesDG_Sort(Object sender, DataGridSortCommandEventArgs
e)
{
string SortExpression = e.SortExpression.ToString();
LoadIssuesDG(m_FilterName, m_SearchStr, SortExpression);
}



protected void IssuesDG_Command(Object sender, DataGridCommandEventArgs e)
{
string ID = e.Item.Cells[0].Text;
switch (((LinkButton)e.CommandSource).CommandName)
{
case "Edit":
EditItem(ID); //** BEFORE SORT, WORKS FINE. AFTER SORT, ID
IS INCORRECT!
break;

case "View":
ViewItem(ID); //** BEFORE SORT, WORKS FINE. AFTER SORT, ID
IS INCORRECT!
break;

default:
// Do nothing.
break;

}
}

THANKS SO MUCH FOR YOUR HELP!

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your update.

The control's event is fired after the Page_Load event, I saw that you are
always reloading the DataGrid in Page_Load event with following code:

LoadIssuesDG(m_FilterName, m_SearchStr, m_SortBy);

That's fine as long as the m_SortBy variable is persisted between
postbacks, i.e., it's reflected to the current sorted column's key, how did
you persist it?

Normally it will be persisted as follows:

public string SortKey
{
set { ViewState["A"] = value;}
get { return ViewState["A"] as string; }
}

Then in the LoadIssuesDG function, it will be updated:

SortKey = SortBy;

In Page_Load, it will be:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SortKey = "CustomerID";
}
LoadIssuesDG(SortKey);
}


Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Alex,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Wlater -

Hi. Thanks for checking back with me about this. Unfortunately, I have NOT
solved it and it's driving me nuts! I read your advice about persisting the
SortKey in the ViewState but, truth is, I don't think I need to do that and
here's why:

The SortKey is presented to me when the user clicks the header of the column
they want to Sort by. I understand that if I had some other form of
Post-Back, I'd have to persist this value, but for this example, I've checked
and I alsways have a valid Sort Experession. Further, and I think more
importantly, the sorting is obviously happening properly as the textual rows
that I can SEE have, in fact, been sorted appropriately. But what's so
strange is that even though the text rows are sorted, the Link Columns still
point back to the rows as they were originally sorted!

I'm wondering if maybe this is because in the PostBack to Sort scenario, I'm
calling my LoadDG(SortExpression) function twice: the first time in the
PageLoad and then againimmediately thereafter in the DG_Sort() handler, with
the proper new SortOrder. I don't WANT to be doing this but I've noticed that
if I don't populate and data-bind my DataGrid in PageLoad() and I wait 'til
the DG_Sort does it, for some reason the DataGrid doesn't display at all.

So first, is it possible that the row Link Buttons are getting screwed up
because I'm data binding my DataGrid twice in one page loading?

And Second, why do I have to always populate my DataGrid in PageLoad if I
*know* I'm going to be re-Populating it with a defferent sort order in the
DG_Sort handler?

Alex
 
G

Guest

Well, I've finally figured this out and it's a little NUTS! What was
happening was that the list was displayed properly after Sort and all that
BUT, when the user clicked a CommanLink on one of the rows, upon post-back,
apparently, the control needs the data to have been populated again in the
same order so that the right row can be passed along to the event handler for
the link.

Thanks for your help.

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your update and I'm glad the problem is solved.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top