Monday, March 26, 2012

populate detailview from code behind

i took it down to the basic level and still get an error -- Object reference not set to an instance of an object.

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

If DetailsView1.CurrentMode = DetailsViewMode.EditThen

CType(Me.DetailsView1.FindControl("textbox1"), TextBox).Text ="here"

EndIf

EndSub

<

asp:DetailsViewID="DetailsView1"

runat="server"

DefaultMode="Edit">

<Fields>

<asp:TemplateFieldHeaderText="Ticket Number">

<EditItemTemplate>

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

</EditItemTemplate>

</asp:TemplateField>

</Fields>

</asp:DetailsView>

for all data control when you want to findcontrol in the row you should define which row you need to find the control therefore for the code you gave is not valid. it is becaus you are finding the control within the detailview list such as footer, header and other and not for the control in the row.

so may be you try thisCType(Me.DetailsView1.Rows.Item(index of the field).FindControl("textbox1"), TextBox).Text ="here"

I am not sure the code I given to you work or not but i am sure that it is the problem that i mention at above.

its the only thing in the detialview

CType(Me.DetailsView1.Rows.Item(0).FindControl("textbox1"), TextBox).Text = "here"

error i get -- Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


oh yeah i have found the solution already

DetailsView1.Rows.Item(DetailsView1.PageIndex).FindControl(

"textbox1")

for get to tell you the item index need to be the pageindex. giving you the wrong example


same error

built another project -- this works fine in the page load -- when i move it to my user control in my main project it screws up

CType(Me.Details1.Rows.Item(0).FindControl("drop1"), DropDownList).SelectedValue = 5


tpiazza:

same error

built another project -- this works fine in the page load -- when i move it to my user control in my main project it screws up

CType(Me.Details1.Rows.Item(0).FindControl("drop1"), DropDownList).SelectedValue = 5

did you change the item index from 0 to details.pageindex?

CType(Me.Details1.Rows.Item(0).FindControl("drop1"), DropDownList).SelectedValue = 5

to

CType(Me.Details1.Rows.Item(Details1.PageIndex).FindControl("drop1"), DropDownList).SelectedValue = 5


Hello,

Look at these samples, especially "DetailsView Editing". They provide much codes and sample to follow.

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/detailsview.aspx

HTH

0 comments:

Post a Comment