A
Arpan
Consider the following code which populates a DataGrid with the items
of a string array:
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If (ViewState("Colors") Is Nothing) Then
arrColors = New String(3) {"red", "blue", "pink", "white"}
ViewState("Colors") = arrColors
Else
arrColors = ViewState("Colors")
End If
'creating a DataTable with 2 columns - ID & Color,
'adding the DataTable to a DataSet, adding rows
'to the DataTable & finally populating the DataSet
dgColors.DataBind()
End Sub
Sub ChangeColor(ByVal obj As Object, ByVal ea As DataGridItemEventArgs)
Dim intIndex As Integer = ea.Item.ItemIndex
Response.Write("Index: " & intIndex & "<br>")
End Sub
<form runat="server">
<aspataGrid ID="dgColors" OnItemCreated="ChangeColor" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"><%# Container.DataItem("ID")
%></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Color" HeaderText="COLOR"/>
<asp:EditCommandColumn HeaderText="CHANGE" EditText="EDIT"
UpdateText="CHANGE" CancelText="CANCEL"/>
</Columns>
</aspataGrid>
</form>
Note the Response.Write line in the sub "ChangeColor". That line
produces the following output:
Index: -1
Index: 0
Index: 1
Index: 2
Index: 3
Index: -1
Now where from is the value "-1" coming, that too, not once but twice?
Thanks,
Arpan
of a string array:
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If (ViewState("Colors") Is Nothing) Then
arrColors = New String(3) {"red", "blue", "pink", "white"}
ViewState("Colors") = arrColors
Else
arrColors = ViewState("Colors")
End If
'creating a DataTable with 2 columns - ID & Color,
'adding the DataTable to a DataSet, adding rows
'to the DataTable & finally populating the DataSet
dgColors.DataBind()
End Sub
Sub ChangeColor(ByVal obj As Object, ByVal ea As DataGridItemEventArgs)
Dim intIndex As Integer = ea.Item.ItemIndex
Response.Write("Index: " & intIndex & "<br>")
End Sub
<form runat="server">
<aspataGrid ID="dgColors" OnItemCreated="ChangeColor" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"><%# Container.DataItem("ID")
%></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Color" HeaderText="COLOR"/>
<asp:EditCommandColumn HeaderText="CHANGE" EditText="EDIT"
UpdateText="CHANGE" CancelText="CANCEL"/>
</Columns>
</aspataGrid>
</form>
Note the Response.Write line in the sub "ChangeColor". That line
produces the following output:
Index: -1
Index: 0
Index: 1
Index: 2
Index: 3
Index: -1
Now where from is the value "-1" coming, that too, not once but twice?
Thanks,
Arpan