开发者

ListView controls display wrong data the 3rd time

开发者 https://www.devze.com 2023-01-10 22:59 出处:网络
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.

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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号