e.Row.RowState
I’m building a GridView that contains edit/update capabilities. In edit mode for certain rows I need to turn on a drop down list, and based on that row add/remove Items from the list, other rows I want to show a text input to edit, all based on Foreign Key for that row in the database (which I store in a DataKey to access). I handle the OnRowDataBound with a custom subroutine. This hasn?t been an issue in the past but this time I have multiple controls in the ItemTemplate and need to manipulate at databound time, which is a first.
<EditItemTemplate>
<asp:DropDownList ID=?ddlAnswer? runat=?server? Visible=?False?>
<asp:ListItem Value=?Yes? Text=?Yes? />
<asp:ListItem Value=?No? Text=?No? />
<asp:ListItem Value=?Neutral? Text=?Neutral? />
</asp:DropDownList>
<asp:TextBox ID=?txtAnswer? runat=?server? Text=?<%# Bind(?Answer?) %>? Visible=?False?></asp:TextBox>
</EditItemTemplate>
Both have visible=false so I need to get at the bound event and flip one on and change the item list if it?s the DropDownList.
RowState
I find the e.Row.RowState property and assign an if conditional to DataControlRowState.Edit.
If e.Row.RowState = DataControlRowState.Edit Then
?CHANGE UPDATE COMMAND ARGUMENT
CType(e.Row.FindControl(?lbUpdate?), LinkButton).CommandArgument = _
e.Row.RowIndex.ToString()
Dim iAnswerTypeID = CInt(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(0))
?TURN ON CORRECT TYPE OF INPUT
Select Case CInt(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(0))
Case 1
CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Visible = True
CType(e.Row.FindControl(?ddlAnswer?), DropDownList).SelectedValue = _
CStr(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(2))
Case 2
CType(e.Row.FindControl(?txtAnswer?), TextBox).Visible = True
Case 3
Case 4
CType(e.Row.FindControl(?txtAnswer?), TextBox).Visible = True
CType(e.Row.FindControl(?txtAnswer?), TextBox).TextMode = TextBoxMode.MultiLine
Case 5
CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Visible = True
Dim oldListItem As ListItem = CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Items.FindByValue(?Neutral?)
?REMOVE NEUTRAL
CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Items.Remove(oldListItem)
?ADD NA
CType(e.Row.FindControl(?ddlAnswer?), DropDownList).Items.Add _
(New ListItem(CStr(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(1)), CStr(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(1))))
CType(e.Row.FindControl(?ddlAnswer?), DropDownList).SelectedValue = _
CStr(gvEditSurveyResults.DataKeys(e.Row.RowIndex).Item(2))
End Select
End If
This doesn?t work, I never get into the if block for all rows. Setting a debug point I find the state of the row is not always DataControlRowState.Edit, but sometimes Alternate with an enumeration of 5. Looking thru Microsoft documentation I don?t see any 5 reference.
My Assumption
I am assuming that every property of the DataControlRowState handler has an Alternate row of that plus 1. So edit = 4 and edit on an alternative row is 5.
Related articles
- Add Web References to Visual Studio 2008 (February 27th, 2008)
- ASP:HyperLink & Mailto (August 15th, 2007)
- Stored Procedures vs Ad Hoc SQL (June 1st, 2007)
- Subdomain Cookies and Localhost (May 22nd, 2007)
- Ajax.Net Pro and Atlas Together (February 27th, 2007)
5 comments
Read the comments left by other users below, or:
Tim
#2.
July 17th, 2007, at 1:21 PM.
Yes,
The above code is within an IF e.Row.RowType == DataControlRowType.DataRow block.
Mynet sohbet
#3.
June 15th, 2011, at 10:08 PM.
thanks you nice blogs.
cinsel sohbet
#5.
June 15th, 2011, at 10:11 PM.
Thank you very much a matter of super great site. excellent.
Leave your comment...
If you want to leave your comment on this article, simply fill out the next form:
You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> .
#1. July 17th, 2007, at 9:12 AM.
Hi, I think it’s the IF, have you tried:
e.Row.RowType == DataControlRowType.DataRow