C# code:
protected void DropDownListDB_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListDB.SelectedValue == "Other")
{
LabelIfOtherDb.Visible = true;
TextBoxIfOtherDb.Visible = true;
}
}
ASP code:
<asp:DropDownList AutoPostBack="True" ID="DropDownListDB" runat="server" Height="20px"
Width="158px">
<asp:ListItem>- Select -</asp:ListItem>
<asp:ListItem>Oracle</asp:ListItem>
<asp:ListItem>MS SQL Server</asp:ListItem>
<asp:ListItem>MySQL</asp:ListItem>
<asp:ListItem>MS Access</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
I have AutoPostBack="True" but still it doesn't show my hidden textbox/label.. Any suggestions?
It seems, your Event is not connected to the event-handler.
Two possibilities: either, define the event-handler in the Markup, like:
<asp:DropDownList AutoPostBack="True" ID="DropDownListDB" runat="server" Height="20px" SelectedIndexChanged="DropDownListDB_SelectedIndexChanged">
or in the code-behind
DropDownListDB.SelectedIndexChanged += DrowpDownListDB_SelectedIndexChanged;
You should put this up in mark-up though.
This works have the very same question for a college assignment
<asp:DropDownList ID="DropDownList1" AutoPostBack="true"
runat="server" Height="16px" Width="237px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"/>
Related
When the selection of the dropdown is changed by the user, the corresponding textbox will show depends on user's selection. Let's say when user select "A" from the dropdown, the Textbox "A" will shown, while the other textbox will be invisible.
The issue is when user select "A" in the drop down, the other two text box won't disappear.
aspx
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:DropDownList>
A <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
B <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
C <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
aspx.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "A")
{
TextBox2.Visible = false;
TextBox3.Visible = false;
}
}
Make all textbox properties set to visible false and then try in dropdown selection chagned event.
<asp:TextBox ID="TextBox1" runat="server" visible="false"></asp:TextBox>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "A")
{
TextBox2.Visible = false;
}
else
{
TextBox2.Visible = true;
}
}
It's not working because you should refresh your page. You should add AutoPostBack="true" property to your DropDownList.
Also, you can use UpdatePanel to don't need to refresh all your page.
In aspx use this:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
I hope it helps you
If your goal is just to toggle visibility of the textboxes based on drop down selection, you should write a JavaScript function on your page which will take index as parameter. Then it will change the visibility of the indexed text box to true and false for the other text boxes.
Something like this:
Dropdown event:
on-click=“return toggleText(selectedIndex);”
<script>
function toggleText(index) {
// show or hide textbox by setting it’s style display:block or display:none respectively
return false; // ensures the page is not posted-back to server
}
</script>
i have a DropDownList in a GridView and want to bind the selected value of DropDown in TextBox inside a GridView to pass the selected value in query string in asp.net c#
please help me to guide and code of SelectedIndexChanged of DropDownList
screenshot of GridView
You could attach a method to the SelectedIndexChanged event on each child dropdown, either in the markup, either using a AddHandler in the RowDataBound event of your gridview
<asp:GridView ID="gv1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSelectedIndexChanged">
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="txt1" runat="server" />
<asp:Button ID="btn1" runat="server" Text="redirect" />
Save the value wherever you want (hidden or textbox) when the selected item is changed
protected void ddlSelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
txt1.Text = ddl.SelectedValue;
}
And use this value when clicking the button
private void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("mypage.aspx?q=" + txt1.Text);
}
or use jquery onChange event atttached to the dropdowns ?
$('select').on('change', function() {
alert( this.value );
})
I have dropdownList in asp.net webforms:
<asp:DropDownList runat="server" ID="ItemDate" DataSourceID="dsItems" DataTextField="Name" DataValueField="Value" AutoPostBack="true" EnableViewState="true" >
</asp:DropDownList>
But url is still this same:
http://.../Default.aspx
I want to change this method that url should have additional parameter:
http://.../Default.aspx?item=23
you can use the SelectedIndexChanged event here bind you DropDown in aspx as follows..
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
then in C# code behind use the following snippet..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect(Request.Url.AbsolutePath+"?item=" + DropDownList1.SelectedValue);
}
I am trying to access the values in the drop down list from the code behind but I am getting this error: “ddl_Ext does not exist in the current context”. I am not sure what am I doing wrong here.. can someone please help? Here is my drop-down list in aspx file. Thanks.
<asp:TemplateField HeaderText="Is this external?">
<ItemTemplate>
<asp:Label ID="lblExt" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Ext") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_Ext" runat="server"
AutoPostBack="false" AppendDataBoundItems="true">
<asp:ListItem Text="Please select ..." Value="Please Select ..."></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
here is the code behind i am trying to use:
protected void DV_WScript_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string Ext = ddl_Ext.SelectedValue;
}
Try this:
protected void DV_WScript_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//Put here if you want to find control of your Insert Mode
DropDownList dropDown = (DropDownList)DetailsView1.FindControl("ddl_Ext");
string Ext = dropDown.selectedValue;
}
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;
...
}