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

ASP.NET

Status
Not open for further replies.

asraj16

Programmer
May 15, 2004
19
US
Hi,
I am trying to get the value of <Option> tag. Here is my example.

I have

<select id="select1">
<option value="100">Test1</option>
<option value="101">Test2</option>
</select>

I want to get this value using ASP.Net. I am using Request.Form("select1"), Getting only the value but not text. Any inputs.

Thanks
 
Use a DropDownList (which renders as a SELECT) then reference the SelectedItem.Text, SelectedItem.Value or SelectedValue.
 
This Value is coming from different page. I have to retrieve a value of this text from my page. They can use only this type of dropdown.

Thanks
 
It IS that type of DropDown (behind the scenes). With or without the server control, you're just retrieving a value, yes? Better to get the value via myDropDown.SelectedValue instead of Request.Form("myDropDown").

You can also populate the DropDownList in the code-behind based on whatever values you wish (adding ListItems).
 
I am retrieving the dropdown <option> tag value from differenet page. Int the page1 I have <select> code, In the page2 I am doing this.

mports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class WebForm2
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim test1 As String = Request.Form("REPORTORSTATEMENT")
Dim test2 As String
'test2 = Request.Form("TXTRPTID")
'test2 = Request.Form("DropDownList1.SelectedIndex")
test2 = Request.Form("TXTRPTID.SelectedValue")
End Sub
End Class

TXTRPTID is the name of <select> tag. I am not getting anything,

Thanks
Raj
 
My appologies. I see what you're trying to do now.

Out of curiosity, who is the caller (I'm curious about the setup). Do you have control over the calling page?
 
Yes. I am getting the value of REPORTORSTATEMENT. This is a textbox value. But not getting the dropdown box text. How to do this.

Thanks
Raj
 
I want to get this value using ASP.Net. I am using Request.Form("select1"), Getting only the value but not text. Any inputs.

So instead of "100" or "101", you want to get "Test1" or "Test2"?

When you submit a form containing a group of <select> options, only the values of those options are sent in the request. Therefore you will not be able to retrieve the text.

In an asp.net DropDownList control, this is possible by using myDropDown.SelectedItem.Text, but usually you are only interested in the value of the SelectedItem anyway.

In your case you will have to map the values back to what the text was in the original form. Or, just edit the form and make the value and text of the <option>s the same, but it sounds like that is not possible.

Hope that makes sense

Greetings,
Dragonwell
 
No I need to pass the value and text are different. I need to use only this type of dropdown. I can't use the control. Is there any way? Please I need to complete this by Today. This is very urgent.

<form name="Form1" action="WebForm2.aspx" method="post">
<table>
<tr>
<td><select class="palistlg" id="TXTRPTID" size="1" name="TXTRPTID">
<option value="Y" selected>Current</option>
<option value="0010000">FICO Midnight Run</option>
<option value="0010001">ISCD M-F 5:00 AM run</option>
</select>
</td>
</tr>
</table>
<INPUT id="REPORTORSTATEMENT" type="hidden" value="R" name="REPORTORSTATEMENT"> <INPUT class="btnwidthSM" id="submit1" type="submit" value="Run" name="submit1">
<input class="btnwidthSM" id="clear1" type="reset" value="Reset" name="clear1">

</form>

I want to get the text of this selected item in <option> tag?

Thanks for all your help.
 
The correct way to do this with asp.net is to use the asp:DropDownList server control.
Code:
html...
<asp:DropDownList ID="ddl1" Runat="server">
<asp:ListItem Value="Y"></asp:ListItem>
<asp:ListItem Value="0010000"></asp:ListItem>
<asp:ListItem Value="0010001"></asp:ListItem>
</asp:DropDownList>

in code behind...
if( Page.IsPostBack )
{
  Response.Write(ddl1.SelectedValue);
}

If you still want to use the regular select then this will get you the value.
Response.Write(Request.Form["TXTRPTID"]);

Jon Gallant
jon[at]dotnetconnect.com
 
still I am not getting the text? I am passing this option value to a different form and I need to assing the text of <option> tag to a variable also ? Could you please anyone help me? I am really sick of this.

Thanks
Raj
 
.NET doesn't support postback to a different page.

Take the runat=server out of the form tag, set the action and it should work.

<form id="Form1" method="post" action="WebForm4.aspx">

WebForm4.aspx
Response.Write(Request.Form["TXTRPTID"]);

Jon Gallant
jon[at]dotnetconnect.com
 
It is displaying the value not text.

Thanks
Raj
 
Ah,

The text is not passed in the request object. You need to use some sort of programmatic lookup or delimit the value to include the text as well "value|text" or something similiar to it, or simply make the value the same as the text.

Jon Gallant
jon[at]dotnetconnect.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top