I have code below:
<select id="testSelect">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<asp:Button ID="btnTest" runat="server" Text="Test it!" onclick="btnTest_Click" />
I need to get selected options' value on postback. How can I do this with asp.net?
You need to add a name to your <select> element:
<select id="testSelect" name="testSelect">
It will be posted to the server, and you can see it using:
Request.Form["testSelect"]
I've used this solution to get what you need.
Let'say that in my .aspx code there's a select list runat="server":
<select id="testSelect" runat="server" ClientIDMode="Static" required>
<option value="1">One</option>
<option value="2">Two</option>
</select>
In my C# code I used the code below to retrieve the text and also value of the options:
testSelect.SelectedIndex == 0 ? "uninformed" :
testSelect.Items[testSelect.SelectedIndex].Text);
In this case I check if the user selected any of the options. If there's nothing selected I show the text as "uninformed".
If you would use asp:dropdownlist you could select it easier by testSelect.Text.
Now you'd have to do a Request.Form["testSelect"] to get the value after pressed btnTes.
Hope it helps.
EDIT: You need to specify a name of the select (not only ID) to be able to Request.Form["testSelect"]
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> HtmlSelect Example </title>
<script runat="server">
void Button_Click (Object sender, EventArgs e)
{
Label1.Text = "Selected index: " + Select1.SelectedIndex.ToString()
+ ", value: " + Select1.Value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
Select an item:
<select id="Select1" runat="server">
<option value="Text for Item 1" selected="selected"> Item 1 </option>
<option value="Text for Item 2"> Item 2 </option>
<option value="Text for Item 3"> Item 3 </option>
<option value="Text for Item 4"> Item 4 </option>
</select>
<button onserverclick="Button_Click" runat="server" Text="Submit"/>
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
Source from Microsoft. Hope this is helpful!
Java script:
use elementid. selectedIndex() function to get the selected index
Related
I'm new to VB.net and I'm working on a project in Visual Studio 2015. In one of my application's tab I'm trying to INSERT new records into a SQL Table via form post, but once I click the button nothing happens and no new records are added. My current code is below, any help would be great! Thanks:
Contact.aspx
<%# Page Title="Contact" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Contact.aspx.vb" Inherits="Contact" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<head>
<meta charset="utf-8">
</head>
<body>
<br />
<p style="font-size: 15px;">Fill out the form below and click SUBMIT once completed:</p>
<form id="submitData" method="post">
<fieldset>
<p><label for="Term">Term:</label>
<input type="text" name="Term" required />*
</p>
<p><label for="genre">Definition:</label>
<input type="text" name="Definition" required />*
</p>
<p><label for="year">Long Description:</label>
<textarea rows="3" type="text" name="LongDescription" required></textarea>*
</p>
<p><label for="title">Category:</label>
<select name="Category" style="width: 312px; height: 31px; font-family: Arial, Helvetica, sans-serif; font-size: 1.2em;">
<option selected="selected">
<option value="Branding">Branding</option>
<option value="Business">Business</option>
<option value="Business Management">Business Management</option>
<option value="CRM">CRM</option>
<option value="Communications">Communications</option>
<option value="Dental Health">Dental Health</option>
<option value="Education">Education</option>
<option value="General">General</option>
<option value="Governmental">Governmental</option>
<option value="IT">IT</option>
<option value="Marketing">Marketing</option>
<option value="Nutrition">Nutrition</option>
<option value="State Laws">State Laws</option>
<option value="Other">Other...</option>
</select>
</p>
<p><label for="title">Date Loaded:</label>
<input type="text" name="DateLoaded" id="datepicker" required />*
</p>
<p><label for="title">Provided By (your name):</label>
<input type="text" name="ProvidedBy" required />*
</p>
<p><label for="title">Department:</label>
<input type="text" name="Department" required />*
</p>
<p><label for="title">Internal Expert or SME for this topic:</label>
<input type="text" name="SME" />
</p>
<p><asp:Button ID="buttonSubmit" runat="server" OnClick="buttonSubmit_Click" Text="Submit" /></p>
</fieldset>
</form>
<br />
<p>* Please allow between 24 to 48 hours to have your term added to the dictionary.</p>
<br />
<a style="font-size: 16px; font: bold; background-color: black; color:white; text-decoration:none; padding:5px;" href="Default.aspx">Back</a>
</body>
</asp:Content>
Contact.aspx.vb
Partial Class Contact
Inherits Page
Protected Sub buttonSubmit_Click(sender As Object, e As EventArgs)
Dim sqlConnection1 As New Data.SqlClient.SqlConnection("ConnectionString HERE")
Dim cmd As New Data.SqlClient.SqlCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "INSERT INTO table_terms (Term, Definition, LongDescription, Category, DateLoaded, ProvidedBy, Department, SME) VALUES (Term, Definition, LongDescription, Category, DateLoaded, ProvidedBy, Department, SME)"
cmd.Connection = sqlConnection1
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
End Sub
End Class
Any hint would be great. Thanks a lot!
M.C.
Your command text is jacked up. Strings need to be quoted, for instance. Try running the statement in SSMS. If it doesn't work from there it's not going to work from .Net.
Also, what you're doing is a security issue. Use stored procedures and pass values as parameters. Building up a statement to execute in .Net will lead both to worse performance and injection vulnerability.
I have an input in ASP.NET form. I did not use ASP.NET controls, but the adjective runat="server" was used. Like this:
<input id="cmbIOType" name="cmbIOType" runat="server" list="listcmbIOType" autocomplete="off" data-id="564" value="111" />
I'm using strig valcmbIOType = cmbIOType.value; to get the input value. I also need to data-id the input.
I used string idcmbIOType = cmbIOType.Attributes["data-id"] but returned NULL.
Meanwhile, the data-id is made by Jquery.
Please help.
i used this:
<input id="cmbLoc" name="cmbLoc" runat="server" list="listcmbLoc" autocomplete="off" />
<datalist id="listcmbLoc">
<option data-id="15" value="CPF"></option>
<option data-id="18" value="HOLD"></option>
<option data-id="19" value="CCR"></option>
</datalist>
<div style="display: none"><input id="cmbLoc_ID" type="text" name="cmbLoc_ID" runat="server"/></div>
<script type="text/javascript">
$(document).ready(function () {
$("#cmbLoc").change(function () {
$("#MainContent_cmbLoc_ID").val($('#listcmbLoc').find('option[value="' + $(this).val() + '"]').data('id'));
});
});
</script>
And enter cmbLoc_ID.value in the code behind it to get data-id.
I'm not sure what I'm doing wrong here. I'm try to get an OnSelectedIndexChanged event working, but I'm trying to do it without using the asp form controls.
In the below example the OnServerClick works for the <a> element but neither the OnSelectedIndexChanged nor OnServerClick seem to work for the <select>.
<%# Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html>
<html>
<head>
<script runat="server">
Sub HtmlAnchor_Click_1(sender As Object, e As EventArgs)
Message.InnerHtml = "this doesn't work"
End Sub
Sub HtmlAnchor_Click_2(sender As Object, e As EventArgs)
Message.InnerHtml = "this works"
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<select id="AnchorSelect" name="select1" OnSelectedIndexChanged="HtmlAnchor_Click_1" runat="server">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br /><br />
<a id="AnchorButton" onserverclick="HtmlAnchor_Click_2" runat="server">Click Here</a>
<br /><br />
<span id="Message" runat="server"/>
</form>
</body>
</html>
Any ideas, or solutions would be appreciated.
Cheers.
select is an HTML input and the OnSelectedIndexChanged would be a Javascript function that gets called.
Use <asp:DropDownList> and set autopostback=true. Then, you would put the OnSelectedIndexChanged in your codebehind to use it.
Check out this example: DropDownList's SelectedIndexChanged event not firing
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="account-content">
<option value="" onclick="tbl_city"> All</option>
<option value="">City</option>
<option value="">State</option>
<option value="">Country</option>
<option value="">Project</option>
<option value="">Project Detail</option>
<option value="">Project Category</option>
<option value="">User</option>
<option value="">User Detail</option>
</asp:DropDownList>
Use Entity Framework. Bind data to the gridview and use SelectedIndexChanged property of DropDownList to Redirect to that page . you can use something like this:
private DataEntities dataContext;
dataContext = new DataEntities1();
gv.DataSource = dataContext.objectName.ToList<TEntity>();
gv.DataBind();
Appoligise for what seems to me like a silly question, I'm not that familair with the web-based side of C# using ASP, and I've been unable to find a solution online - all the related things I have found have been for either <ASP:> controls or about adding items to the dropdown.
I am trying to return the string contains within the <option></option> tags, i.e. House, Reg, Year or Status. As I need to add the value to part of my Session variable in the code behind.
So I have added the runat=server parameter, and now I can access the select part via it's ID.
The count returns 1, but the item don't appear to be the string "house"... I can't seem to access the part I want, I thought I would be able to loop through items with findByText but it returns null.
Having a brain fart over this, could someone poke me back on track?
<div class="filterDropDowns" runat="server" id="filterDropDowns" name="filterDropDowns" visible="false">
<select name="houseFilter" runat="server" id="houseFilter" class="row1 filterDropDown">
<option value="*" class="row1">House</option>
</select>
<select name="regFilter" runat="server" id="regFilter" class="row1 filterDropDown">
<option value="*" class="row1">Reg</option>
</select>
<select name="yearFilter" runat="server" id="yearFilter" class="row1 filterDropDown">
<option value="*" class="row1">Year</option>
</select>
<select name="statusFilter" runat="server" id="statusFilter" class="row1 filterDropDown">
<option value="*" class="row1">Status</option>
</select>
</div>
<div class="filterButtonContainer" runat="server" id="filterButton" name="filterButton" visible="false">
<input type="button" value="Filter" class="keyboardButtonStyle filterButton" onclick="javascript:DoFilter();" />
</div>
<div>
Have you tried
regHouse.InnerText
or
regHouse.Items[regHouse.SelectedIndex].Text
I think this also should be
<select name="houseFilter" runat="server" id="houseFilter" class="row1 filterDropDown">
<option value="House" class="row1">House</option>
....