Basically I want my label priceLabel to automatically update when the user changes their selection on a dropdown list sizeDropdown
For example, they select a size from sizeDropdown, this then changes an amount decimal and then converts this decimal into a string and puts it into priceLabel. If the user changes there selection on sizeDropdown it automatically reupdates priceLabel.
This is what I currently have but it doesnt work:
In the HTML
<asp:DropDownList ID="sizeDropdown" runat="server" OnSelectedIndexChanged="sizeDropdown_SelectedIndexChanged"></asp:DropDownList>
Then in the aspx.cs
Values get Added in one method:
sizeDropdown.Items.Add("50g");
sizeDropdown.Items.Add("100g");
The in another I try to assign the price:
protected void sizeDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
double amount = 0;
if (sizeDropdown.SelectedItem.Equals("50g"))
{
amount = 4.99;
}
else if (sizeDropdown.SelectedItem.Equals("100g"))
{
amount = 7.99;
}
priceLabel.Text = amount.ToString();
}
Thanks
EDIT: hank you everyone for the help! I will leave this ehre, and if anyones nterested in ow I fixed it just converted the selected value to a string then used that which saved a lot of hastle. Cheers!
You need to do two things
Add AutoPostBack="True" in tag to perform postback which is false by default.
Bind code in !Page.IsPostBack, so that your dropdown do not bind again and loose the selected option.
HTML
<asp:DropDownList ID="sizeDropdown" runat="server" AutoPostBack="True" OnSelectedIndexChanged="sizeDropdown_SelectedIndexChanged"></asp:DropDownList>
Code bhind
if( !Page.IsPostBack)
{
sizeDropdown.Items.Add("50g");
sizeDropdown.Items.Add("100g");
}
Use this in sizeDropdown Tag
AutoPostBack="True"
SelectedItem.Text in all conditions
if (sizeDropdown.SelectedItem.Text.Equals("50g"))
{
amount = 4.99;
}
if you adding the items in page_load event otherwise it is not needed
if(!Page.IsPostBack)
{
sizeDropdown.Items.Add("50g");
sizeDropdown.Items.Add("100g");
}
Aspx Code:
<asp:DropDownList ID="sizeDropdown" runat="server" AutoPostBack="True"
onselectedindexchanged="sizeDropdown_SelectedIndexChanged">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem Value="4.99">50gms</asp:ListItem>
<asp:ListItem Value="7.99">100g</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="priceLabel" runat="server" Text="Label"></asp:Label>
.Cs Code
protected void sizeDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
if (sizeDropdown.SelectedItem.Text != "Select")
{
priceLabel.Text = sizeDropdown.SelectedItem.Value;
}
else
{
priceLabel.Text = "";
}
}
Quick Solution
Please replace your code with following code.
aspx page :
<asp:DropDownList ID="sizeDropdown" runat="server" OnSelectedIndexChanged="sizeDropdown_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
code page:
if(!Page.IsPostBack)
{
sizeDropdown.Items.Add("50g");
sizeDropdown.Items.Add("100g");
}
hey try this i have same issue solve by this....
In desgin add AutoPostBack="True" for dropdownlist.
<asp:DropDownList ID="sizeDropdown" AutoPostBack="True" runat="server" OnSelectedIndexChanged="sizeDropdown_SelectedIndexChanged">
In code behind.....
protected void sizeDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
double amount = 0;
if (sizeDropdown.SelectedItem.Equals("50g"))
{
amount = 4.99;
}
else if (sizeDropdown.SelectedItem.Equals("100g"))
{
amount = 7.99;
}
else if (sizeDropdown.SelectedItem.Equals("150g"))
{
amount = 9.99;
}
priceLabel.Text = amount.ToString();
}
Hope this may be useful to you.
Related
I have requirement where users are redirected youtube.com by using hyperlink control using below
I want to change the URL dynamically based on drop down list selected item by using below code.
protected void ddlPType_SelectedIndexChanged(object sender, EventArgs e)
{
int x = ddlPType.SelectedIndex;
if (x == 0)
{
activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ";
activateCerts.Text = "activateCerts";
activateCerts.Target = "_blank";
//activateCerts.HRef = "http://www.youtube.com/watch?v=3AYoipyqOkQ";
}
else if (x == 1)
{
//activateCerts.Target = "_blank";
//activateCerts.HRef = "http://www.youtube.com/watch?v=hk3hxUuwg0w";
activateCerts.Text = "activateCerts";
activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ";
}
and this is the one aspx code
<asp:Label runat="server" style="padding-left:23rem;" Text="pls watch this video on How to"></asp:Label>
<asp:HyperLink ID="activateCerts" runat="server"></asp:HyperLink>
but when I click on link I am not able to open a youtube video
This is working for me by setting AutoPostBack=true for dropdpwn ddlPType :
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="ddlPType" AutoPostBack="true" OnSelectedIndexChanged="ddlPType_SelectedIndexChanged">
<asp:ListItem Text="Option 1" Selected="True" />
<asp:ListItem Text="Option 2" />
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" style="padding-left:23rem;" Text="pls watch this video on How to"></asp:Label>
<asp:HyperLink ID="activateCerts" runat="server"></asp:HyperLink>
</div>
</form>
.cs Page :
protected void ddlPType_SelectedIndexChanged(object sender, EventArgs e)
{
int x = ddlPType.SelectedIndex;
if (x == 0)
{
activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ";
activateCerts.Text = "activateCerts";
activateCerts.Target = "_blank";
//activateCerts.HRef = "http://www.youtube.com/watch?v=3AYoipyqOkQ";
}
else if (x == 1)
{
//activateCerts.Target = "_blank";
//activateCerts.HRef = "http://www.youtube.com/watch?v=hk3hxUuwg0w";
activateCerts.Text = "activateCerts";
activateCerts.NavigateUrl = "http://www.youtube.com/watch?v=3AYoipyqOkQ";
}
}
For your Dropdownlist named ddlPType, you need to make sure its AutoPostBack is true. You can set it in the Attribute Panel, or using the code:
<asp:DropDownList runat="server" ID="ddlPType" AutoPostBack="true" OnSelectedIndexChanged="ddlPType_SelectedIndexChanged">
By this step you should achieve your goal, but sometimes this is not that simple. You may need to make sure you put your data-bind (if there is) in if (!Page.IsPostBack) in Page_Load.
Also, Dropdownlist will only send data when the data is changed. That is to say, if you get two options sharing the same value, Dropdownlist may not respond you. For example:
if(!IsPostBack)
{
for(int i=0;i<10;i++)this.DropDownList1.Items.Add(new ListItem(i.ToString(),"same_value"));
}
Here comes the most strange situation: you have done all above but it still does not work. Sometimes it happens in IE8. If you use window.showModalDialog() to show DropDownList, submitting will leads you to a new page. You need to add between head tag:
<base target=_self></base>
Hope my experience will do help.
I have a web form with two drop down lists.
The 1st drop down list contains 2 values &
the 2nd drop down list contains 3 values.
My objective is, when I select the 1st value of the 1st drop down list, the 2nd dropdownlist should not be visible, but when i select the 2nd value of the 1st dropdownlist, the 2nd dropdownlist should appear.
You need to set the AutoPostBack-property of your first drop down list to true and add an OnSelectedIndexChanged-EventHandler
<asp:DropDown id="FirstList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="FirstList_Changed"></asp:DropDown>
In your EventHandler you can check the selected index and act accordingly.
protected void FirstList_Changed(object sender, EventArgs e) {
if(FirstList.SelectedIndex == 0) {
SecondList.Visible = false;
} else {
SecondList.Visible = true;
}
}
However you can also do the same thing with JavaScript (see BPX's solution).
If you want to enable and disable you can use this Code
<asp:DropDownList ID="ddl1" runat="server" AppendDataBoundItems="true" AutoPostBack="true" Width="100%">
<asp:ListItem Text="" ></asp:ListItem>
<asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
</asp:DropDownList>
You need to set AutoPostBack="true" and in the SelectedIndexChanged write this code
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (ddl1.SelectedValue == "1")
{
ddl2.Enabled = false;
//ddl2.Visible = false;
}
else
{
ddl2.Enabled = true;
//ddl2.Visible = true;
}
}
catch (Exception ex)
{
string b= ex.Message;
}
}
For VISIBLE *enabling/disabling* USE the commented line and comment the other line
Hope this helps
Happy coding
You can do it by using JQuery. A sample code is given below.
$("#DropDownList1").change(function(){
indx = $("#DropDownList1 option:selected").index();
if(indx==1)
{
$("#DropDownList2").hide();
}
else
{
$("#DropDownList2").show();
}
})
Don't forget to add jquery plugin.
I have an aspx site with a DropDownList in it. I want to be able to choose between 3 variables there and then keep the chosen value for the postback. The page is made so that it loads 10 entries from the database and with this DropDownList I want to be able to choose between 10, 20, 30 entries.
DropDownList
<asp:DropDownList ID="dd1" runat="server" AutoPostBack="True" EnableViewState="True">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
</asp:DropDownList>
Here I am trying to set the value that is being sent to the database for the query that brings out the 10, 20 or 30 first entries.
public IEnumerable<XX> repOrder_GetData([ViewState]DateTime? UpdatedRows)
{
var ordrar = _facade.OrderGetForAttest(1, Convert.ToInt32(dd1.SelectedValue));
return ordrar;
}
How do I retain this value during a postback because the page will reload every time you choose something in the DropDownList resulting in only the first value ever being chosen.
protected void Page_Init(object sender, EventArgs e)
{
try
{
_masterpage = this.Master as XX.resource.masterpage.Site;
}
catch (Exception)
{
throw;
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Kolla behörighet första gången.
KollaBehorighet();
_masterpage.ClearMessage();
if (Page.IsPostBack)
{
}
else
{
Page.DataBind();
// Första gången..
PageInit();
FillPage(null);
//FIXME: xxx.Focus();
}
}
You should set the 'OnSelectedIndexChanged' event.
<asp:DropDownList ID="dd1" runat="server" AutoPostBack="True" onselectedindexchanged="ddlItemSelected" EnableViewState="True">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
</asp:DropDownList>
protected void ddlItemSelected(object sender, EventArgs e)
{
//Add your selected value to viewstate or session or whatever. Then check this value when binding on postback.
Viewstate["myValue"] = dd1.SelectedValue;
}
You can try to store it in Session state and then load it from session
Some like this:
Session["Selected"] = dd1.SelectedIndex;
And in Load Event you can use:
dd1.SelectedIndex = Convert.ToInt32(Session["Selected"]);
I have a status drop down list which I am using within a grid view, the users can select a list item they want when editing a row within the grid view. The problem is that the current implementation is not retrieving the selected value, but is only retrieving the default/loaded value.
This is the defination of the grid view:
<asp:GridView ID="applicationGrid" runat="server" Width="95%"
AutoGenerateEditButton="True"
AutoGenerateColumns="False"
ShowFooter="True"
CellSpacing="10"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left"
OnRowUpdating="applicationGrid_RowUpdating"
OnRowEditing="applicationGrid_RowEditing"
OnRowCancelingEdit="applicationGrid_RowCancelingEdit"
AutoPostBack="true" >
And this is the defination of the Column where the dropdown list will appear on edit:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="StatusDescription" runat="server" Text='<%# Eval("STATUS_DESCRIPTION") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
This is the code behind which is handling the update scenario:
protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = applicationGrid.Rows[e.RowIndex];
applicationGrid.EditIndex = -1;
Label applicationCodeLabel = row.FindControl("AppID") as Label;
TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
string applicationName = applicationNameTextBox.Text;
string applicationURL = applicationURLTextBox.Text;
int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
//string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
//string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
application.UpdateApplication(applicationCode, applicationName, applicationURL);
PopulateApplications();
}
All is working, but the selected value is not the one which the is loaded and not the one which the user selects. Therefore the problem is getting the selected value from the list. What needs to be changed, and why?
protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = applicationGrid.Rows[e.RowIndex];
applicationGrid.EditIndex = -1;
Label applicationCodeLabel = row.FindControl("AppID") as Label;
TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
string applicationName = applicationNameTextBox.Text;
string applicationURL = applicationURLTextBox.Text;
int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
//string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
//string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
application.UpdateApplication(applicationCode, applicationName, applicationURL);
PopulateApplications();
}
EDIT: Adding my Populate Methods:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
PopulateApplications();
}
catch (Exception exception)
{
throw exception;
}
}
}
private void PopulateApplications()
{
DataTable reader = application.GetApplicationList();
applicationGrid.DataSource = reader;
applicationGrid.DataBind();
applicationGrid.AllowSorting = true;
}
protected void DropDownLoadEdit(object sender, EventArgs e)
{
DataTable statusTable = application.GetStatusList();
DropDownList dropdown = sender as DropDownList;
dropdown.DataSource = statusTable;
dropdown.DataTextField = "status_description";
dropdown.DataValueField = "application_status_code";
dropdown.DataBind()
}
Update #2: I am trying to fill up a static variable in the class which is for the selected index. This will then be used when update is pressed. However, this is still getting the original value of the drop down list and not the selected one.
This is the method:
protected void StatusDescriptionList_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
selectedValue = Convert.ToInt32(ddl.SelectedValue.ToString());
}
Are you repopulating the list in DropDownLoadEdit, regardless of whether the transition is a postback or not? If so, the list will be repopulated before its value is read, and set to the default before your method has a chance to read the value.
The issue may be of the post back.
Once the item is selected from the drop down the page get post back the value you selected get vanished.
To over come this issue. Bind the drop down list inside page is post back or else make use of a Ajax update panel.
#Ryan, Have you done this
'
AutopostBack="True"
<asp:DropDownList ID="StatusDescriptionList" runat="server" AutopostBack="True" DataTextField="status_description"
DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
DataValueField="application_status_code" SelectedValue="application_status_code" AutopostBack="True" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
I have a dropdown list and some textboxes (5). I would like to pass each selected item to respectively textbox1, textbox2... and so on.
How can I achieve this in either C# or jquery?
Thanks
Use SelectedIndexChanged.
Something like this:
Code Front:
<asp:DropDownList runat="server" id="MyDropDown" AutoPostBack="True" OnSelectedIndexChanged="MyDropDown_SelectedIndexChanged">
...
</asp:DropDownList>
Code Behind:
void MyDropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
var selectedValue = ((DropDownList)sender).SelectedValue;
if (!string.IsNullOrEmpty(textbox1.Text))
textbox1.Text = selectedValue;
else if (!string.IsNullOrEmpty(textbox2.Text))
textbox2.Text = selectedValue;
...
}
i = 0
function setTextInTextField()
{
document.getElementById('textfield' + i).value = document.getElementById('dropdownlist').value;
i += 1;
}
You can use jQuery to do so:
<asp:DropDownList id="MyDropDown" runat="server" ClientIDMode="Static"/>
<asp:TextBox id="MyTextBox" runat="server" ClientIDMode="Static"/>
$(document).ready(function(){
$('select#"MyDropDown").change(function(){
$('input#"MyTextBox").val($(this).val());
});
});
Please take a look at the following url:
http://api.jquery.com/val/
Hope this helps you out!