Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox not acknowledging a selection

Status
Not open for further replies.

HebieBug

Programmer
Jan 8, 2001
354
0
0
JP
Got a real Bizarre one.
Am populating a list box via linq.
Just have a button that should tell me the listbox id via a text box.
But when I push the button it states that the selectedItem is nothing.
The data is being sent to the listbox. It is almost like the listbox is stating that nothing is selected.
Heres the code:
Populate the Listbox:
----------------------------------------
Dim DC As DataClasses1DataContext
DC = New DataClasses1DataContext
Me.LB_Job.DataSource = From C In DC.SM_CLIENTs, J In DC.SM_JOBs _
Where (J.SM_JOB_ACTIVE = True) And _
C.SM_CLIENT_ID = J.SM_CLIENT_ID
Select J.SM_JOB_ID, C.SM_CLIENT_NAME

Me.LB_Job.DataTextField = "SM_CLIENT_NAME"
Me.LB_Job.DataValueField = "SM_JOB_ID"
Me.LB_Job.DataBind()

--------------------------------
AND NOW THE BUTTON:
Protected Sub Btn_Start_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Start.Click
Me.TxtBoxOperator.Text = Me.LB_Job.SelectedItem.Text

End Sub

Any ideas? I'm stumped
 
are you populating the list box in the page load method and are you checking for postback? if you are binding i the page load event and not checking for postback, than the items collection of the listbox is reset and your selection is cleared. then then button click fires.

resolution, check for postback
Code:
void page_load(object sender, eventags e)
{
   if(ispostback) return;

   load listbox from linq
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Your issue is pretty absurd. Maybe you should try to check the following:
1. Is the listbox populated at run time?
2. Is the button a web control or html control?
3. Do you have the runat="server" attribute in the button control?
4. Try changing Me.TxtBoxOperator.Text = Me.LB_Job.SelectedItem.Text
to
TxtBoxOperator.Text = LB_Job.SelectedItem.Text

If it persist, and it is not pertinent you use vb to get it, then try this javascript/jquery

<script >
function getDDLValue() {
document.getElementById("#<%=TxtBoxOperator.ClientID%>").value = $("#<%=LB_Job.ClientID%>").val()
}
</script>


at the button do
<input id="Button1" type="button" value="clickme" onclick="getDDLValue()" />
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top