2016-11-16 83 views
0

我在位於InsertItemTemplate的FormView中的PlaceHolder控件中動態創建了表單。我有一個按鈕,FOrmView中的單擊窗體將會顯示在窗體的內部,我有一個用於插入的按鈕。如果我點擊插入按鈕,在代碼後面動態創建的表單部分將會消失。按動按鈕後動態創建的表單消失

這是代碼在ASP頁:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 

    <asp:Button ID="btnShowForm" runat="server" BackColor="#336699" Font-Names="Arial" Font-Size="9pt" ForeColor="White" Text="Show Form" Width="160px"/> 

    <br> 

    <asp:FormView ID="FV" runat="server" DefaultMode="Edit" Width="100%"> 

    <InsertItemTemplate> 

     <table style="width:60%; border: 1px solid black; border-collapse: collapse;"> 

     <asp:PlaceHolder ID="plControlUnos" runat="server" /> 

     <tr> 
     <td style="height:30px; text-align:center;" colspan="7"> 

     <asp:Button ID="save" runat="server" BackColor="#336699" Font-Names="Arial" Font-Size="9pt" ForeColor="White" Text="Insert" Width="140px" CommandName="insert"/> 
     <asp:Button ID="cancel" runat="server" BackColor="#336699" Font-Names="Arial" Font-Size="9pt" ForeColor="White" Text="Cancel" Width="140px" CommandName="cancel"/> 

     </td> 
     </tr> 
     </table> 

</InsertItemTemplate> 

</asp:FormView> 

</asp:Content> 

這是創建表單的其他部件的方法,並把佔位符:

Sub BindData() 

Dim strConnString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 
Dim con As New SqlConnection(strConnString) 
Dim cmd As New SqlCommand() 

cmd.CommandType = CommandType.Text 
cmd.CommandText = "SELECT NAZIV, NAZIV_POLJA FROM PR;" & _ 
"SELECT NAZIV, NAZIV_POLJA FROM PP" 

cmd.Connection = con 

Using sda As New SqlDataAdapter() 
    cmd.Connection = con 
    sda.SelectCommand = cmd 

    Using ds As New DataSet 

     Try 
      con.Open() 
      sda.Fill(ds) 
    Dim holder As PlaceHolder = CType(FV.FindControl("plControlUnos"), PlaceHolder) 

    For Each Row As DataRow In ds.Tables(0).Rows 


       holder.Controls.Add(New LiteralControl("<tr style='border: 1px solid black; height:30px;'>")) 
       holder.Controls.Add(New LiteralControl("<td style='padding-right:40px; width:400px; text-align:right; background-color:lightgray;border: 1px solid black;'><b>")) 

       holder.Controls.Add(New LiteralControl(Row(columnName:="Naziv"))) 

       holder.Controls.Add(New LiteralControl("</b></td>")) 
       holder.Controls.Add(New LiteralControl("<td style='width:300px; text-align:center; background-color:lightgray;border: 1px solid black;'>")) 

       Dim txtNOV As New TextBox() 
       txtNOV.ID = "txtNOV_" + Row(columnName:="NAZIV_POLJA") + "_KOMADA" 
       txtNOV.CssClass = "txtFormat" 
       holder.Controls.Add(txtNOV) 

       holder.Controls.Add(New LiteralControl("</td>")) 
       holder.Controls.Add(New LiteralControl("<td style='width:300px; text-align:center; background-color:lightgray;border: 1px solid black;'>")) 

       Dim txtNOV_SUM As New TextBox() 
       txtNOV_SUM.ID = "txtNOV_" + Row(columnName:="NAZIV_POLJA") + "_KOMADA_sum" 
       txtNOV_SUM.CssClass = "txtFormat" 
       txtNOV_SUM.Enabled = False 
       holder.Controls.Add(txtNOV_SUM) 

       holder.Controls.Add(New LiteralControl("</td>")) 
       holder.Controls.Add(New LiteralControl("</tr>")) 

    Next 

      holder.Controls.Add(New LiteralControl("<tr style='border: 1px solid black; height:30px;'>")) 
      holder.Controls.Add(New LiteralControl("<td colspan='2' style='text-align:center; background-color:lightgray;border: 1px solid black;'><b>UKUPNO NOVČANICE</b></td>")) 
      holder.Controls.Add(New LiteralControl("<td style='width:300px; text-align:center; background-color:lightgray;border: 1px solid black;'>")) 

      Dim txtNOV_KOMADA As New TextBox() 
      txtNOV_KOMADA.ID = "txtNOV_KOMADA" 
      txtNOV_KOMADA.CssClass = "txtFormat" 
      txtNOV_KOMADA.Enabled = False 
      holder.Controls.Add(txtNOV_KOMADA) 

      holder.Controls.Add(New LiteralControl("</td>")) 
      holder.Controls.Add(New LiteralControl("<td style='width:300px; text-align:center; background-color:lightgray;border: 1px solid black;'>")) 

      Dim txtNOV_KOMADA_sum As New TextBox() 
      txtNOV_KOMADA_sum.ID = "txtNOV_KOMADA_sum" 
      txtNOV_KOMADA_sum.CssClass = "txtFormat" 
      txtNOV_KOMADA_sum.Enabled = False 
      holder.Controls.Add(txtNOV_KOMADA_sum) 

      holder.Controls.Add(New LiteralControl("</td>")) 
      holder.Controls.Add(New LiteralControl("</tr>")) 

      holder.Controls.Add(New LiteralControl("<tr style='border: 1px solid black; height:25px;'>")) 
      holder.Controls.Add(New LiteralControl("<td rowspan='9' style='width:100px; text-align:center; background-color:lightgray;border: 1px solid black;' ><b>B)</b></td>" & _ 
                "<td colspan='3' style='padding-left:40px;width:100px; text-align:left; background-color:lightgray;border: 1px solid black;'><b>Kovani novac</b></td>" & _ 
                "</tr>")) 
     Catch ex As Exception 
      Response.Write("Error:" & ex.ToString) 
     Finally 
      cmd.Parameters.Clear() 
      con.Close() 
     End Try 

    End Using 
End Using 
End Sub 

這是在其點擊我的表格按鈕會可見和創建:

Protected Sub btnShowForm_Click(sender As Object, e As EventArgs) Handles btnShowForm.Click 

If IsPostBack Then 

    FV.Visible = True 
    FV.ChangeMode(FormViewMode.Insert) 
    BindData() 

End If 

End Sub 

這裏是按鈕(插入)我的窗體內點擊fo rm消失:

Protected Sub FV_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FV.ItemCommand 

If e.CommandName = "insert" Then 

Then 

End Sub 

我需要做些什麼來阻止我的表單在點擊按鈕後消失?

回答

0

動態控件需要(重新)在每個頁面加載時創建,並且包含PostBack。

等等場所BindData()Page_Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    BindData()  
End Sub 
+0

裏面我加了這一點,形式按鈕點擊後再次消失。但是如果這個通過,我的所有數據將會丟失,因爲表單將被重新創建。 – SeaSide

+0

如果不重新創建表單,數據將會丟失。 'btnShowForm'沒有綁定到它的'btnShowForm_Click'事件。這可能是爲什麼它仍然沒有工作。 – VDWWD