Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

How to check for a row and remove it based on a commandfield click

Ok, I am not sure how I can undo my creation of rows based on the user clicking my image button.

So far, what works, is when the user clicks on the commandfield control below in my GridView, the images changes to a minus (hence collapses the gridview row to reveal it's detailed row which is newly created by my CreateRow method below)

<asp:commandfield  buttontype="Image" selectimageurl="/images/plus.gif" showselectbutton="True" />

When the user clicks the control again, the image goes back to plus.  I am not sure how to:

a) capture if the user has expanded or collapsed since there is no ID attribute on a commandfield that I can grab onto to maybe do something like a check on the image to determine that
b) How to check if the CreateRow created a row before and undo it if it has...hence collapse

I am new to commandfield so bare with me.  Here's the function that actually creates the row, underneath the current row that the user has selected (selected mean by clicking on the commandfield image which changes form a plus to minus icon or vice versa when clicked)

    Private Sub CreateRow(ByVal table As Table, ByVal index As Integer)
        Dim row As GridViewRow = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
        Dim selectedrow As GridViewRow = SearchResults.Rows(index)
        Dim StfID As Integer = SearchResults.DataKeys(index).Value
        row.Cells.Add(CreateColumn(StfID, index))
        table.Rows.AddAt(index + 2, row)
    End Sub

so how do I do the opposite?  How can I check to even see if the CreateRow is even there.  It's a bit tricky and not sure how to attack this
Avatar of KarinLoos
KarinLoos

You might want to rethink your strategy. Personally in your example I would not have used a gridview but rather a datalist.  You can Define your own ItemTemplate which you could for example have defined as an  HTML table. whereby in the first row you defined your field including your "command" button only then just being an ordinary asp button.  and a second row for the detail stuff.  When binding the datalist you can then already fill the "detail" row, and just set its visible property to false. Then all your button has to do is set the visible property to the opposite value of the current vislble property.
Just a thought.
Avatar of dba123

ASKER

datalist?? what is that.  I am using GridView for other reasons, not just this.  I need paging, sorting, etc. so I choose GridView.
"datalist?? what is that "
well have a look at this link http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/default.aspx
you will see that there are a number of datacontrols provided on vs2005 asp.net of which one of them  is the datalist.
However as you say,  you are using the gridview for reasons like paging and sorting.
However the same concept as i outlined above can be applied. Instead of creating a new row each time, ensure that your template definition is within a templatecolum and define your own htmltable in this. (ie not using boundfields). Then onrowcommand  you meerely make a div or htmlrow visible / not visible.
eg
<asp;Gridview... >
<columns>
   <asp:TemplateField>
            <ItemTemplate>
                           DEFINE YOUR TEMPLATE HERE like for example 2 divs or a htmltable
            </ItemTemplate>
</colujmns>
Have a look at this link for further info on templatefield
http://msdn2.microsoft.com/en-us/library/aa479353.aspx
Avatar of dba123

ASKER

Problem is, if your html is in a templatecolum, it shows at the end, as a column of whatever row, not as a new line / new row.
ASKER CERTIFIED SOLUTION
Avatar of KarinLoos
KarinLoos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

I'm not using master/details in my gridview...
Avatar of dba123

ASKER

I had no idea you could add <tr> tags inside a template.  most examples don't show that.  Now I see.