Solved

How to increment an Integer variable across methods

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

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

Suggested Solutions

Title # Comments Views Activity
Query in AngularJS 8 84
Header Font Size in Grid View 6 51
Access-Control-Allow-Origin 4 46
Content of a read only textbox are not being saved in table in asp.net - VB 5 36
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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 …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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