1

我正在尝试通过由两个文本框控件和一个链接控件组成的空数据模板控件将数据插入到 xml 文件中:

...该设计:

<emptydatatemplate>
 No Data Found. <br /> 
 <asp:TextBox ID="txtAltprodID" runat="server"></asp:TextBox><br />
 <asp:TextBox ID="txtAltprodNum" runat="server"></asp:TextBox><br />
 <asp:LinkButton ID="lnkpro" runat="server" OnClick="writeProductInfo" Text="Add Product Number"></asp:LinkButton>
</emptydatatemplate>

文本框控件提交的输入将放置在以下 XML 文件的产品表中:

<PersonalData>
  <products>
    <product>
      <productID>2</productID>
      <productNumber>123744</productNumber>
    </product>
    <product>
      <productID>7</productID>
      <productNumber>123456</productNumber>
    </product>
  </products>
  <customers>
    <customer>
      <LastName>Doe</LastName>
      <FirstName>Sam</FirstName>
      <Address>1234 Crescent RD</Address>
      <City>San Antonio</City>
      <State>Texas</State>
      <ZipCode>67894</ZipCode>
    </customer>
  </customers>
</PersonalData>

我为添加产品信息链接创建了以下事件处理程序:

Public Sub writeProductInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim altprodID As String = DirectCast(gvProductNumber.EmptyDataTemplate.FindControl("txtAltprodID"), TextBox).Text
        Dim altprodNum As String = DirectCast(gvProductNumber.EmptyDataTemplate.FindControl("txtAltprodNum"), TextBox).Text
        BindGridView()
        Dim oDs As DataSet = gvProductNumber.DataSource
        Dim oDr As DataRow = oDs.Tables(0).NewRow
        oDr("productID") = altprodID
        oDr("productNumber") = altprodNum
        oDs.Tables(0).Rows.Add(oDr)
        oDs.WriteXml(Request.PhysicalApplicationPath + "~/xml/PersonDataVI.xml")
        BindGridView()
    End Sub

...但是,我的语法存在问题:

Dim altprodID As String = DirectCast(gvProductNumber.EmptyDataTemplate.FindControl("txtAltprodID"), TextBox).Text
            Dim altprodNum As String = DirectCast(gvProductNumber.EmptyDataTemplate.FindControl("txtAltprodNum"), TextBox).Text

...我可以在如何设置上面的两行以从空数据模板中读取文本框控件的 ID 方面获得一些帮助吗?谢谢你

4

1 回答 1

0

以下是获取 ID 的方法:

 Public Sub writeProductInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'BindGridView()
        Dim productID As String = DirectCast(gvProductNumber.Controls(0).Controls(0).FindControl("txtAltprodID"), TextBox).Text
        Dim productNumber As String = DirectCast(gvProductNumber.Controls(0).Controls(0).FindControl("txtAltprodNum"), TextBox).Text
        Dim oDs As New DataSet()
        oDs.ReadXml(Server.MapPath("~/xml/PersonDataVI.xml"))
        oDs.Tables.Add("product")
        oDs.Tables("product").Columns.Add("productID")
        oDs.Tables("product").Columns.Add("productNumber")
        Dim oDr As DataRow = oDs.Tables("product").NewRow
        oDr("productID") = productID
        oDr("productNumber") = productNumber
        oDs.Tables("product").Rows.Add(oDr)

        oDs.WriteXml(Server.MapPath("~/xml/PersonDataVI.xml"))
        BindGridView()
    End Sub
于 2013-08-18T23:00:51.930 回答