OnButtonClick make an individual cell visible/invisible?

R

Roy

What is the best way to approach this problem? I have a datagrid with
an imagebutton. When one clicks the imagebutton it triggers an onClick
sub which makes column 9 (a user control) visible or invisible. Only
problem is when one clicks the imagebutton on a specific row, I want
*only* that row's column 9 to appear or disappear.


********My imagebutton*******************
<asp:TemplateColumn><ItemTemplate>
<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="ImageButton 1" ImageAlign="left"
ImageUrl="arrow_right.gif" OnClick="ImageButton_Click"/>
</ItemTemplate></asp:TemplateColumn>


*********My user control (row 9)*********
<asp:TemplateColumn Visible = "false">
<ItemTemplate><tr><td colspan="9">
<UC_Grid:Nested id="FVDGrid" x=<%# container.dataitem("vown")%> y=<%#
container.dataitem("van_no")%> runat="server" />
</td></tr></ItemTemplate>
</asp:TemplateColumn>


**********My click sub**********
Public Sub ImageButton_Click(sender As Object, e As
ImageClickEventArgs)
If MR_Grid.Columns(9).Visible = False Then
MR_Grid.Columns(9).Visible = True
Else
MR_Grid.Columns(9).Visible = False
End If
End Sub


I tried modifying the sub with the individual cell as seen below, but
that bombs out with your average "'Item' is not a member of
'System.Web.UI.ImageClickEventArgs'" error. Anyone have any clues on
how I can implement this functionality?

Public Sub ImageButton_Click(sender As Object, e As
ImageClickEventArgs)
If e.Item.Cells(9).Visible = False Then
e.Item.Cells(9).Visible = True
Else
e.Item.Cells(9).Visible = False
End If
End Sub
 
R

Roy

I should have mentioned this above... my current code does make column
9 visible/invisible when the button is clicked. But it makes *all* of
the datagrid's column 9's visible/invisible. I wish just the row
clicked to reveal or conceal it's column 9.

Thanks!
 
S

Stefano

Hi Roy,

I think you should put all inside a panel, or <div runat="server">, and then
set its visible property to false.

For example:

<asp:TemplateColumn Visible = "false">
<ItemTemplate><tr><td colspan="9">

<div id="mypanel" runat="server">

<UC_Grid:Nested id="FVDGrid" x=<%# container.dataitem("vown")%> y=<%#
container.dataitem("van_no")%> runat="server" />

</div>

</td></tr></ItemTemplate>
</asp:TemplateColumn>

Public Sub ImageButton_Click(sender As Object, e As
ImageClickEventArgs)
Dim mypanel as Control = MR_Grid.Columns(9).FindControl("mypanel")

If mypanel.Visible = False Then
mypanel.Visible = True
Else
mypanel.Visible = False
End If
End Sub

HTH

Bye,
Stefano
 
R

Roy

Thanks for the reply Stefano, but the code doesn't work. :(

After some extensive tweaking of your code I can get the page to
display, but it always displays all the columns as hidden, regardless
of if I hit the imagebutton or not. At the end of the day, I think the
code above isn't the way to go as it still doesn't solve the problem of
telling the computer exactly which row's column should be
visible/invisible. MR_Grid.Columns(9).FindControl("mypanel") is still
broadly referencing all the grids columns to the compiler. :(

Any other thoughts?
 
R

Roy

I took out the Columns(9) part of your code above because FindControl
isn't a member of it and generates errors.

**********
Public Sub ImageButton_Click(sender As Object, e As
ImageClickEventArgs)
Dim mypanel as Control = MR_Grid.FindControl("mypanel")
If mypanel.Visible = False Then <<<<<<----ERROR HERE
mypanel.Visible = True
Else
mypanel.Visible = False
End If
End Sub
***********
Only problem now is that an error is generated as shown above. The
error is "Object reference not set to an instance of an object" which I
suspect is because the FindControl is not finding anything and setting
mypanel to null.
 
R

Roy

Correction. The .Visible property cannot be applied to a control (i.e.,
"mypanel").

SIGH

Maybe I should rename the thread "Social Security "crisis" solved!"
betcha I'd get a lot more input.... ;)
 
R

Roy

OK! Solved it...sorta...Behold the power of this FULLY OPERATIONAL
deathst---datagrid, I mean...

Public Sub ImageButton_Click(sender As Object, e As
ImageClickEventArgs)
If sender.Parent.Parent.Cells(9).Visible = False Then
sender.Parent.Parent.Cells(9).Visible = True
Else
sender.Parent.Parent.Cells(9).Visible = False
End If
End Sub

But here's the rub. No matter where I stick the visible="false" in the
datagrid, whether in the <TemplateColumn> tag or User Control tag it
always overrides the visible setting that the imagebutton_click
provides. Thus, if I remove the visible="false" from the .aspx page it
runs fine, openly and closing just the selected row I desired. The only
problem with that is that the default visible setting is now "true,"
which is something I don't want.

Soooo close.... tips anyone?
 
R

Roy

Don't mean to be a pest, but I have to ask a final time as I still
can't resolve this small issue...

Advice anyone?
 
D

Danny

Here's my two cents. I think Stefano's idea was close...but the
problem is your making the id of that <div> tag the same for every
row....so when you do the Columns(9).findControl() it returns that
control from each row.

You could try to find a way to change the ID of that <div> tag for each
row, and then do the FindControl() with that ID that is unique.

You could try using the primary key of the data you get from the
database and sticking it in there - and find that Control....

-OR-

Remembering back to our posts on the MSDN managed newsgroup...

http://msdn.microsoft.com/newsgroup...trol&mid=e9800a41-33ee-4f46-a43f-700d941c8e3d

This problem you are having is pretty much the reason why I use the
Selected Index and/or the EditItemIndex property. That's a good way
to get that one specific row.

Here's one thing you could try:

1. In the <asp:DataGrid id="MR_Grid"..... > tag, add the
attribute OnSelectedIndexChanged="MR_Grid_Select" or something like
that.

2. Also in your <asp:ImageButton ....> tag, add the attribute
CommandName="Select"

3. Create your public sub called MR_Grid_Select(object sender,
EventArgs e)

4. Inside this function you can call get the attribute of the
DataGrid. me.MR_Grid.SelectedItem and this is the actual row you just
clicked on.

5. Now you can do what stephano said - basically
Dim myGrid as DataGrid =
me.MR_Grid.SelectedItem.Cells(9).FindControl("FVDGrid")

If mypanel.Visible = False Then
Mypanel.Visible = true
Else
Mypanel.Visible = false
End if

(If you only have one control in the column this will work too
me.MR_Grid.SelectedItem.Cells(9).Controls[0]... )

Forgive me, I'm no expert on VB.NET, so if syntax is wrong, sorry.

Hope this Helps.

-Danny
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top