OnItemCreated?

Discussion in 'ASP .Net' started by Arpan, Aug 9, 2006.

  1. Arpan

    Arpan Guest

    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">
    <asp:DataGrid 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>
    </asp:DataGrid>
    </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
     
    Arpan, Aug 9, 2006
    #1
    1. Advertising

  2. Arpan,

    That should be for the header and footer rows of the data grid.

    Regards,
    Augustin

    "Arpan" wrote:

    > 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">
    > <asp:DataGrid 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>
    > </asp:DataGrid>
    > </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
    >
    >
     
    =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmE=?=, Aug 9, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmltIEhlYXZleQ==?=

    OnItemCreated

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jun 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    481
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Jun 23, 2004
  2. =?Utf-8?B?SmltIEhlYXZleQ==?=

    OnItemCreated

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jun 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Jun 23, 2004
  3. =?Utf-8?B?SmltIEhlYXZleQ==?=

    OnItemCreated Event

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jun 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    320
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Jun 23, 2004
  4. =?Utf-8?B?SmltIEhlYXZleQ==?=

    OnItemCreated Event

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jun 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    336
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Jun 23, 2004
  5. =?Utf-8?B?SmltIEhlYXZleQ==?=

    OnItemCreated Event

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jun 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    914
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Jun 23, 2004
Loading...

Share This Page