How to make extend property for HTML Select server control? - c#

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 ;)

Related

Issue in getting value of input type = radio from asp.net childf page of a master page to c#

I am having an issue in getting the value of an input type = radio In c#
My form is in the master page as follows:
<form id="form1" runat="server">
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<div class="header">
<asp:Label ID="DoctorName" runat="server" Text="" CssClass="Label"></asp:Label>
<asp:ImageButton ID="logout" ImageUrl="~/Logout.png" style ="float:right; margin-right:20px;" runat="server" Height="40" Width="30"/>
</div>
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
In my child page (c# file) I just tried to do the Request.form["MouvementYesNo"] but it returned a null value what should I do as another method ? should the two radio buttons have the same id? or how to get them by name?
<li>
<label>Troubles du mouvement <span class="required">*</span></label>
<input type="radio" runat="server" name="MouvementYesNo" value="Yes" checked> Yes
<input type="radio" runat="server" name="MouvementYesNo" value="No"> No
</li>
Since all of HTML radio buttons have runat="server" attribute which means they're accessible from code behind, put different control IDs and use if condition to check which radio button is selected.
Markup
<input id="RadioButton1" type="radio" runat="server" name="MouvementYesNo" value="Yes" checked> Yes
<input id="RadioButton2" type="radio" runat="server" name="MouvementYesNo" value="No"> No
Code behind
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
// do something
}
else if (RadioButton2.Checked == true)
{
// do something else
}
// other stuff
}
Note that if you want to use Request.Form, the HTML server control attribute (i.e. runat="server") should be removed, also Request.Form contains null value if no radio button input elements are selected.
Side note:
Better to use RadioButtonList server control like this:
<asp:RadioButtonList ID="MouvementYesNo" runat="server">
<asp:ListItem value="Yes">Yes</asp:ListItem>
<asp:ListItem value="No">No</asp:ListItem>
</asp:RadioButtonList>
And you can get currently selected radio button value later:
var selected = MouvementYesNo.SelectedValue;
Similar issue:
Getting value from html radio button - in aspx-c#

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

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);
}

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
}

Weird behaviour of DropDownList Asp.net

Weird behaviour happening in my DropDownList Asp.net
My Code:
<asp:DropDownList ID="DDLFromMinutes" runat="server" Width="20%">
<asp:ListItem Text="00" Value="00"></asp:ListItem>
<asp:ListItem Text="15" Value="15"></asp:ListItem>
<asp:ListItem Text="30" Value="30"></asp:ListItem>
<asp:ListItem Text="45" Value="45"></asp:ListItem>
</asp:DropDownList>
However when i run it for example if 15 is the Selected Value i get it Twice in my DropDownList
While Debugging in FireBug get the following:
<select id="ContentPlaceHolder1_DDLFromMinutes" style="width:20%;" name="ctl00$ContentPlaceHolder1$DDLFromMinutes">
<option value="00" selected="selected">30</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
Code behind:
string Frominput = seprateFromTime[1];
string Frommin = Frominput.Substring(0, 2);
DDLFromMinutes.SelectedItem.Text = Frommin;
if (DDLFromMinutes.Items.Count > 0)
{
DDLFromMinutes.SelectedIndex = DDLFromMinutes.Items.IndexOf(DDLFromMinutes.Items.FindByText(Frommin));
}
While saving the Data is use DDLFromMinutes.SelectedItem.Text Could this be an issue?
You need to use MyDropDown.SelectedValue instead of selectedItem.Text.
Details here: MSDN
do you want to get the value of drop down list? If so, I think you need to change SelectedItem to SeletedValue.

Categories