Solved

How to increment an Integer variable across methods

Posted on 2006-07-19
4
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

624 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