Pregunta

I have an ASCX control that I load dynamically inside an aspx page. I pass a parameter using public property to my ASCX control and I'm able to catch it in the OnInit Event.

In the ASCX file I have an ASPxGridView control, in which I'm using an ObjectDataSource to get the data.

When the ASPxGridView control is binding the data, the property that has been set is now becomes NULL in value. Since the property is an ID that I need to access some SQL Tables, the data binding is failed.

I believe I've done things incorrectly, why does the public property becomes null?

What must I do to prevent it?

Thanks

¿Fue útil?

Solución

I think you are facing this problem because of ViewState. ViewState is only retrieve the data for the same page. If you navigate to some other page then that value becomes null on the page load event. So, instead of ViewState you can use SESSION STATE variable.

SESSION is also use to store the data but advantage in this is, you will get the data even after navigation of the page.

I hope you this will solve your problem.!

Otros consejos

    //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()

Looks like u got a parameter, bind data for a grid view and after page_load moment something like a postback occurs, and second time u don't have a specified parameter. Check if your DataGridView AutoPostBack property is false. Or if you need postbacks, save parameter (ID) to somewhere like a Session etc.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top