Results 1 to 14 of 14

Thread: Formview's dropdownlist is not being populated after ItemInserted

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Formview's dropdownlist is not being populated after ItemInserted

    Hello,
    I've got formview that is always in insert mode: dropdownlist populated from some other db table and a textbox.
    When user clicks insert:
    - the selected value from the ddlist is ignored, and the textbox value is written in db
    - the ddlist is not populated again, although I placed the sub for populating that ddlist in page_load (regardless of ispostback or not) and after the insert.

    See for yourself (be my guest and enter whatever data you wish):

    http://www.movie-remakes.com/kp/epiz...SIMPLE_EN.aspx

    And here is the code (1st part, codebehind):

    Code:
    <%@ Page Language="VB" ValidateRequest="false" Debug="true" SmartNavigation="true"  %>
    
    <%@ Register src="../inc_main.ascx" tagname="ucMain" tagprefix="uc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        Dim myID As Integer
        Dim rs As System.Data.IDataReader
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
           
            ' myID IS THE VALUE OF THE DB MASTER TABLE THAT IS INSERTED INTO NEW RECORDS AS FIELD ID_epiz 
            
            myID = 1
           
            'POPULATING GRIDVIEW
            
            SetGridview(DSautori, gvAutori, "SELECT veza_epiz_autor.ID AS veza_epiz_autor__ID, veza_epiz_autor.ID_autor AS veza_epiz_autor__ID_autor, veza_epiz_autor.funkcija AS veza_epiz_autor__funkcija, autori.ime AS autori__ime, autori.prezime AS autori__prezime FROM veza_epiz_autor LEFT JOIN autori ON veza_epiz_autor.ID_autor=autori.ID WHERE veza_epiz_autor.ID_epiz=" & myID & " ORDER BY veza_epiz_autor.ID")
    
            ' POPULATING FORMVIEW'S DROPDOWNLIST ddlID_autor
            
            populate_fvAutori_ddlID_autor()
    
        End Sub
    
    
        Sub populate_fvAutori_ddlID_autor()
            Inc.cnOpen()
            rs = Inc.cnExec("SELECT ID, ime, prezime FROM autori ORDER BY prezime, ime")
            Dim tmpddl1 As DropDownList = CType(FindControl("fvAutori").FindControl("ddlID_autor"), DropDownList)
            tmpddl1.Items.Clear()
            tmpddl1.Items.Add(New ListItem("-undefined-", "-1"))
            While rs.Read()
                tmpddl1.Items.Add(New ListItem(rs("ime").ToString & " " & rs("prezime").ToString, rs("ID").ToString))
            End While
            Inc.cnClose()
        End Sub
    
        
        Sub SetGridview(ByVal DSwhich As SqlDataSource, ByVal GVwhich As GridView, ByVal strSC As String)
            DSwhich.ConnectionString = Inc.getCnString()
            DSwhich.ProviderName = Inc.getProviderName()
            DSwhich.SelectCommand = strSC
            GVwhich.DataBind()
        End Sub
        
        
    
        Protected Sub gvAutori_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    
            If e.Row.RowType = DataControlRowType.DataRow Then
                            
                If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
                    ' ROW IS IN ITEM/ALTERNATINGITEM MODE
                    
                    ' POPULATING LABELS lblautor1, lblfunkcija
                    
                    CType(e.Row.FindControl("lblautor1"), Label).Text = DataBinder.Eval(e.Row.DataItem, "autori__ime").ToString & " " & DataBinder.Eval(e.Row.DataItem, "autori__prezime").ToString
                    CType(e.Row.FindControl("lblfunkcija"), Label).Text = DataBinder.Eval(e.Row.DataItem, "veza_epiz_autor__funkcija").ToString
    
                ElseIf e.Row.RowState = DataControlRowState.Edit Or e.Row.RowState.ToString = "Alternate, Edit" Then
                    ' ROW IS IN EDIT MODE
                    
                    ' POPULATING DDLIST ddlID_autor
                    
                    Inc.cnOpen()
                    rs = Inc.cnExec("SELECT ID, ime, prezime FROM autori ORDER BY prezime, ime")
                    Dim tmpddl1 As DropDownList = CType(e.Row.FindControl("ddlID_autor"), DropDownList)
                    tmpddl1.Items.Add(New ListItem("-undefined-", "-1"))
                    While rs.Read()
                        tmpddl1.Items.Add(New ListItem(rs("ime").ToString & " " & rs("prezime").ToString, rs("ID").ToString))
                    End While
                    tmpddl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "veza_epiz_autor__ID_autor").ToString
                    Inc.cnClose()
    
                    ' POPULATING TEXTBOX txtfunkcija
                    
                    CType(e.Row.FindControl("txtfunkcija"), TextBox).Text = DataBinder.Eval(e.Row.DataItem, "veza_epiz_autor__funkcija").ToString
    
                End If
                
            End If
        End Sub
    
    
        Protected Sub gvAutori_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
            
            ' UPDATING RECORD IN THE DATABASE TABLE veza_epiz_autor
            
            Dim ID_autor As Integer = CType(gvAutori.Rows(e.RowIndex).FindControl("ddlID_autor"), DropDownList).SelectedValue
            Dim funkcija As String = CType(gvAutori.Rows(e.RowIndex).FindControl("txtfunkcija"), TextBox).Text.Trim
            DSautori.UpdateCommand = "UPDATE veza_epiz_autor SET ID_autor = " & ID_autor & ", funkcija = '" & funkcija & "' WHERE ID = " & e.Keys(0)
        End Sub
    
       
        Protected Sub gvAutori_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
            
            ' DELETING RECORD IN THE DATABASE TABLE veza_epiz_autor
            
            DSautori.DeleteCommand = "DELETE FROM veza_epiz_autor WHERE ID=" & e.Keys(0)
        End Sub
    
    
    
        Protected Sub InsertAutori_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            
            ' INSERT NEW RECORD IN THE DATABASE TABLE veza_epiz_autor
            
            Dim ID_epiz As Integer = myID
            Dim ID_autor As Integer = CType(FindControl("fvAutori").FindControl("ddlID_autor"), DropDownList).SelectedValue
            Dim funkcija As String = CType(FindControl("fvAutori").FindControl("txtfunkcija"), TextBox).Text.Trim.ToString
            DSautori.InsertCommand = "INSERT INTO veza_epiz_autor (ID_epiz, ID_autor, funkcija) VALUES (" & ID_epiz & ", " & ID_autor & ", '" & Inc.nav(funkcija) & "')"
            
            ' POPULATING FORMVIEW'S DROPDOWNLIST ddlID_autor AGAIN
            
            populate_fvAutori_ddlID_autor()
    
        End Sub
    
        Protected Sub fvAutori_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
    
            ' THIS IS WHERE I ALSO TRIED PLACING ALL THE CODE FROM THE InsertAutori_Click METHOD
            
        End Sub
    
    
        Protected Sub cbOnOffAutori_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            
            ' SHOW/HIDE THE PANEL plAutori (FORMVIEW + GRIDVIEW)
            
            If cbOnOffAutori.Checked Then
                populate_fvAutori_ddlID_autor()
                plAutori.Visible = True
            Else
                plAutori.Visible = False
            End If
             
        End Sub
    
    </script>

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    ...and the 2nd part:

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta name="ROBOTS" content="NOINDEX,NOFOLLOW" />
        <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
        <meta http-equiv="PRAGMA" content="NO-CACHE" />
        <title></title>
        <link href="unos.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" language="javascript">
        function ConfDelete(xx) {
            if (confirm(xx)) {return true;} else {return false;}
        }
        </script>   
    </head>
    <body id="body1" runat="server">
    <form id="f1" runat="server">
        <uc1:ucMain ID="Inc" runat="server" EnableViewState="false" />
        <asp:SqlDataSource ID="DSautori" runat="server" EnableViewState="false" />
    
    
                        <asp:CheckBox ID="cbOnOffAutori" runat="server" Text="Authors:" BackColor="#CCFF66" Font-Bold="True" Font-Size="Medium" Font-Underline="False" AutoPostBack="True" oncheckedchanged="cbOnOffAutori_CheckedChanged"  />
    
    <asp:PlaceHolder ID="plAutori" runat="server" Visible="false"><br />            
    
                        <asp:FormView ID="fvAutori" runat="server" 
                        DataKeyNames="ID" DataSourceID="DSAutori" DefaultMode="Insert" 
                        CellPadding="0" CellSpacing="0" GridLines="None" BorderStyle="None" BorderWidth="0" Width="1" 
                        OnItemInserting="fvAutori_ItemInserting" 
                        >
                        <InsertItemTemplate>
                            <table border="0" cellspacing="1" cellpadding="3">
                            <tr>
                            <td valign="top" class="headerstyle" align="center" width="200">
                            <b>Name (integer):</b>
                            </td>
                            <td valign="top" class="headerstyle" align="center" width="200">
                            <b>Function (string):</b>
                            </td>
                            <td valign="top" class="headerstyle">
                            </td><td valign="top" class="headerstyle">
                            </td></tr>
                        
                            <tr>
                            <td valign="top" class="itemstyle">
                            <asp:DropDownList ID="ddlID_autor" runat="server"></asp:DropDownList>
                            <br />
                            </td>
                            <td valign="top" class="itemstyle">
                            <asp:TextBox ID="txtfunkcija" runat="server" CssClass="input" EnableViewState="false" />
                            </td>
                            
                            <td valign="top" class="itemstyle">
                            <asp:Button ID="InsertAutori" runat="server" CausesValidation="True" CommandName="Insert" Text=" OK " CssClass="mediumbutton" EnableViewState="false" OnClick="InsertAutori_Click" />
                            <br />
                            
                            </td><td valign="top" class="itemstyle">
                            <asp:Button ID="CancelAutori" runat="server" CausesValidation="False" Text=" Cancel " CssClass="mediumbutton" EnableViewState="false" />
                           
                            </td>
                            </tr>
                            </table>
                        </InsertItemTemplate>
                        </asp:FormView> 
     
                                
                        <asp:GridView ID="gvAutori" runat="server"
                        DataKeyNames="veza_epiz_autor__ID" DataSourceID="DSautori"
                        EnableViewState="false" AutoGenerateColumns="false"
                        EmptyDataText="No records found."
                        AllowPaging="false" AllowSorting="false"
                        CellPadding="3" CellSpacing="1" BorderStyle="None" BorderWidth="0" GridLines="None"
                        OnRowDataBound="gvAutori_RowDataBound" 
                        OnRowDeleting="gvAutori_RowDeleting"
                        OnRowUpdating="gvAutori_RowUpdating">
                        <Columns>
                        
                        <asp:TemplateField HeaderText="name">
                        <HeaderStyle CssClass="headerstyle" />
                        <ItemStyle CssClass="itemstyle" />
                        <ItemTemplate>
                            <asp:Label ID="lblautor1" runat="server" EnableViewState="false" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlID_autor" runat="server" EnableViewState="false"></asp:DropDownList>
                        </EditItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="function">
                        <HeaderStyle CssClass="headerstyle" />
                        <ItemStyle CssClass="itemstyle" />
                        <ItemTemplate>
                            <asp:Label ID="lblfunkcija" runat="server" EnableViewState="false" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtfunkcija" runat="server" CssClass="input" EnableViewState="false" />
                        </EditItemTemplate>
                        </asp:TemplateField>
    
                        <asp:CommandField ButtonType="Button" ShowEditButton="True" EditText=" Edit " UpdateText=" OK ">
                        <ControlStyle CssClass="mediumbutton" />
                        <HeaderStyle CssClass="headerstyle" Width="11" />
                        <ItemStyle CssClass="itemstyle" Width="11" />
                        </asp:CommandField>
    
    
                        <asp:TemplateField>
                        <HeaderStyle CssClass="headerstyle" Width="10" />
                        <ItemStyle CssClass="itemstyle" />
                        <ItemTemplate>
                            <asp:Button ID="DeleteButton" runat="server" Text=" Delete " CommandName="Delete" EnableViewState="false" Font-Bold="True" ForeColor="Red" CssClass="mediumbutton" />
                        </ItemTemplate>
                        </asp:TemplateField>
    
                        
                        </Columns>
                        </asp:GridView>
    
    </asp:PlaceHolder>                    
        
    </form>
    </body>
    </html>

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Formview's dropdownlist is not being populated after ItemInserted

    I suggest that you place a breakpoint in your page load and DDL and button click events to have a look at the actual values being passed to and received from the database. When debugging, you can move your mouse over variables and object properties to see the values they hold.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by mendhak
    I suggest that you place a breakpoint in your page load and DDL and button click events to have a look at the actual values being passed to and received from the database. When debugging, you can move your mouse over variables and object properties to see the values they hold.
    I just did - I haven't found out anything new that I already didn't know... It didn't help

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Formview's dropdownlist is not being populated after ItemInserted

    So when you did step into the populate_fvAutori_ddlID_autor method, did it retrieve data from the database? Are you getting values when you do a quickwatch on rs("ime").ToString?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by mendhak
    So when you did step into the populate_fvAutori_ddlID_autor method, did it retrieve data from the database? Are you getting values when you do a quickwatch on rs("ime").ToString?
    I dunno... When I mouseover tmpddl1 it says its value is Nothing; mouseover any other part of the method doesn't bring anything... And the Locals window shows only "Me" and "tmpddl1" which is Nothing.
    (I state once before I don't know how to use debugging )
    But populate_fvAutori_ddlID_autor method does populate dropdownlist ddlID_autor but only the first time; after insertion it does not populate it... (see the live demo http://www.movie-remakes.com/kp/epiz...SIMPLE_EN.aspx )

    And the complete solution should look like this:
    http://www.movie-remakes.com/kp/epiz...it_EN.asp?ID=1
    (with population of ddlists, enter-key-submission of the proper part of the form etc.)

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Actually, post #4 looks like you are saying you did do debugging. Is the DDL being added dynamically?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by mendhak
    Actually, post #4 looks like you are saying you did do debugging. Is the DDL being added dynamically?
    I did debugging, but haven't found out anything new.

    DDL is generated in design time, but its populated dynamically: see the method populate_fvAutori_ddlID_autor()

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Formview's dropdownlist is not being populated after ItemInserted

    My only guess at this point would be that the database isn't returning values.

    There's also something else you can do... look at this line:

    Dim tmpddl1 As DropDownList = CType(FindControl("fvAutori").FindControl("ddlID_autor"), DropDownList)

    fvAutori is a control on the page, inside a placeholder. Do fvAutori.FindControl() or do a placeholderid.findcontrol("fvAutori").FindControl().

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by mendhak
    My only guess at this point would be that the database isn't returning values.

    There's also something else you can do... look at this line:

    Dim tmpddl1 As DropDownList = CType(FindControl("fvAutori").FindControl("ddlID_autor"), DropDownList)

    fvAutori is a control on the page, inside a placeholder. Do fvAutori.FindControl() or do a placeholderid.findcontrol("fvAutori").FindControl().
    Thanks for your reply, mendhak. However, it is not the issue:

    1) database not returning values
    - it returns values for the first time, and not after the postback (iteminserting)
    - it returns values for the similar DDL in gridview; the only difference in code is that it sets selectedvalue for the gridview DDL. (see the comment "' POPULATING DDLIST ddlID_autor" in gvAutori_RowDataBound; compared to the method populate_fvAutori_ddlID_autor)

    2) referencing plAutori.FindControl("fvAutori").FindControl...
    I tried that before, no success. In fact, I learned that you don't have to reference placeholder at all; Gridview gvAutori is in the same placeholder and I don't have to reference it in a way "plAutori.FindControl("gvAutori")", it works without it.

    Take a look at this, this is even more simplified - just a formview with the DDL that should be populated over and over again, after each submission (insertion); I wrote the code in the page content:

    http://www.movie-remakes.com/kp/_TES...ATASOURCE.aspx

    but I managed to solve it only after I removed the datasource out of the picture:

    http://www.movie-remakes.com/kp/_TEST_EN.aspx

    But I wouldn't call this "resolved" because I don't understand what's wrong with the 1st solution that includes sqldatasource.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Formview's dropdownlist is not being populated after ItemInserted

    A little tough to be honest. I can't see why the ddl won't repopulate on postback, I see no postback checks and no dynamically added controls that would possibly cause this to happen.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by mendhak
    A little tough to be honest. I can't see why the ddl won't repopulate on postback, I see no postback checks and no dynamically added controls that would possibly cause this to happen.
    I never thought that I will stumble upon a problem that you can't solve
    But thanks anyway
    (Imagine the puzzled look on my newbie's face when I try to find the reason why this is not happening; so I had to go the other way around)

    P.S. You might have noticed that I took your advice and never use <%# ... %>
    Last edited by imbrod; Sep 26th, 2008 at 07:21 AM.

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by imbrod
    P.S. You might have noticed that I took your advice and never use <%# ... %>
    And I'll bet you're kicking yourself now

    As for your problem, if you really want to know why it wasn't working that one particular way, (whenever I run into such a situation) I start the same form from scratch, rewriting it all but with the same technique to see where I went wrong.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    Re: Formview's dropdownlist is not being populated after ItemInserted

    Quote Originally Posted by mendhak
    And I'll bet you're kicking yourself now

    As for your problem, if you really want to know why it wasn't working that one particular way, (whenever I run into such a situation) I start the same form from scratch, rewriting it all but with the same technique to see where I went wrong.

    Believe me, mendhak, I wrote it 4-5 times from the scratch... I do the same as you do: start all over again.
    But that's how I found the alternate solution without sqldatasource.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width