Keep form's fields after clicking on button - c#

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.

Related

Getting value from Dropdownlist in Checkboxlist in C#

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"];

DropDown not showing selected value when loaded

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>

Getting HTML drop down list value from code behind

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"];

Server Event for HTML Select control

How can I add a server event for an HTML Select control?
HTML code is like this:
<div class="ui-widget">
<select id="Select1" runat="server">
<option>Select one...</option>
<option>ActionScript</option>
<option>AppleScript</option>
<option>Asp</option>
<option>BASIC</option>
</select>
</div>
C# code is like this:
public void Select1_SomethingChange(object sender, EventArgs e)
{
//random code
}
Now I know just as is it won't work, the 2nd line of the HTML needs an attribute of some kind.. I already tried the only 2 I could find which are these two below
<select id="Select1" runat="server" onServerChanged="Select1_SomethingChange">
<select id="Select1" runat="server" onSelectedIndexChanged="Select1_SomethingChange">
The problem is that the first options event never fires, and the 2nd option just doesn't exist. Please help me out here, any help is welcome.
The 2013 answer is pretty conservative. The select element has the OnChange event, see: this MS doc
The event handler in code-behind is only called after a post event. For Asp controls, you add the AutoPostBack attribute, otherwise you just call the __doPostBack() javascript method.
Complete example:
<select id="select_clients" runat="server"
OnServerChange="SelectClients_Change" onchange="__doPostBack()">
</select>
code-behind:
using System.Web.UI.HtmlControls;//HtmlSelect
protected void SelectClients_Change(object sender, EventArgs e)
{
HtmlSelect HS = sender as HtmlSelect;
ListItem SelectedItem = HS.Items[HS.SelectedIndex];
}
NB: the options I added dynamically in Page_Load using:
select_clients.Items.Add(new ListItem("...name...", "...value...")
Why use the primitive html elements and __doPostBack call instead of the higher level Asp.Net controls? Well, sometimes this is convenient, e.g. in combination with the Bootstrap framework.
When you add runat="server" to an otherwise normal HTML element, it becomes an HtmlControl in the server-side code. Which doesn't expose a lot of events. In order to get richer server-side eventing, you need to use the ASP.NET controls. In this case that would be a DropDownList, which has the events you're looking for.
Something like this:
<div class="ui-widget">
<asp:DropDownList id="Select1" OnSelectedIndexChanged="Select1_SomethingChange" runat="server">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Select one...">Select one...</asp:ListItem>
<asp:ListItem Value="ActionScript">ActionScript</asp:ListItem>
<asp:ListItem Value="AppleScript">AppleScript</asp:ListItem>
<asp:ListItem Value="Asp">Asp</asp:ListItem>
<asp:ListItem Value="BASIC">BASIC</asp:ListItem>
</asp:DropDownList>
</div>

Is there a way to disable a label?

I am using an asp radio button group to list out answers in a form. Here is an example of the structure.
<asp:RadioButtonList id="Q2" runat="server">
<asp:ListItem Text="Go to <a target='_blank' href='http:www.google.com'>Google</a>" Value="a"></asp:ListItem>
<asp:ListItem Text="Go to <a target='_blank' href='http:www.yahoo.com'>Yahoo</a>" Value="a"></asp:ListItem>
</asp:RadioButtonList>
So I want to be able to click on the links and not have the radiobutton associated with it become selected. In IE it works fine but in Firefox the radio button will be selected when i click on the link. I don't really need the label to actually select the proper radio button so is there a way to just disable them either in javascript or somewhere in the asp or C# code?
It's undoubtedly wrapping them within a label element which is giving you the select behavior. You could either generate the code manually -- my choice -- or unwrap them with javascript on the client side.
<div>
<input type="radio" name="Q2" id="Q2_google" value="google" /> <a target="_blank" href="http://www.google.com">Google</a>
...
</div>
or client-side
$(function() {
$('label:has(a)').each( function() {
var html = $(this).html();
$(this).replaceWith($(html));
});
});
Try this:
<a target='_blank' href='http:www.google.com' click='return false;'>Google</a>"
You can use Enabled="false" in ListItem level
Ex:
Google" Value="a" Enabled="false">

Categories