我有一个ASCX控件,可以在aspx页面中动态加载。我使用public属性将一个参数传递给我的ASCX控件,并能够在OnInit事件中捕获它。
在ASCX文件中,我有一个ASPxGridView控件,在该控件中,我使用一个ObjectDataSource获取数据。
当ASPxGridView控件绑定数据时,已设置的属性现在变为NULL值。由于该属性是一个ID,因此我需要访问一些SQL表,因此数据绑定失败。
我认为我做得不对,为什么public属性变成null?
我应该怎么做才能阻止它?
谢谢
发布于 2014-03-16 08:12:04
我认为你正面临这个问题,因为ViewState。ViewState只检索同一页的数据。如果导航到其他页面,则该值将在页面加载事件中变为空。因此,您可以使用ViewState变量代替SESSION STATE变量。
SESSION也用于存储数据,但优点是,即使在页面导航之后,也可以获得数据。
我希望你这能解决你的问题。
发布于 2014-03-13 07:39:27
//TO pass data in acxs control
Public Property DDLItemFilter() As String
Get
Return ViewState("P_DDLItemFilter")
End Get
Set(ByVal Value As String)
ViewState("P_DDLItemFilter") = Value
End Set
End Property
// to use this function in ascx control
Sub Bind_Grid()
Dim SearchVal As String = MakeQueryString(txt.Text)
lssql = " Select ApplNo,dbo.Fnc_LocName(CompCode,YearCode,TenantBranch) As TenantBranch from PropAllocAppl " & _
"Where COMPCODE='" & compcode & "' And YEARCODE='" & yearcode & "'"
lssql = lssql & " And ApplNo Like '" & SearchVal.Trim & "%' "
If ViewState("P_DDLItemFilter") <> "" Then
lssql = lssql & " And " & ViewState("P_DDLItemFilter") & " "
End If
lssql = lssql & " Order By ApplNo "
Dim dq As New DbQuery(lssql, True)
Dim dt As DataTable = dq.GetTable
DataGrid_ddl.DataSource = dt
DataGrid_ddl.DataBind()
End Sub
//pass data from aspx page and the id of the control is "UC_ApplCode1"
UC_ApplCode1.DDLItemFilter = " PropAllocAppl.AssetType not in ('B','V') "
//in same way get the data from ascx control
Public Function getDDLText() As String
Return (Trim(txt1.Text))
End Function
//you got the data in aspx page just calling this function with that .ascx id
string abc=UC_ApplCode1.getDDLText()发布于 2014-03-19 15:20:46
看起来,您获得了一个参数,为网格视图绑定了数据,在page_load之后发生了类似于回发的事情,而第二次没有指定的参数。检查您的DataGridView AutoPostBack属性是否为false。或者,如果需要回发,请将参数(ID)保存到类似会话之类的地方。
https://stackoverflow.com/questions/22316885
复制相似问题