How to post multiple Select values using HttpWebRequest in Asp.Net - c#

my goal is to use Asp.Net's HttpWebRequest to submit multiple values to a webpage, as if they were sent from a <select> element.
The code i use for creating the data string is as follows:
NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("username", "myusername");
outgoingQueryString.Add("password", "mypassword");
string postData = outgoingQueryString.ToString();
Now lets say I have the following select for example:
<select name="banana" multiple="">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
I need to submit options 1 and 2, how do i add this to the query string? as an array, or can i add several values with the same name?
Thanks in advance.

You can pass comma separed values, because when you do actually post a multi select control, you get comma separated values in Request, so to emulate, you can pass comma separated values.
Following HTML used. Note select is a server control here.
<form id="form1" runat="server">
<div>
<select name="banana" multiple="" id="sl" runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<asp:Button ID="btn" runat="server" Text="sub" OnClick="btn_Click" />
</div>
</form>

You can simply Use ArrayList
ArrayList MyArray;
if (Request.QueryString["MyArray"] != null)
{
MyArray = (ArrayList)Request.QueryString["MyArray"];
}
else
{
MyArray = new ArrayList();
}
Add values to ArrayList
if (MyArray.IndexOf(checkAllIndex) == -1)
{
MyArray.Add(checkAllIndex);
}
Remove from ArrayList
if (MyArray.IndexOf(checkAllIndex) != -1)
{
MyArray.Remove(checkAllIndex);
}

Related

Get selected values from DB in select list

I want dropdown auto select value from DB while loading. For example i have values like this:
<option value="1">Mango</option>
<option value="2">Orange</option>
but I want when dropdown load orange selected because in previous form I already select orange.
<select class="form-control" data-plugin="select2" id="ddl_fruit" name="ddl_fruit" disabled="false" data-select2-id="2" tabindex="-1">
<option value="">Select fruit</option>
#if (ViewBag.fruitname!= null)
{
foreach (var item in ViewBag.fruitname)
{
<option value="#item.ID">#item.fruitname</option>
}
}
</select>
I am using ADO.NET and using not html.dropdown.
How do I solve the problem?
foreach (var item in ViewBag.fruitname)
{
if(#item.fruitname == "Orange")
<option value="#item.ID" selected = "selected">#item.fruitname</option>
else
<option value="#item.ID">#item.fruitname</option>
}
if you do not want to use if condition or html helpers then you use Jquery to select specific item.
Note: if you are using sections please put this code in it.
$(function(){ $('#ddl_fruit').val('2') // for orange })
Pass selected value in ViewBag.SelectedFruit from Controller to View.
In View:
foreach (var item in ViewBag.FruitName)
{
<option value="#item.Id" #(ViewBag.SelectedFruit == item.FruitName ? "selected" :"")>#item.FruitName</option>
}

Programatically change value based on selected item from dropdownlist

I'm still getting used to changing over from asp.net to asp.net mvc and I know that it doesn't use on action commands but I'm trying to change the text of a label based on when a user selects an item from a dropdownlist. I'm really not sure where to start :(
You can easily get this done using some jQuery, here I made a Fiddle for you.
Below is how you HTML should look like, incase you are using pure HTML in your views or even if you are using #Html.LabelFor or #Html.DropDownListFor
HTML
<label id="myLabel">Select a fruit:</label>
<select id="fruitSelector">
<option val="">None</option>
<option val="apple">apple</option>
<option val="orange">orange</option>
<option val="mango">mango</option>
</select>
jQuery
$("#fruitSelector").change(function(){
$("#myLabel").text("Fruit has been selected");
});
Related help
https://stackoverflow.com/a/16828702/1182982
https://stackoverflow.com/a/14606324/1182982
Here is the simple example using jquery
#Html.DropDownList("State", ViewBag.StateName as SelectList,
"Select a State",
new { id = "State" })
<label id="lbl1"></label>
<script type="text/jscript">
$(function () {
$('#State').change(function () {
$('#lbl1').text($('#State').val());
});
});
</script>
You can try something like this
<label id="item">Selected Item: </label>
<select id="selector">
<option value="">None</option>
<option value="JS">JavaScript</option>
<option value="aspnet">Asp.net</option>
<option value="mvc">Asp.Net MVC</option>
</select>
<label id="result"></label>
$("#selector").change(function()
{
$("#result").text($(this).val());
});
http://jsfiddle.net/PLbnS/
To start with, your aspx should look like this:
<asp:DropDownList AutoPostBack="true" runat="server" ID="myListDropDown"
CssClass="text" OnSelectedIndexChanged="myListDropDown_Change" />
And your code-behind file:
private void myListDropDown_SelectedIndexChanged(object sender, System.EventArgs e)
{
//put your code here
}
Simple :
Write this code in your SelectedValueChanged Event of dropdownlist :-
private void dropdownlist_SelectedValueChanged(object sender, EventArgs e)
{
dropdownlist.Items["abc"]=label.text;
dropdownlist.Items["xyz"]=label.text;
}

HTML form submit, .Net, save fields as XML

We have an HTML page with an form on it.
We are prepopulating a form from an XML file using jQuery AJAX.
We would like the form, on submit, to save over our default.xml file. Most likely we will use ASP.Net as our server side.
Our question would be, what would the ASP.Net/C# look like in order to save the info in put in the form and save over our XML file.
Thanks in advance.
EDIT
This is our example:
<option id="timeBlockOne">
<select>12:00</select>
<select>1:00</select>
</option>
<option id="titleBlockOne">
<select>Title One</select>
<select>Title Two</select>
</option>
<option id="titleBlockTwo">
<select>Title three</select>
<select>Title Four</select>
</option>
<option id="timeBlockTwo">
<select>12:00</select>
<select>1:00</select>
</option>
...etc up to 10 blocks.
<button action="Post">Submit</button>
Desired XML output.
<main>
<itemOne>
<timeBlockOneValue>Value selected from form</timeBlockOneValue>
<titleBlockOneValue>Value Selected from form</titleBlockOneValue>
</itemOne>
<itemTwo>
<timeBlockTwoValue>Value selected from form</timeBlockTwoValue>
<titleBlockTwoValue>Value Selected from form</titleBlockTwoValue>
</itemTwo>
...etc
</main>
Hopefully this helps out with my question.
Alright, here is a possible way to get you on the right track, please let me know if this is not clear.
JavaScript:
function GetXMLButton() {
var xml = "<main>";
var i = 1;
// Loop for each OPTION element in the myForm div
$("#myform select").each(function() {
// Get value of selected option.
var selectedValue = $(this).val();
xml = xml + "<item_" + i + "><" + this.id + ">" + selectedValue + "</" + this.id + "></item_" + i + ">";
i++;
});
xml = xml + "</main>";
// Call C# WebMethod to save into xml file
PageMethods.SaveXMLFile(xml);
}
ASPX: Make sure to set EnablePageMethods = true in the ScriptManager object. Also I think you might have switched your OPTION and SELECT tags, check your code, it should be SELECT then OPTION tag.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="fileupdatepanel">
<ContentTemplate>
<div id="myForm">
<select id="timeBlockOne">
<option selected>12:00</option>
<option>1:00</option>
</select>
<select id="titleBlockOne">
<option>Title One</option>
<option selected>Title Two</option>
</select>
<select id="titleBlockTwo">
<option selected>Title three</option>
<option>Title Four</option>
</select>
<select id="timeBlockTwo">
<option>12:00</option>
<option selected>1:00</option>
</select>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="button1" runat="server" Text="Get XML" OnClientClick="return GetXMLButton();" />
C# Code-Behind:
You need to reference the following:
using System.Web.Services;
Then add the following method and make sure you put [WebMethod] before method declaration:
[WebMethod]
public static void SaveXMLFile(string formXMLData)
{
// Put your code to convert formXMLData string to an XML File and save/upload on/to server
}

change the value of a dropdown list

I am trying to change the value of a drop down list using the following code on the click event handler. Nothing actually changes when the button is pressed. What am I missing? Is this the correct way to do this?
HtmlDocument document = webBrowser1.Document;
HtmlElement salutation = document.GetElementById("status");
salutation.SetAttribute("value", "Mr");
Here is the html
<select id="status">
<option selected="selected" value="">Select</option>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
</select>
You could try this (based on your code)
HtmlDocument document = webBrowser1.Document;
HtmlElement salutation = document.GetElementById("status");
var option = salutation.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("Mr"));
option.SetAttribute("selected", "selected");
Are you selecting the <select> tag? If so, don't use that. Select the <option>, just like this:
<select>
<option value="volvo">Volvo</option>
<option value="saab" selected="selected">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
</select>
To see it in action: See this link
Using some codes from scartag, how about this:
HtmlElement salutation = document.GetElementById("status");
var option = salutation.Children.Cast<HtmlElement>().
Where(x => x.GetAttribute("selected").Equals("selected")).First();
if (option != null) option.SetAttribute("value","Mr,");

Setting the first default value in a drop down list MVC

I want to be able to set the first value in my dropdown list as "Select a poll", and then load all my select items from my viewdata respectively. How would I go about doing that? My block of code right now looks something like this:
<select name="ddlPolls" id="ddlPolls">
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>
This returns a dropdown list of my polls, but I want the first value in that dropdown list to be "Select a poll" so that I can use jquery to perform .change ajax functions to load another dropdown list. Any help is much appreciated!! :D
Just write your code as follow:
<select name="ddlPolls" id="ddlPolls">
<option value="0">Select a poll</option>
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>

Categories