Can't get value from datagrid when generating columns on the server side

A

Alex Shirley

Hi

I have a problem with a datagrid, I need to get a value from one of
the columns from when I hit a hyperlink column. The hyperlink column
is the first column, and the second column is the value (which is in
fact "ContactID").

The event that fires when I click on the hyperlink column definitely
works.
When I add in the ContactID column in the html like this (in the
datagrid)

<asp:BoundColumn DataField="Contact ContactID" SortExpression="Contact
ContactID" HeaderText="Contact ContactID"></asp:BoundColumn>

And click on the hyperlink button, then no problem, the value is
returned when I put this code in the button event handler:

-------------------------------------------------------------------------------

Response.Write("ContactID=" & e.Item.Cells(1).Text)
Response.End

-------------------------------------------------------------------------------

However if I create the bound column using the following back end code
(which will show the column):

-------------------------------------------------------------------------------

showdgcolumn(DataGridWhatever, "ContactID", "Contact ContactID")

.......>

Public Sub showdgcolumn(ByVal mydatagrid As DataGrid, ByVal
strHeadertext As String, ByVal strdatafield As String)
Dim col As BoundColumn = New BoundColumn
col.HeaderText = strHeadertext
col.DataField = strdatafield
col.SortExpression = strHeadertext
mydatagrid.Columns.Add(col)
End Sub

-------------------------------------------------------------------------------

Now in this case if I click on the view hyperlink button I get
"Specified argument was out of the range of valid values. Parameter
name: index", using the same event handler code above.

Note the column is in exactly the same position. I just don't seem to
get the index value if I generate the column using back end VB code.

Any ideas?

Many thanks!

Alex
 
A

Alex Shirley

OK I see my mistake now. I was generating the dynamic columns in
"page_load" when I should have been generating them in "page_init". So
I have one line that will call a function that will generates the
columns...

However!

Visual Studio 2003 warns in the Page.Init Section:
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.

And I have placed code in this area before and it has disappeared
occasionally. How do I place code in Page.Init without it being at
risk?

Many Thanks

Alex
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top