我在PlaceHolder控件中动态创建了表单,该控件位于InsertItemTemplate中的FormView中。我有一个按钮,在FOrmView中可以看到单击表单,在表单中我有一个Insert按钮。如果单击“插入”按钮,则在代码后面动态创建的表单部分将消失。
这是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>
这是创建表单的其他部分并放入PlaceHolder的方法:
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
这是我的表单中的单击形式消失的按钮(插入):
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 :(得分:0)
需要在每次页面加载时(重新)创建动态控件,其中包括PostBack。
所以将BindData()
放在Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
BindData()
End Sub