elbillaf
elbillaf

Reputation: 1984

Selectedvalue method returns null string

Note, I have found the solution to my problem. Please see below.

I'm converting a system of numerous programs from foxpro/foxweb/javascript to asp.net/vb/sql server.

I'm coding for a dropdownlist which is currently controlled via js.

I don't like using the control this particular way, but I want to maintain look and feel for the customer.

The control is initialized with some values via Page_Load() in behind code. This works.

In selectedIndexChanged() I execute

dim s as string
s = ddl.SelectedValue
textbox1.text = s  ' this line just for debugging.

(I then go on to clear the ddl and repopulate it with different items based on the value of s - I don't like this functionality, but I want to give things the same look and feel as the old system.) important - I have gotten the value of s before I change anything with the ddl. (and in fact, for debugging purposes, I've commented out the rest of the dang code.)

The problem is that the selectedvalue always returns a null string instead of the value I set with New Listitem("x", "value-of-x"). Is there some special trick to getting values when I'm using an asynchpostbacktrigger?


thanks for your answers. I found out what my problem was - and it was not what I thought. I didn't realize that the autoupdate would implement a page_load() command. (This seems odd to me, because for some reason I thought the reason for using a panel update was so it didn't have to reload the entire page. I guess page_load() fires whenever any part of a page loads? Not complaining - just wondering aloud.)

Anyway, I found in another post a reference to a function called ispostback(). In my page_load code, I say,

If not ispostback() then initialize

It works now.

I saw it referenced here earlier: DropDownList doesn't postback on SelectedIndexChanged

But at that time I didn't see the relevance to what I was doing - and no reference to page_upload(). Anyway, I'm not sure how to close this out, but I figure I should give my solution which follows below. The values are kinda stupid, as I'm trying to avoid posting my production code.

In front code, after the form tag, I put:

Then later in the front code, I put the object:

In behind code, I have several pieces:

Protected Sub initialize()

    ddlAssign.Items.Clear()
    ddlAssign.Items.Add(New ListItem("SELECT A CATEGORY", ""))
    ddlAssign.Items.Add(New ListItem("option 1", "combo1"))
    ddlAssign.Items.Add(New ListItem("option 2", "combo2"))
    ddlAssign.Items.Add(New ListItem("option 3", "combo3"))
    ddlAssign.SelectedIndex = 0

End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
  initialize()
End If

End Sub

Protected Sub set_next_menu()

    ddlAssign.Items.Clear()
    ddlAssign.Items.Add(New ListItem("menu 2", "2"))
    ddlAssign.Items.Add(New ListItem("menu 3", "3"))
    ddlAssign.Items.Add(New ListItem("menu 4", "4"))
    ddlAssign.Items.Add(New ListItem("menu 5", "5"))
    ddlAssign.SelectedIndex = 0

End Sub

Protected Sub ddlAssign_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlAssign.SelectedIndexChanged Dim s As String

    s = ddlAssign.SelectedValue

  if s = "" then
     initialize
  end if
 if s = "combo1" then
   set_next_menu
 end if

end sub

Wish I had a better description and video to show what I'm doing. (As I wrote in the OP, I don't recommend this technique, but it's what the users expect in this case). Thanks again.

Upvotes: 1

Views: 1323

Answers (3)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

The values manipulated in JS can't be retrieved on the Server side. You can put the SelectedValue in a Hidden field and then you will be able to get it in server side from the Hidden Field

Upvotes: 1

elbillaf
elbillaf

Reputation: 1984

The (an) answer to this question is posted at the end of the initial post. I'm not sure I like the answer, but it worked for me.

Upvotes: 0

Kinexus
Kinexus

Reputation: 12904

I believe in the scenario above, you do not have anything selected the second time around, therefore SelectedValue would be null. When you repopulate the data, you are also able to set the selectedindex to 0, therefore in theory 'selecting' and item and this may help.

Upvotes: 0

Related Questions