I have a problem with the SelectedItem in the DropDownList
<asp:DropDownList ID="Etkin_Drop" runat="server" OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged">
<asp:ListItem Text="Seç" Value="-1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Aktif" Value="1"></asp:ListItem>
<asp:ListItem Text="Deaktif" Value="0"></asp:ListItem>
</asp:DropDownList>
First list item value is -1 but when I want to check in the if statement its not working
protected void Etkin_Drop_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(Etkin_Drop.SelectedItem.Value) == -1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Lütfen Bir Seçim Yapınız');", true);
}
else
{
Label4.Text = Etkin_Drop.SelectedItem.Value;
}
}
I could not define the problem
Add AutoPostBack Property to your DropDownList and set this property to True. Like this:
<asp:DropDownList ID="Etkin_Drop" runat="server"
OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged" AutoPostBack="True">
Related
I want to when the item x5 is selected on dropdownlist1 the item y0 is automatically selected on dropdownlist2
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_Itemchanged">
<asp:ListItem Value='5'>x5</asp:ListItem>
<asp:ListItem Value="4">x4</asp:ListItem>
<asp:ListItem Value="3">x3</asp:ListItem>
<asp:ListItem Value="2">x2</asp:ListItem>
<asp:ListItem Value="1">x1</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="0.75">y0.75</asp:ListItem>
<asp:ListItem Value="0.50">y0.50</asp:ListItem>
<asp:ListItem Value="0.25">y0.25</asp:ListItem>
<asp:ListItem Value="0">y0</asp:ListItem>
</asp:DropDownList>
protected void DropDownList1_Itemchanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Value == "5")
{
DropDownList2.Items.FindByValue("0").Selected = true;
DropDownList2.Items.FindByValue("0.75").Attributes.Add("Disabled", "Disabled");
DropDownList2.Items.FindByValue("0.50").Attributes.Add("Disabled", "Disabled");
DropDownList2.Items.FindByValue("0.25").Attributes.Add("Disabled", "Disabled");
}
}
When I select the item x4 on dropdownlist1 and select the item y0.25 on dropdownlist2, and after that when I select x5 on the dropdownlist1 it gives me "Cannot have multiple items selected in a DropDownList"
Use the SelectedValue property on the list:
protected void DropDownList1_Itemchanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Value == "5")
{
DropDownList2.SelectedValue = "0";
}
}
For example i have two radio button list:
<asp:RadioButtonList ID="rbl1" runat="server" OnSelectedIndexChanged="rbl1_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Value="1"> 1</asp:ListItem>
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="3"> 3</asp:ListItem>
<asp:ListItem Value="4"> 4</asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="rbl2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="rbl2_SelectedIndexChanged" >
<asp:ListItem Value="1"> 1</asp:ListItem>
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="3"> 3</asp:ListItem>
<asp:ListItem Value="4"> 4</asp:ListItem>
</asp:RadioButtonList>
protected void rbl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbl1.SelectedIndex == 1)
{
Label1.Text = "Correct Answer.";
}
else
{
Label1.Text = "Incorect";
}
}
protected void rbl2_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbl2.SelectedIndex == 0)
{
Label2.Text = "Correct Answer";
}
else
{
Label2.Text = "Incorect";
}
and I want to if rbl1 selectedindex =1 add 1 value to a label (like Lable3) to show also if rbl2 selectedindex = 0, again sum 1 with previous score in label3.
Please help me, Thanks in advance.
I have a radio button and I would like to default the first item as checked by default. How do I go about doing that? Below is my source code for the radio button.
<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="High">High</asp:ListItem>
<asp:ListItem Value="Mid">Mid</asp:ListItem>
<asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
ASPX Markup
<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="High" Selected="True">Select</asp:ListItem>
<asp:ListItem Value="High">High</asp:ListItem>
<asp:ListItem Value="Mid">Mid</asp:ListItem>
<asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
Selecting default value via ASPX markup:
<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="High" Selected="True">Select</asp:ListItem>
<asp:ListItem Value="Mid">Mid</asp:ListItem>
<asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
Selecting default value via C# codebehind:
if (!IsPostBack) // prevent from selecting default value during postback
{
if (radRiskLevel.SelectedIndex == -1) //-1 is the indication of none selected
{
radRiskLevel.SelectedIndex = 0; // the index of items in radiobutton in the list you want to select
//radRiskLevel.Items.FindByText("Select").Selected = true; //can also be selected by text, need to pass text name as parameter
//radRiskLevel.Items.FindByValue("High").Selected = true; //can also be selected by value, need to pass value name as parameter
}
}
Please help me if you have an idea on autopostback in asp.net using C#
i have 4 dropdown lists in one page
if i selects 1st dropdownlist then it enables 2nd dropdownlist
and after selection an option in 2nd dropdownlist 3rd dropdownlist enables
and after selection an option in 3rd drop down list 4th dropdown list enables
please let me know to do this task...
please help me
Try this:-
ASPX
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
ASPX CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Enabled = true;
DropDownList2.Enabled = false;
DropDownList3.Enabled = false;
DropDownList4.Enabled = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Enabled = true;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Enabled = true;
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList4.Enabled = true;
}
you can use SelectedIndexChanged event of drop down list to do the server side functionality and set AutoPostBack = true so that on selection change your page will get re-loaded.
Check out this Fiddle .
Important part is to handle the change event of dropdown list and to show another dropdownlist.
$("#one").change(function()
{
$("#two").toggleClass("show",true);
});
Hi I have a dropdownlist and id like upon selecting one of the four choices to set the imageurl of Image2 in code behind?
An example. In your markup:
< <asp:DropDownList ID="TestDropDownList" runat="server"
onselectedindexchanged="TestDropDownList_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="http://url.com/image1.png" Text="Option 1"></asp:ListItem>
<asp:ListItem Value="http://url.com/image2.png" Text="Option 2"></asp:ListItem>
<asp:ListItem Value="http://url.com/image3.png" Text="Option 3"></asp:ListItem>
<asp:ListItem Value="http://url.com/image4.png" Text="Option 4"></asp:ListItem>
</asp:DropDownList>
<asp:Image ID="TestImage" ImageUrl="" runat="server" />
In your code-behind:
protected void TestDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
Image i = this.TestImage;
i.ImageUrl = ((DropDownList)sender).SelectedValue;
}
You have to enable AutoPostBack property for the dropdownlist. Then each time a selection changes postback will be send to server, so you codebehind will be executed. If I remeber corectly DropDownList control has an event for changed selection.
Add a OnSelectedIndexChanged eventhandler, and set AutoPostBack to true:
<asp:DropDownList ID="Options" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="Options_SelectedIndexChanged">
<asp:ListItem Value="Item1">Text 1</asp:ListItem>
<asp:ListItem Value="Item2">Text 2</asp:ListItem>
</asp:DropDownList>
In the code behind you implement the method that handles the event:
protected void Options_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = this.Options.SelectedValue;
...
}