ASP .NET webpage won't accept changes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a dropDownList with a selectedIndexChanged event which populates a
textBox. The dropDownList is being populated by a dataSet. Problem is when
you select an entry from the list, the whole form refreshes and it just goes
back to the top of the dataSet. Any ideas?
 
OK I resolved the original problem by setting If Not IsPostBack in the
page_Load event which is where the dataset is being loaded. Now however I
get an exception when I change the value of the dropDownList:

System.IndexOutOfRangeException: Index 0 is not non-negative and below total
rows count

This was returning the correct data when it was refreshing the dataSet, just
for the wrong user selected.
 
I apologize to so many changes to the original post but, here's what I've
found so far. Apparently, the dropDownList Index_change event is triggering
a refresh of the form. Now that the form isn't loading the dataset because
of the Not isPostBack, it's just emptying the dataSet so I'm doing an index
lookup on an empty set. Is this the way this is supposed to work? If not,
why am I losing my dataSet? I'm calling a DataBind within the Page_Load.
 
Because HTTP is a connectionelss protocol. Once the page is served up, the
Page object is destroyed. When the client makes another request (e.g. a
postback), a new Page object is created and the code is executed - but
because it's a brand new instance of an object, all of its local variables
are initialized to their defaults.

Don't make the mistake of thinking of the page as you would a windows form
where you have one instance of an object the whole time. Web programming is
completely different.
 
Roman,

I think that this simple sample that I just made can help you how to handle
your problem.

\\\
Private Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet
If Not IsPostBack Then
ds = New Dataset
Dim dt As New DataTable
dt.Columns.Add("mycolumn1")
dt.Columns.Add("mycolumn2")
dt.Rows.Add(dt.NewRow)
dt.Rows.Add(dt.NewRow)
dt.Rows(0)(0) = "Een"
dt.Rows(0)(1) = "Roman"
dt.Rows(1)(0) = "Twee"
dt.Rows(1)(1) = "Cor"
Session.Item("ds") = ds
ds.Tables.Add(dt)
DropDownList1.AutoPostBack = True
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataTextField = "mycolumn1"
DropDownList1.DataValueField = "mycolumn2"
DropDownList1.DataBind()
Else
ds = DirectCast(Session.Item("ds"), DataSet)
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal _
sender As System.Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
Me.TextBox1.Text = Me.DropDownList1.SelectedValue.ToString
End Sub
///

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top