Solved

How to increment an Integer variable across methods

Posted on 2006-07-19
4
765 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 350 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now