Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to increment an Integer variable across methods

Posted on 2006-07-19
4
Medium Priority
?
779 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:tmccrank
4 Comments
 
LVL 5

Expert Comment

by:jjaqua
ID: 17142364
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.
0
 
LVL 24

Accepted Solution

by:
Justin_W earned 1400 total points
ID: 17142615
Instead of this:
                Dim i As Integer = CInt(dsAnswers.Tables(0).Rows(0).Item(2))
                dv.RowFilter = "QuestionsID =" & i  <-- THIS IS WHERE I NEED "i" TO INCREMENT
you should derive the QuestionID from the RepeaterItemEventArgs object. For example:
                Dim i As Integer = e.Item.ItemIndex
                dv.RowFilter = "QuestionsID =" & i

Otherwise, your code will break unless all of the questions have sequential, incrementing IDs (which may be the case now, but relying on such assumptions in your code isn't generally a good idea).
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 17143980
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
0
 

Author Comment

by:tmccrank
ID: 17147494
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question