views:

27

answers:

2

I have a ListView that populates and displays a user's data (via a profile system in VWD 2008) during Page_Load and also when I go from the EditItemTemplate to the ItemTemplate.

When I go to the edit screen again, my DropDownList and RadioButtonList controls display the first items in the corresponding tables instead of the correct profile values.

I don't understand why the controls populate correctly the first and second times but not on the third time (that's right, the third time is NOT a charm).

Can someone can help me understand how to solve this problem?

ItemTemplate:

<asp:DropDownList ID="ddlTState" AppendDataBoundItems="True" 
DataSourceID="srcState" DataTextField="StateName" DataValueField="StateName" 
Enabled="False" TabIndex="125"  runat="server" />

EditItemTemplate:

<asp:DropDownList ID="ddlEState" AppendDataBoundItems="true" 
                  DataSourceID="srcState" DataTextField="StateName" 
                  DataValueField="StateName" TabIndex="125" runat="server">    
<asp:ListItem Text="--State--" Value="" />

VB code behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load  
    lsv = Util.FindChild(Me, "lsvProfile")  

    'If Not IsPostBack Then  
    ddl = Util.FindChild(lsv, "ddlEState") 'Util.FindChild = my version of FindControl  
    If ddl IsNot Nothing Then ddl.SelectedValue = Profile.State  

    ddl = Util.FindChild(lsv, "ddlTState")  

    If ddl IsNot Nothing Then ddl.SelectedValue = Profile.State  
    'End If  
End Sub
A: 

Hey,

It could be it's hitting the null condition and not writing; a better way to handle items/edit template is to tap into the ItemDataBound event or ItemCreated event handlers. This features at the time it creates the row, and you will have better success there. I believe you could also leverage ItemEditing, which fires when a row switches to edit mode, as another way to process an edit action.

EDIT: Try to do this:

protected void lsvProfile_ItemDataBound(..)
{
    if (e.Item.ItemTYpe == ListViewItemType.DataItem)
    {
        DropDownList d = e.Item.FindControl("ddlTState") as DropDownList;
    }

}

And debug to see if that is null or not null. The only time that should be null is if its rendering the header/footer or the edit item... That should work though, I do that all the time.

HTH.

Brian
Thanks, Brian... even if you don't spell your name right. (;>) Unfortunately, none of the events you suggested even "noticed" the controls (except for the ListView control, "lsvProfile"). They are, however, picked up in the first go-round of Page_Load. I'm inclined to think that the problem has to do with the ListView's EditIndex, but I'm not sure how or where I'd go about manipulating the EditIndex to find that out.
Bryan
These list controls (DDLs and RBLs) are becoming a big pain. First I'm told that a bug in .Net 3.5 doesn't let me bind them in the declarative code unless I want errors; and now when I try to follow the workarounds and populate them from code-behind, I can't do it. Have these bugs been repaired in 4.0?
Bryan
The bug you mention I think is actually an issue with binding because it will apply the selected value before it applies the data source; I've gotten errors with that before. To test, In RowDataBound, try the code above; I'll edit the post. Also, make sure you are using the item to find items by, you can't use the listview reference directly.
Brian
A: 

Thanks, Brian. I resolved it with this code before I read your latest post:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    lsv = Util.FindChild(Page, "lsvProfile")
    If Not IsPostBack Then
        Try
            lsv.EditIndex = 0
            rbl = Util.FindChild(lsv, "rblEGender")
            If rbl IsNot Nothing Then rbl.SelectedValue = Profile.Gender
            ddl = Util.FindChild(lsv, "ddlEState")
            If ddl IsNot Nothing Then ddl.SelectedValue = Profile.State
            ddl = Util.FindChild(lsv, "ddlEBirthDay")
            If ddl IsNot Nothing Then ddl.SelectedValue = Profile.BirthDay
            ddl = Util.FindChild(lsv, "ddlEBirthMo")
            If ddl IsNot Nothing Then ddl.SelectedValue = Profile.BirthMo
        Catch ex As Exception
        End Try
    End If
End Sub

Protected Sub lsvProfile_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lsvProfile.ItemDataBound
    ddl = Util.FindChild(Me, "ddlEState")
    If ddl IsNot Nothing Then ddl.SelectedValue = Profile.State
    ddl = Util.FindChild(Me, "ddlEBirthMo")
    If ddl IsNot Nothing Then ddl.SelectedValue = Profile.BirthMo
    ddl = Util.FindChild(Me, "ddlEBirthDay")
    If ddl IsNot Nothing Then ddl.SelectedValue = Profile.BirthDay
    rbl = Util.FindChild(Me, "rblEGender")
    If rbl IsNot Nothing Then rbl.SelectedValue = Profile.Gender

End Sub
Bryan