OnButtonClick make an individual cell visible/invisible?

Discussion in 'ASP .Net' started by Roy, Feb 11, 2005.

  1. Roy

    Roy Guest

    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
     
    Roy, Feb 11, 2005
    #1
    1. Advertising

  2. Roy

    Roy Guest

    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!
     
    Roy, Feb 11, 2005
    #2
    1. Advertising

  3. Roy

    Stefano Guest

    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

    "Roy" <> ha scritto nel messaggio
    news:...
    > 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!
    >
     
    Stefano, Feb 11, 2005
    #3
  4. Roy

    Roy Guest

    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?
     
    Roy, Feb 11, 2005
    #4
  5. Roy

    Roy Guest

    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.
     
    Roy, Feb 11, 2005
    #5
  6. Roy

    Roy Guest

    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.... ;)
     
    Roy, Feb 11, 2005
    #6
  7. Roy

    Roy Guest

    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?
     
    Roy, Feb 11, 2005
    #7
  8. Roy

    Roy Guest

    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?
     
    Roy, Feb 12, 2005
    #8
  9. Roy

    Danny Guest

    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
     
    Danny, Feb 14, 2005
    #9
    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. john
    Replies:
    2
    Views:
    7,142
  2. Miguel Dias Moura

    Make panels visible and invisible.

    Miguel Dias Moura, Jun 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    12,591
    Ireney Berezniak
    Jun 21, 2004
  3. Roy
    Replies:
    0
    Views:
    379
  4. Selden McCabe
    Replies:
    5
    Views:
    10,241
    Selden McCabe
    Apr 25, 2005
  5. Replies:
    1
    Views:
    134
Loading...

Share This Page