我有一个页面,用户可以从中搜索项目等。我使用 .ascx 页面在 grdview 中显示结果。
aspx 页面只有一个 seacrh 框和一个按钮,单击按钮时我想将文本框内部的值传递到 ascx 页面,并让 gridview 填充结果。但是调试后发现点击按钮后gridview设置为null。所有其他值都在那里,结果从数据库中返回。
有人可以帮忙吗?过去几个小时我一直在寻找解决方案,并尝试了一些不同的事情,但仍然没有得到任何结果。
提前致谢
编辑
这是我的代码
这是aspx页面。
<%@ Register Src="Search.ascx" TagName="Search" TagPrefix="uc1" %>
<form id="form1" runat="server">
<div class="content">
<div style="padding:5px;">
<b>Search</b>: <asp:TextBox runat="server" ID="txtSearch" />
<asp:RequiredFieldValidator ErrorMessage="*" ValidationGroup="SearchGroup" ControlToValidate="txtSearch" ID="RequiredFieldValidator1"
runat="server" />
</div>
<div style="padding:5px;">
<asp:Button Text="Search" runat="server" ValidationGroup="SearchGroup" OnClick="btnSearch_Click" ID="btnSearch" />
</div>
</div>
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div class="Results">
<uc1:Search ID="UserControl1" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
在 ASCX 页面中,我有以下内容
背后的代码
暗淡测试 As Object = txtSearch.Text
Dim StockList As cls_Stock_Col = MyLOTSDB.LoadStock_Col()
Try
Dim dt As DataTable = New DataTable()
Dim dcol As New DataColumn("TradeName", GetType(System.String))
dt.Columns.Add(dcol)
'Create an ID column for adding to the Datatable
dcol = New DataColumn("PackSize", GetType(System.String))
dt.Columns.Add(dcol)
dcol = New DataColumn("PLU", GetType(System.String))
dt.Columns.Add(dcol)
dcol = New DataColumn("SOH", GetType(System.String))
dt.Columns.Add(dcol)
dcol = New DataColumn("RetailAsCurrency", GetType(System.String))
dt.Columns.Add(dcol)
For Each col As DataColumn In dt.Columns
'Declare the bound field and allocate memory for the bound field.
Dim bfield As New BoundField()
'Initalize the DataField value.
bfield.DataField = col.ColumnName
'Initialize the HeaderText field value.
Dim DisplayName As String = ""
Dim DataFormat As String = ""
Dim FieldType As Type = col.GetType()
Dim StringLength As Int32
GetFieldDetails(TheType, col.ColumnName, DisplayName, DataFormat, FieldType, StringLength)
bfield.HeaderText = DisplayName
If (DataFormat = "$0.00") Then
bfield.DataFormatString = "{0:C2}"
End If
bfield.SortExpression = "col.ColumnName"
'Add the newly created bound field to the GridView.
GridView1.Columns.Add(bfield)
Next
GridView1.DataSource = StockList
GridView1.DataBind()
Catch ex As Exception
End Try
当我尝试将列添加到gridview时,我可以调试并抛出错误,
再次感谢