Groups | Blog | Home
all groups > asp.net datagrid control > november 2005 >

asp.net datagrid control : Takes two clicks to fire event?? Please help!



treilly via DotNetMonster.com
11/28/2005 4:39:56 PM
Hey guys,

This is really starting to annoy me. In short, I have a datagrid with a
buttoncolumn in the first column. Once a user clicks on a button in this
column, it fires the OnItemCommand correctly. In the OnItemCommand function,
I then replace the control that fired with a different one in order to
perform some other actions the next time it is clicked. Everything works as
expected, except it takes two clicks in order to fire the events which
totally screws up my datagrid.

After doing some research on this problem, I THINK I may need to add the
RaiseBubbleEvent and OnBubbleEvent functions to my code. The only problem is,
I've tried doing that with no success. Perhaps I am doing it incorrectly.
Can anyone shed some light on where I may need to add the RaiseBubbleEvent
(this, e) line as well as where to place my OnBubbleEvent function?

I can't think of any other reason why it would take two clicks to fire the
event.
I wanted to post this question without including code because I know how
terrible it is looking through somebody else's code - but I don't think I
could have done it without confusing myself as well as others.
Thanks for the help in advance!

-TR

Here is my code for the OnItemCommand handler:

protected void ItemClick(Object source, DataGridCommandEventArgs e)
{

switch (e.CommandName) //depending on which button the user
clicked
{
case "cmdArrow": //expand the item to show more info,
picture
{

LinkButton downArrow = new LinkButton(); //replace
the old control with the new control
downArrow.ID = "downArrow";
downArrow = (LinkButton)e.Item.Cells[0].Controls[0];
downArrow.Text = "<img src='arrowdownVA.GIF'
border='0'/>";
downArrow.CommandName = "downArrow";


string name; //temporary storage variables
string title;
string location;
string phone;
string emladdress;
string email;
string about;
string imageURLwithTag;
string imageURL;

LinkButton linkBtn = (LinkButton)e.Item.Cells[1].
Controls[0];
name = linkBtn.Text;
title = e.Item.Cells[2].Text;
location = e.Item.Cells[3].Text;
phone = e.Item.Cells[4].Text;
HyperLink hyperLnk = (HyperLink)e.Item.Cells[5].
Controls[0];
emladdress = hyperLnk.Text;
email = "<a href='mailto:" + emladdress + "'>" +
emladdress.ToLowerInvariant() + "</a>";
about = e.Item.Cells[6].Text;
imageURLwithTag = "<img src=" + e.Item.Cells[7].Text
+ "/>"; //width='100px' height='80px'
imageURL = e.Item.Cells[7].Text;

TableCellCollection tcc = e.Item.Cells; //clear all
but the first column's controls
int totalCols = tcc.Count;
for (int i = 1; i < totalCols - 1; i++)
{
e.Item.Cells.RemoveAt(0);

}

e.Item.Cells[0].Controls.Add(downArrow); //add the
new control to first column of datagrid

e.Item.Cells[1].ColumnSpan = 5; //column needs to
span 5 in order to show more info
e.Item.Cells[1].Text = "<div class='imageHolder'>" +
imageURLwithTag + "</div>";
e.Item.Cells[1].Text += "<div class='infoBlock'><div
class='expandedName'>" + name + "</div><br/>"
+ "<div class='expandedTitle'>" + title +
"</div><br/>"
+ "<div class='expandedLocation'>" + location +
"</div><br/>"
+ "<div class='expandedPhone'>" + phone +
"</div><br/>"
+ "<div class='expandedEmail'>" + email +
"</div></div>"; ;
e.Item.Cells[1].Text += "<div class='expandedAbout'>"
+ about + "</div>";

//save this row in sessionstate
dirMember member = new dirMember(name, title,
location, phone, emladdress, about, imageURL);
Session[e.Item.ItemIndex.ToString()] = member;


break;
}
case "downArrow":
{
LinkButton cmdArrow = new LinkButton(); //replace the
old control with the new control
cmdArrow.ID = "cmdArrow";
cmdArrow = (LinkButton)e.Item.Cells[0].Controls[0];
cmdArrow.Text = "<img src='arrowVA.gif' border='0'/>";
cmdArrow.CommandName = "cmdArrow";

TableCellCollection tcc = e.Item.Cells; //clear all but
the first column's controls
int totalCols = tcc.Count;
for (int i = 1; i < totalCols - 1; i++)
{
e.Item.Cells.RemoveAt(0);

}

e.Item.Cells[0].Controls.Add(cmdArrow); //add the
control to first column of datagrid

//restore member's info to original columns
dirMember member = (dirMember)Session[e.Item.ItemIndex.
ToString()];

for(int i = 1; i < DataGrid1.Columns.Count - 1; i++)
//return columns to original spans
{
TableCell tc = new TableCell();
tc.ColumnSpan = 1;
e.Item.Cells.AddAt(i, tc);
}

LinkButton collapsedName = new LinkButton();
collapsedName.CommandName = "cmdArrow";
collapsedName.Text = member.Name;
e.Item.Cells[1].Controls.Add(collapsedName);

e.Item.Cells[2].Text = member.Title; //place the info
from session state back into columns
e.Item.Cells[3].Text = member.Location;
e.Item.Cells[4].Text = member.Phone;
LinkButton collapsedEml = new LinkButton();
collapsedEml.Text = "<a href='mailto:" + member.
Emladdress + "'>" + member.Emladdress + "</a>";
e.Item.Cells[5].Controls.Add(collapsedEml);
e.Item.Cells[6].Text = member.About;
Sylvain Lafontaine
11/28/2005 7:19:59 PM
I don't know about your problem (didn't make any test) but a possible
solution would be to use two buttons and hide/unhide the buttons as
required.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF


[quoted text, click to view]
AddThis Social Bookmark Button