Link to home
Start Free TrialLog in
Avatar of tmccrank
tmccrank

asked on

How to increment an Integer variable across methods

Hi,

How can I increment "i" with every iteration of the ItemDataBound event?


    Private Sub BindData()
        'retrieve DataSet
        Dim dsQuestions As DataSet = Me.propQuestionsData

        'bind the questions Repeater to dsQuestions DataSet
        rptQuestions.DataSource = dsQuestions
        rptQuestions.DataBind()
    End Sub

    Public Sub rptQuestions_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptQuestions.ItemDataBound

        'retrieve DataSet from Session variable
        Dim dsAnswers As DataSet = CType(Session("dsAnswers"), DataSet)

        Dim lt As ListItemType = e.Item.ItemType
        If lt = ListItemType.Item Or lt = ListItemType.AlternatingItem Then

            'find the RadiobuttonList control
            Dim rblAnswers As RadioButtonList = CType(e.Item.FindControl("rblAnswers"), RadioButtonList)
            'if the RBL control exists, then
            If Not rblAnswers Is Nothing Then

                Dim dv As New DataView(dsAnswers.Tables(0))
                Dim i As Integer = CInt(dsAnswers.Tables(0).Rows(0).Item(2))

                dv.RowFilter = "QuestionsID =" & i  <-- THIS IS WHERE I NEED "i" TO INCREMENT

                rblAnswers.DataSource = dv
                rblAnswers.DataTextField = "Answers"
                rblAnswers.DataValueField = "AnswersID"
                rblAnswers.DataBind()

            End If
        End If
    End Sub


Thanks,
Jens
Avatar of jjaqua
jjaqua
Flag of United States of America image

You could set up a hidden label with ViewState enabled. Add to the existing value of the label and reassign the new value when you are done incrementing.
ASKER CERTIFIED SOLUTION
Avatar of Justin_W
Justin_W

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GavinMannion
GavinMannion

Or you should be able to declare i outside of the method in the class delcaration and then just increment it.

public class MyPage : System.Web.UI.Page
{
    int i;

ItemDataboundmethod()
{
    i++;
}
}

It's C# but hopefully you get the idea
Avatar of tmccrank

ASKER

Justin,

Thanks, that's the ticket.  You're right about the sequential, incrementing IDs in SQL Server, this takes care of that without having to worry about missing IDs.

Jens