I have drop down list and asp button in my page like this:
<select id="select1" >
<option value="smiley.gif">Smiley</option>
<option value="angry.gif">Angry</option>
<option value="stickman.gif">Stickman</option>
</select>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
Once the button is clicked, I need to get the selected value from the drop down list and save it in the session.
But when the list is changed, it should not reload the page (Simply I don't need to use runat="server").
How to do this?
You get the value from the Request object.
Edit, thanks to comment by #Péter, add name property to the select element:
<select id="select1" name="select1">
<option value="smiley.gif">Smiley</option>
<option value="angry.gif">Angry</option>
<option value="stickman.gif">Stickman</option>
</select>
In code-behind:
var selectedOption = Request["select1"];
Though, if you are using asp.net I suggest you use the server version (runat=server) of the select and thereby could access the value directly from a Select object in code-behind.
To avoid postback you can set AutoPostback=false, the following solution would not post back when selecting a value in the drop down list. And you get the bonus of type safety etc by using an asp.net control for your drop down:
<asp:DropDownList id="select1" runat="server" AutoPostback="False">
<asp:ListItem Value="smiley.gif" Text="Smiley"/>
<asp:ListItem Value="angry.gif" Text="Angry"/>
<asp:ListItem Value="stickman.gif" Text="Stickman"/>
</asp:DropDownList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
In code-behind:
var selectedOption = select1.SelectedValue;
You need to set name of the html control, and then you can get it from Request Object.
<select id="select1" name="myselect" >
<option value="smiley.gif">Smiley</option>
<option value="angry.gif">Angry</option>
<option value="stickman.gif">Stickman</option>
</select>
Inside you button click event handler:
string selection = Request["myselect"];
Related
While attempting to keep my form' s fields comprising of:
<select ...>
<option></option>
</select>
from my page.aspx, after clicking on a submit button:
<input type="button" id="btnEn" value="Submit" runat="server" onserverclick="btnCalc_Click" style="margin-left:30px; border-radius:0; width:50px; font-size:14px;background-color:#c1c1c1"/>
I have amended the form's tag with onsubmit="return false"
.
However it looks like onsubmit="return false" deactivates btnEn, since it does not call anymore protected void btnCalc_Click(object sender, EventArgs e) held in page.cs.
I've tried other solutions involving UpdatePanel, which did not yield the expected outcome: call the btnCalc_Click() method while keeping in the form values displayed in the textboxes, selected dropdown list, after click.
Thus, any idea (ideally with a minimum of javascript if the latter represents the panacea to this issue) would be appreciated as I'd like to keep calling my bntCalc (written in C#).
Thanks
You should use a server control instead of the html element.
The ASP.Net Webforms DropDownList control renders a select and a set of option tags, and mantains state using ViewState.
For example, this code:
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Value 3" Value="3"></asp:ListItem>
</asp:DropDownList>
will render as:
<select name="ddl" id="ddl">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
And by default will preserve selected value. In the webform, you can access control value using this.ddl.SelectedValue.
Note: the "return false" in the javascript event handler cancels the event. Is standard html/javascript behaviour.
Ok. I did something crazy. This actually renders correct but how would you get the selected value from the dropdown from the server-side using C#?
I tried getting the dropdownlist code
CheckBoxList.Items[0].Text.Substring(CheckBoxList.Items[0].Text.indexOf("<select>"));
But now that I have the dropdown, how do I get the selected value from it?
EDIT 5/15/15 5:39PM EST
I think it would if I wrote the code as to how I am creating this:
CheckBoxList chkBoxLst = new CheckBoxList();
chkBoxLst.Items.Add("Grade");
chkBoxLst.Items.Add("2");
chkBoxLst.Items.Add("3");
chkBoxLst.Items[0].Text += "<select id='Letter' runat='server'>
<option>A</option>
<option>B</option>
<option>C</option>
</select>"
I am creating this dynamically with server-side code.
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Grade <select id="Letter" runat="server">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:CheckBoxList>
If you see what I am trying to do and know a better way, suggestions welcome.
If what you are trying to accomplish is get the selected value, change this
<select id="Letter" runat="server">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
for this
<asp:DropDownList ID="Letter" runat="server" >
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
</asp:DropDownList>
and to get the selected value do this
string selectedValue = Letter.SelectedValue;
You can also get the value from the Form values collection using the id for the SELECT element.
var val = Request.Form["Letter"];
I have dropdown as below:
<asp:DropDownList ID="ddlPriority" runat="server">
<asp:ListItem Text="Yes" Value="True" Selected="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
From above code it should show default text as "Yes" when loaded.
I havent wrote any code behind for binding this drop down. Just wanted to have hard-Code Yes No values in it.
But its not showing me anything selected when i load the page.
Its as below when i load page:
When i done inspect element for this dropdown i got:
<select name="ctl00$MainContent$ddlPriority" id="MainContent_ddlPriority">
<option selected="selected" value="True">Yes</option>
<option value="False">No</option>
</select>
I am wondering why default selected Yes is not comming in drop down...
Note: Yes - No values are comming when i scroll the drop down, but default selected value is not comming when i load the page.
Please help me.
Try to add this property to your DropDownlist: AppendDataBoundItems="true"
try add these properties to your DropDownlist ddlPriority
EnableViewState = "true"
AppendDataBoundItems="true"
Have you try this :
<asp:DropDownList id="ddlPriority" runat="server" >
<asp:ListItem value="0">Select</asp:ListItem>
<asp:ListItem value="1" Selected="True">Yes</asp:ListItem>
<asp:ListItem value="2">No</asp:ListItem>
I want to display company name and logo in dropdownlist.I have fetch all the company name in dropdownlist but i am not able to add logo with name.
I have xml file where company name and images are specified.
Structure of xml file:
<ente>
<nazione>ALBANIA</nazione>
<name>Tirana</name>
<img>tvsh-albania.png</img>
<descri>TVSH - Rruga Ismail Quemali 11, Tirana</descri>
<latitudine>41.321102</latitudine>
<longitudine>19.823112</longitudine>
<zoom>-4</zoom>
</ente>
I have all images in image folder also.
I have use this code:
Protected Sub BindDataToGridviewDropdownlist()
Dim xmlreader As New XmlTextReader(Server.MapPath("XMLFILE.xml"))
Dim ds As New DataSet()
ds.ReadXml(xmlreader)
xmlreader.Close()
If ds.Tables.Count <> 0 Then
ddlDetails.DataSource = ds
ddlDetails.DataTextField = "nome"
ddlDetails.DataValueField = "nome"
ddlDetails.DataBind()
End If
End Sub
What i need to do so i can also display image with company name.
Dropdown list out of the box does not support adding images. Look for 3rd party components.
I think it's quite hard to achieve in html and .net but it should be easier with a jQuery plugin, for example this one
Try this using jQuery
http://www.htmldrive.net/items/show/749/Image-Select-Elements-with-jQuery-and-CSS3.html
Or try this
http://designwithpc.com/Plugins/ddSlick
At example 2.. the HTML need to look like this
<select id="demo-htmlselect">
<option value="0" data-imagesrc="http://dl.dropbox.com/u/40036711/Images/facebook-icon-32.png"
data-description="Description with Facebook">Facebook</option>
<option value="1" data-imagesrc="http://dl.dropbox.com/u/40036711/Images/twitter-icon-32.png"
data-description="Description with Twitter">Twitter</option>
<option value="2" selected="selected" data-imagesrc="http://dl.dropbox.com/u/40036711/Images/linkedin-icon-32.png"
data-description="Description with LinkedIn">LinkedIn</option>
<option value="3" data-imagesrc="http://dl.dropbox.com/u/40036711/Images/foursquare-icon-32.png"
data-description="Description with Foursquare">Foursquare</option>
</select>
You can create that HTML from Code Behind by binding to a repeater
<asp:Repeater id="rp" runat="server">
<HeaderTemplate>
<select id="demo-htmlselect">
</HeaderTemplate>
<ItemTemplate>
<option value='<%#Container.DataItem("name")%>' data-imagesrc='<%#Container.DataItem("img")%>'
data-description='<%#Container.DataItem("descri")%>'>
<%#Container.DataItem("name")%>
</option>
</ItemTemplate>
<FooterTemplate>
</select>
</FooterTemplate>
</asp:Repeater>
I have a listbox on an ASP.NET page:
<select id="Language" multiple size="5" name="Language" runat="server">
<option value="English" selected>English</option>
<option value="French">French</option>
<option value="German">German</option>
</select>
and the form that contains that listbox has a "Submit" button
<asp:Button id="btnPost" runat="server" text="Submit"
postbackurl="ProcessData.asmx"/>
I'd like the page to form a request to ProcessData.asmx?Language=X when X is selected in the listbox.
How do I achieve that?
Your select simply have to be a part of form. As form submitted it submit all inside control values to action.
<form id="submit" action="ProcessData.asmx" runat="server">
<select id="Language" multiple size="5" name="Language" runat="server">
<option value="English" selected>English</option>
<option value="French">French</option>
<option value="German">German</option>
</select>
<input type="submit" text="Submit form"/>
</form>
Submit button with do job for you :)
I suggest that you do the following:
1) Hook on the OnClientClick event of the submit button.
2) Cancel that event using return false.
3) In the OnClientClick event handler send manually a GET request to ProcessData.asmx using XmlHttpRequest and append the needed query string parameter.. I suggest that you use 3rd party JavaScript Ajax library to do that. Here is short example using jQuery:
<script type="text/javascript">
function clientClick() {
var $ = jQuery;
$.get(
url = "ProcessData.asmx",
data = { Language : $get('<%= Language.ClientID %>').value }
);
}
</script>
<asp:Button runat="server" ID="Button1" Text="Postback" OnClientClick="clientClick(); return false"/>
You should be able to get the value on the target page using Request.Form["Language"]
Or set the OnClientClick attribute to
"javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnPost', '', '', false, '', 'ProcessData.asmx?Language=' + GetSelectedLang();, false, false))"
and add GetSelectedLang() javascript function your page.