There is an issue I have not been able to solve which is where there is an 'Add Item' button on a form. When clicked this opens a Modal window which contains two dropdown lists. The first dropdown list is prepopulated with a set of items and based on this selection, the 2nd dropdown box ideally needs to update with its own set of items.
This is the first Dropdown List which contains the Type
<asp:DropDownList ID="DDLType" CssClass="form-control" runat="server" OnSelectedIndexChanged="LoadSubTypes" AutoPostBack="true">
<asp:ListItem Value="Automobile">Automobile</asp:ListItem>
<asp:ListItem Value="Aeroplane">Aeroplane</asp:ListItem>
<asp:ListItem Value="Boat">Boat</asp:ListItem>
</asp:DropDownList>
This is the second Dropdown List which currently sits empty until a selection is made from the first dropdown list.
<asp:DropDownList ID="DDLSubType" CssClass="form-control" runat="server">
</asp:DropDownList>
With the first DDL where there is an OnSelectedIndexChange, this is the code behind.
protected void LoadSubTypes(object sender, EventArgs e) {
string strSubTypeList = "";
switch (DDLType.SelectedValue)
{
case "Automobile":
strSubTypeList = "Car#Motorbike#Scooter";
break;
case "Aeroplane":
strSubTypeList = "Commercial#Private";
break;
case "Boat":
strSubTypeList = "Boat#Jetski";
break;
}
StringBuilder sbSubTypes = new StringBuilder();
Char delimiter = '#';
String[] substrings = strSubTypeList.Split(delimiter);
foreach (var substring in substrings)
{
DDLSubType.DataTextField = substring;
DDLSubType.DataValueField = substring;
DDLSubType.DataBind();
} }
Normally this type of setup works well for me, but the only difference this time is I have them loaded into a Modal. What happens when I make a selection from the DDL is that the modal closes and causes the postback. This resets any values that were then changed dynamically.
My ideal behavior here is that when a selection is made on the first DDL, the 2nd DDL updates within the modal and do not close.
Any help would be greatly appreciated!
I think you are saying that when I select an item from a dropdown list, then its relevent options will be shown on the next dropdown. You will have to use jquery ajax(it will be more easy).
When an item is selected from a dropdown, its value will be passed using ajax, which will without refreshing your page get your dropdown.
e.g.
<select class="form-control" id="Departments" ><option value="">-Select Department-</option>
<option value="17">No Department</option>
<option value="19">Men</option>
<option value="1018">Danial</option>
</select>
JavaScript with jquery:
$("#Departments").change(function () {
var deptId = $(this).select("option:selected").val();
$.ajax(
{
url: "/Categories/M_Level2/" + deptId
}).done(function (categories)
{
$("#category").html(categories);
});
In url, Categories is the controller and M_Level2 is the action that is getting a value deptId and on that basis returning a PartialView and then adding it on #category
Related
I have used a searchable drop-down list on my aspx page. it's working fine. but my requirement is that whenever the values are selected from the drop-down list at that time, the file upload control will be Enabled. By default file upload control is disabled.
Under the dropdown list, the OnSelectedIndexChanged method is used. and they're used this code fileUpload.Enabled = true;. but it's not working. even whenever I have selected the values from the dropdown list OnSelectedIndexChanged event is not happening.
I want to do it. whenever the data is selected from the dropdown list that time file upload control must be enabled otherwise it will be disabled.
Please help me with this - I am showing my code here:
This is FileLevelMerge.aspx:
<asp:TableCell HorizontalAlign="Left">
<asp:DropDownList CssClass="DropDown" ID="ddlFlights" runat="server" Width="165px"
DataTextField="Flight_Number" DataValueField="File_Level_Id" OnSelectedIndexChanged="ddlFlights_OnSelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<%----%>
</asp:TableCell>
--jquery code for serachble dropdownlist
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
$("#<%= ddlFlights.ClientID %>").ddlFlighNo();
});
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
$("#<%= ddlFlights.ClientID %>").ddlFlighNo();
}
FileLevelMerge.aspx.cs for binddropdownlist
public void BindFlight()
{
DataSet ds = new DataSet();
ds = objExpFileLevelMerge.GetFlightDetail(Convert.ToInt32(Session["Branch_ID"]));
ddlFlights.DataSource = ds;
ddlFlights.DataBind();
DropDownList ddl = ddlFlights;
if (ddl != null)
{
foreach (ListItem item in ddl.Items)
{
item.Attributes["title"] = item.Text;
}
}
}
On select event
protected void ddlFlights_OnSelectedIndexChanged(object sender, System.EventArgs e)
{
fileUpload.Enabled = true;
BtnMerge.Enabled = true;
}
I'm currently running into a head-scratching bug in my ASP.NET Windows Form App. I have two DropDownList, implemented them in the same way, yet they behave differently.
The problem:
When selecting an item in the DropDownList "GroepSelect", the page refreshes (as intended), but resets it's SelectedIndex to the first item.
When selecting an item in the DropDownList "VakSelect", the page refreshes, but also remembers it's SelectedIndex value.
It's doing this behavior consistently, yet I am unable to discover what I do wrong.
My Code:
In my HTML code, I have two DropDownList Controls.
<div>
<asp:DropDownList runat="server" ID="GroepSelect" AutoPostBack="true" AppendDataBoundItems="true" />
<asp:DropDownList runat="server" ID="VakSelect" AutoPostBack="true" AppendDataBoundItems="true" />
</div>
I'm populating the controls in my C# code:
protected void Page_Load(object sender, EventArgs e) {
Database db = new Database();
if (!IsPostBack) {
GroepSelect.DataSource = GenereerDummyGroepen(); // returns a List<ListItem>
GroepSelect.DataTextField = "Text";
GroepSelect.DataValueField = "Value";
GroepSelect.DataBind();
GroepSelect.SelectedValue = "1";
VakSelect.DataSource = db.GetVakken(); // returns a List<Vak>
VakSelect.DataTextField = "Omschrijving";
VakSelect.DataValueField = "Id";
VakSelect.DataBind();
VakSelect.SelectedValue = "1";
}
// Use the SelectedValue to determine which data to get out of the database
Medewerkers = db.GetMedewerkers(int.Parse(GroepSelect.SelectedValue));
Opdracht = db.GetOpdrachten(int.Parse(VakSelect.SelectedValue)).First();
Resultaten = db.GetResultaten(Opdracht.Id, int.Parse(GroepSelect.SelectedValue));
GenereerTabel();
}
As requested, my code for GenereerDummyGroepen() is the following:
private List<ListItem> GenereerDummyGroepen() {
return new List<ListItem>()
{
new ListItem("Groep 1", "1"),
new ListItem("Groep 2", "1")
};
}
Why I implemented it this way?
I try to populate a custom-made pivot table based on the content of Medewerkers, Opdracht and Resultaten. The content of those lists, depends on the selected item in the DropDownList control. The expected behavior of those controls is, that on the moment those are changed, the table should re-populate. The strategy I follow here, is that a page-postback is being processed, and using the AppendDataBoundItems=true remembers the DropDownList contents so that on the newly refreshed page I can generate the table.
My Question
I'm looking for the answer for: why is there a consistent different behavior? Is it the fact that the ListItem class differs in behavior from my custom class Vak?
Here I guess issue is with you function
GenereerDummyGroepen();
Please put your code here. In your code there is value field might have same data for all listItem. Because of that it is changing default to firstIndex as all values are same.
I have added a user control dynamically which contains dropdowns'. Loading User control is iterated 'I' times according to the condition. Now, I need to access the dropdowns' selected item. I have created an accessor function to get the dropdown selected values, but it is giving me the default value, which is "00" of the drop-down.
If someone needs additional Information to solve my problem,
kindly ask me.
Adding user control dynamically:
for (int i = 0; i < 3; i++)
{
//DailyControl is a userControl
DailyControl w1 = (DailyControl)LoadControl("~/DailyControl.ascx");
//InputPanel is a panel where user control is added.
InputPanel.Controls.Add(w1);
}
DailyControl.ascx
Select the Time:
<asp:DropDownList ID="clock" runat="server">
<asp:ListItem Text="am" />
<asp:ListItem Text="pm" />
</asp:DropDownList>
DailyControl.ascx.cs // accessor function to get the dropdown
public string Clock
{
get
{
return clock.SelectedItem.Text.ToString();
}
}
Instead of add controls dinamically, you can use a Repeater on aspx page.
In the Repeater you can bind a DropDownList (or a control containing the DropDownList) and get
myDropDownList.SelectedValue
(or SelectedItem)
I am using a code behind function to bind my dropdownlist dynamically, when a user changes the dropdownlist and submit a purchase, the selectedvalue is always empty.
I have tried both ddl.SelectedItem.ToString(); and ddl.SelectedValue.ToString(); but none work. Also for these 2 code behind functions below, I can't seem to use void methods instead of a function that needs a returning value and a parameter, is there anyway to use void methods without parameters? Any advice is appreciated.
Thanks.
<%# FormattedSize((string)Eval("Size")) %>
<%# FormattedGetSize((string)Eval("Size")) %>
inline:
<asp:DropDownList ID="DropDownList1" runat="server" OnDataBinding='<%# FormattedSize((string)Eval("Size")) %>'></asp:DropDownList>
<a href='AddToCart.aspx?CategoryId=<%# Eval("CategoryId") %>&&ProductId=<%# Eval("ProductId" ) %>&&Size=<%# FormattedGetSize((string)Eval("Size")) %>' style="border: 0 none white;">
Code Behind:
protected string FormattedSize(string size)
{
if (size.Contains("s"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("S");
}
if (size.Contains("m"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("M");
}
if (size.Contains("f"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("Freesize");
}
return null;
}
protected string FormattedGetSize(String Size)
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
string selectedSize = ddl.SelectedItem.ToString();
return selectedSize;
}
The reason why it doesn't work is because.. "you're doing it wrong". You're expecting that the <a href=.. will change based on the user interaction but instead it is already generated when user receives the page. If you want the link to change based on the dropdown, you'd have to have either:
a postback on dropdown selection, then the link would change...
you could change the href with some javascript by attaching event on dropdown selection
What you do in your Page_Load method? Do you check to see if the current request is a post back or not (using IsPostBack)?
If so, check for IsPostBack and bind your DropDownList to the underlying data source only on Get requests.
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow di in GridView1.Rows)
{
RadioButton rad = (RadioButton)di.FindControl("RadioButton1");
if (rad.Checked&&rad!=null)
{
s = di.Cells[1].Text;
}
}
Response.Redirect("applicants.aspx?form=" +s);
}
I'm selecting the rows that are selected with this but I have a problem here I want user to be able to select only one radiobutton but its allowing all the radiobuttons to be selected at once.Can you help me in removing this problem please.
please.
Maybe I'm too late here on the party but this will do the trick,
check out here......
This might be useful for someone watching this answer in the future.
In the ASP page you need to set the GroupName property to be the same for all the radio buttons, e.g.:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="RadioGroup" />
well for that you can use follwing code
first of all you should define the groupName.The follwing code will work
<asp:RadioButton ID="RadioButton1" OnCheckedChanged="rbSelector_CheckedChanged" AutoPostBack="true" GroupName="Apply" runat="server"></asp:RadioButton>
C#
protected void rbSelector_CheckedChanged(object sender, System.EventArgs e)
{
foreach (GridViewRow oldrow in GridView2.Rows)
{
((RadioButton)oldrow.FindControl("RadioButton1")).Checked = false;
}
//Set the new selected row
RadioButton rb = (RadioButton)sender;
GridViewRow row = (GridViewRow)rb.NamingContainer;
((RadioButton)row.FindControl("RadioButton1")).Checked = true;
}
You can try this link to select single radiobutton in grid : http://www.c-sharpcorner.com/uploadfile/krishnasarala/select-single-radio-button-in-gridview-in-Asp-Net/
Using the GroupName property by itself won't work, each radio button will still get a unique name attribute since they're in different rows of the grid.
One option is to emit the radio button markup manually using a Literal control (example). This will make it easy to group the radio buttons on the client-side, but requires a bit more work to determine which button was selected on postback.
When I needed this behavior, I found it easier to keep the radio buttons as server-side controls, and just enforce the button group w/ jQuery. Put your RadioButton in a TemplateField as you've shown, then add this code to uncheck all the other buttons when one is checked:
$(document).ready(function () {
// could also pass in a unique ID selector
createManualRadioButtonGroupForGridView(".myGridViewClass");
});
function createManualRadioButtonGroupForGridView(gridViewSelector) {
$(gridViewSelector + " input[type=radio]").change(function () {
var checkedRadioButton = this;
$(gridViewSelector + " input[type=radio]").each(function (e) {
if (this !== checkedRadioButton) {
$(this).prop("checked", false);
}
});
});
}