Issue: The SqlDataSource control does not have a naming container

W

wubin_98

Hi All,

Could any expert point out what is wrong in my code?
I search around but couldn't found answer. It seems that I am the first
person have this problem. Thanks in advanced

I have a gridview that one column contains a dropDownList and a
SqlDataSource control.
I try to set up dropDownList selection by following code.

------------------------------------------------
Protected Sub GridViewCapacity_RowCreated(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridViewCapacity.RowCreated
If (e.Row.RowState And DataControlRowState.Edit) <> 0 OrElse
(e.Row.RowState And DataControlRowState.Insert) <> 0 Then

Dim tmp As DropDownList =
CType(e.Row.FindControl("DropDownListLineSS"), DropDownList)
tmp.DataBind()

If Not
IsNothing(tmp.Items.FindByValue(DataBinder.Eval(e.Row.DataItem,
"DropDownListLineSS").ToString())) Then
tmp.SelectedValue = DataBinder.Eval(e.Row.DataItem,
"AssignedLineSS").ToString()
End If
End If
End Sub
------------------------------------------------

Following error message was displayed when execute line "
tmp.DataBind() "

The SqlDataSource control 'SqlDataSourceLineSS' does not have a naming
container. Ensure that the control is added to the page before calling
DataBind.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The SqlDataSource control
'SqlDataSourceLineSS' does not have a naming container. Ensure that
the control is added to the page before calling DataBind.

Source Error:


Line 50: 'DropDownListLineSS
Line 51: tmp =
CType(e.Row.FindControl("DropDownListLineSS"), DropDownList)
Line 52: tmp.DataBind()
Line 53:
Line 54: If Not
IsNothing(tmp.Items.FindByValue(DataBinder.Eval(e.Row.DataItem,
"DropDownListLineSS").ToString())) Then


Source File: C:\Work\WIP\EditCapacityData.aspx.vb Line: 52
 
D

Darren Kopp

Wouldn't the tmp = Ctype.... code copy the object data in
DropDownListSS into tmp? Do you need to call tmp.DataBind()?

I think what's happening is you are getting the datasource property on
tmp from dropdownlistss and that's causing the problem. tmp isn't on
the page, it's local, so when sqldatasourcess tries to bind to it, it's
not found.

But that is just a guess, i'm not positive if that's the problem.

HTH,
Darren Kopp
http://blog.secudocs.com/
 
W

wubin_98

Thanks for reply.
The reason I call DataBind is because I could not find query data in
DropDownListSS control.

I found that it seems too early to initialize the DropDownList control
in RowCreated function. In gridview PreRender function, the query data
in the DropDownListSS is load properly.

My question is: How can I initialize DropDownList in PreRender
function.
The hard part is following code doesn't work in gridview PreRender
function:

DataBinder.Eval(e.Row.DataItem, "AssignedLineSS").ToString()

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top