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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I lookup a value from a selected text in a dropdown?

Status
Not open for further replies.

ajoyprabhu

Technical User
Sep 17, 2001
13
US
I am trying to create a file name based upon the firs name, last name and job applied for ("JobType"- which is in a separate table)

In the VB code, "JobTypeID.SelectedItem.Text" gives me the first item of the dropdownlist, not the selected one!!

If I put Request.Form("JobTypeID") in the VB code, it gives me the correct index number, how do I convert it into the it text value?

The code is as follows:

<form enctype="multipart/form-data" runat="server" name="form1" method="POST" action="">
<table width="75%" border="0">
<tr>
<td nowrap>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td nowrap><div align="right">Contractor:</div></td>
<td> <% ContractorTypeID.SelectedIndex = ContractorTypeID.Items.IndexOf(ContractorTypeID.Items.FindByValue(dsCandidates.FieldValue("ContractorTypeID", Nothing) )) %> <asp:dropdownlist ID="ContractorTypeID"
DataSource="<%# dsSubContractor.DefaultView %>"
DataTextField="Contractor"
DataValueField="ContractorID"
runat="server"> </asp:dropdownlist></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">JobType:</td>
<td> <% JobTypeID.SelectedIndex = JobTypeID.Items.IndexOf(JobTypeID.Items.FindByValue(dsCandidates.FieldValue("JobTypeID", Nothing) )) %> <asp:dropdownlist id="JobTypeID"
DataSource="<%# dsJobs.DefaultView %>"
DataTextField="JobType"
DataValueField="JobID"
runat="server"> </asp:dropdownlist> </td>
</tr>
<tr>
<td nowrap><div align="right">First Name: </div></td>
<td><input type="text" id="FirstName" name="FirstName"></td>
</tr>
<tr>
<td nowrap><div align="right">Last Name: </div></td>
<td><input type="text" id="LastName" name="LastName"></td>
</tr>
<tr>
<td nowrap><div align="right">Upload Resume:</div></td>
<td><input id="uploadedFile" type="file" runat="server">
<input type="hidden" name="MM_insert" value="form1"></td>
</tr><input type="hidden" name="Status" value="New">
<tr>
<td nowrap><div align="right"></div></td>
<td><p>
<input name="button" type=button id="button3"
value="Upload"
onserverclick="Upload_Click"
runat="server">
</p>
<p>
<asp:label text='<%# ContractorTypeID.SelectedItem.Text %>' runat="server"/>
<asp:label ID="message" runat="server"/>
</p>
<p></p></td>
</tr>
</table>
</form>

<script language="VB" runat="server">

Dim savePath As String = "C:\Inetpub\Sub Upload_Click(source As Object, e As EventArgs)
Page.DataBind

If Not (uploadedFile.PostedFile Is Nothing) Then
Try
Dim postedFile = uploadedFile.PostedFile
'Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim filename As String = JobTypeID.SelectedItem.Text & "_" & Request.Form("FirstName") & "_" & Request.Form("LastName") & ".doc"

Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
'postedFile.SaveAs(savePath & filename)
message.Text = savePath & filename & " uploaded" & _
"<br>Name: " & filename & "Type: " & contentType & _
"<br>Length: " & contentLength.ToString() & _
Catch exc As Exception
message.Text = "Failed uploading file"
End Try
End If
End Sub
</script>
 
Looks like you're calling Page.Bind and rebinding your controls which is going to reset the JobType DropDown. You need to do that after you get the value.
 
I have removed the page.bind and it still does the same thing :(
 
Try adding EnableViewState="True" to your DropDownList properties. Seems like you're losing your ViewState somewhere along the way. Also, why don't you add runat="server" to the FirstName and LastName text boxes? You should be able to access their values with FirstName.Value & " " & LastName.Value & ".doc". If you can get a hold of a copy of Visual Studio 2003 do it. DW is no way to go with .NET.
 
still nothing. DO I need Page.Bind at all? I took it out of my Sub and was wondering if that needed as a Sub under Page.Load() ??

I am a neubie at .NET, so just baby steps...maybe I should go right into VS and try it...

THanks for your help so far....did not work... I put bouth in there... run at server and enableviwstate ...any other tips?

:)


 
Create a Page_Load() sub. Put the code that creates your DataSet and Binds your DropDownList in there. This way you're only populating the DropDown on the initial page load.
Code:
Sub Page_Load(object sender, System.EventArgs e)
If Not Page.IsPostBack
   'Create your database call and dataset here...
   JobTypeID.DataSource = dsJobs
   JobTypeID.DataBind()
End If
End Sub
If you are just starting out with .NET and can use VSNet go for it. You'll be glad you did. DW does't support 99% of the stuff that you need to create "real" ASP.NET applications and once you start letting it write code for you it really clouds things up.
 
sorry, for this dumb question. DO you mean to say that I need to have my 'variables' that i use int he form preloaded through the page_load subroutine?

Still trying hard to understand this s/w! ...and also looking to see if I can at least get a trial version of VS :)

 
Variable scope is limited to the Sub/Function that it's declared in.
Code:
dim a as string = "I'm a page level variable." ' Page Level
Sub Sub1()
  dim b as string = "Hello World!" ' can only be used here
  Response.Write(a & "<br/>") 'writes out the contents of variable a
  Response.Write(b & "<br/>") ' writes out "Hello World!"
End Sub

Response.Write(b) ' throws an error - b doesn't exist here
Go here and check out some of the code examples starting with the WebForms category. They
should clear some things up.
 
ok got that, was wondering about how to insert the dataset and db calls into a page_load()? These are currently like so--

<MM:DataSet
id="dsCandidates"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connCandidates") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_connCandidates") %>'
CommandText='<%# "SELECT * FROM Candidates" %>'
Debug="true"
>
</MM:DataSet>

<MM:DataSet
id="dsSubContractor"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connCandidates") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_connCandidates") %>'
CommandText='<%# "SELECT Contractor, ContractorID FROM Subcontrators" %>'
Debug="true"
>
</MM:DataSet>

<MM:DataSet
id="dsJobs"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connCandidates") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_connCandidates") %>'
CommandText='<%# "SELECT * FROM Jobs" %>'
Debug="true"
> </MM:DataSet>

<MM:pageBind runat="server" PostBackBind="true" />

do you mean these??

SOrry about sounding dumb in .net. will look up asp.net wite too...can i use webmatrix instead? will that be better than MM??

Thanks!

 
Start by not using Dreamweaver :)
In VB it goes something like this:
Code:
Sub Page_Load(object sender, System.EventArgs e)
If Not Page.IsPostBack
   Dim strConn as string
   strConn = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connCandidates")
   Dim conn as SqlConnection = new SqlConnection(strConn)
   Conn.Open()
   Dim strSql as string = "SELECT JobTypeID,JobType FROM tbl_JobTypes"
   Dim da as SqlDataAdapter = new SqlDataAdapter(strSql,conn)
   Dim ds as DataSet = new DataSet()
   da.Fill(ds,"JobTypes")
   Dim dt as DataTable 
   dt = ds.Tables("JobTypes")
   JobTypeID.DataSource = dt
   JobTypeID.DataBind()   
   JobTypeID.DataSource = dsJobs
   JobTypeID.DataBind()
End If
End Sub
Here's an article that may help.
For that matter take a look at the whole site. It will answer any question that you may have.

The first thing that you need to do is get away from DreamWeaver and letting it set up your Connection String, DataSources etc.
Good Luck
 
ok. calls for a re-thinking of the tools to use! I think that that is the bottom-line.

I'll try webmatrix and see if that helps, if not, then try and get VS to help.

THanks a ton for your advice!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top