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
}
Related
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);
}
I want to make an extend property of control <asp:DropdownList ID="ddlUser" runat="server" />.
Can I create a .SelectedValue property for <select id="selectUser" runat="server" />
so I can use that property in C# so I can use like ddlUser.SelectedValue .
There are some property need to extend in HTML <select runat="server"> control.
selectUser.Value (need to extended with .SelectedValue property)
ddlUser.SelectedValue
You can't do extension properties in C# 3.0 or 4.0.
However, you can do the following
Define a helper class as extension
public static class SelectExtensionHelper
{
public static string SelectedValue(this System.Web.UI.HtmlControls.HtmlSelect select)
{
return select.Items[select.SelectedIndex].ToString();
}
}
while in the ASP page:
<select id="selectUser" runat="server" >
<option id="op1id" value="val1id"> option1 </option>
<option id="op2id" value="val2id"> option2 </option>
<option id="op3id" value="val3id"> option3 </option>
</select>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
PostBackUrl="~/Default.aspx" Text="Button" />
Use this where you need:
Response.Write("The value is:"+ selectUser.SelectedValue() + " Done");
You can use Extension methods instead ;)
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;
}
I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.
In a view, I have a DropDownList, a button.
How can I remove an item from the DropDownList for each click of the button.
I try to use javascript, but I think that's not working because when I click on the button, nothing happens.
This is the code :
<%:Html.Label("Poste :")%>
<%:Html.DropDownListFor(
model => model.SelectedPoste,
Model.PostesItems,
new { #id = "poste" })%>
<div>
<input type="submit"
value="Enregistrer"
id="btnSave"
onclick="remove()" />
</div>
<script type="text/javascript">
function remove() {
var rm = document.getElementById('btnSave');
var poste = document.getElementById('poste');
poste.removeItem();
}
</script>
using jQuery
<select id="poste">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<input type="button" id="btnSave" value="Remove current item" />
<script type="text/javascript">
$(function () {
$('#btnSave').click(function () {
$('#poste option:selected').remove();
return false;
});
});
</script>
EDIT: binding the click event using jQuery
The generated HTML will give the select element an ID of "SelectedPoste", not "poste" as you are attempting to set.
Use remove to remove an item.
Change your javascript to be:
var poste = document.getElementById('SelectedPoste');
poste.remove(poste.selectedIndex);
Edit: The generated HTML for the select will be:
<select id="poste" name="SelectedPoste">...</select>
Either of these two lines will get that elements:
var poste = document.getElementById('poste');
or
var poste = document.getElementById('SelectedPoste');
(Atleast in IE10)
Then to remove the selected item from the list, do:
poste.remove(poste.selectedIndex);
This does not remove the button :)
Edit 2: Like Dimitar's answer, you need to change your function name from remove to something else.
Once you do that, my code above works in IE and Chrome.
Using vanilla JavaScript you can do:
<script type="text/javascript">
function removeOption() {
var posteElement = document.getElementById('poste');
var currentIndex = posteElement.selectedIndex;
posteElement.removeChild(posteElement[currentIndex]);
return false;
}
</script>
That's all you need. Also make sure you rename your function to anything other than remove():
<input type="submit"
value="Enregistrer"
id="btnSave"
onclick="removeOption()" />
Check out this (not very nice inline-fiddle).
However I'd strongly suggest at least looking into a library such as jquery, (this would be much easier than with vanilla.js). Check out Andre's answer.
Try this:
$("#poste option[value='X']").each(function() {
$(this).remove();
});
Or to be more terse, this will work just as well:
$("#poste option[value='X']").remove();
Example:
$("#btnSave").click(function(){
$("#poste option[value='X']").remove();
});
Remember to use JQuery :)
Im trying to get a value or a text from the "select option" tag through C#, but I run on some issues :
this is my code :
<select id="country" >
<option value="" >Select One...</option>
<%for (int i = 0; i < dt.Rows.Count; i++)
{%>
<option value="<%=dt.Rows[i][0].ToString() %>" ><%=dt.Rows[i][1].ToString() %></option>
<%} %>
</select>
and this is my code behind :
string value = Request.Form.Get("country");
its keep getting null in my value. and if Im trying to set my <select> tag with runat="server", its getting an error :
Code blocks are not supported in this context
any help?
Thanks!
Form elements are posted by their name attribute, not id.
Try
<select id="country" name="country">