I have a drop down list and the values I get them from SQL.
There are 4 choices. I need on one of the choices to turn a textbox.visible = false;
I am not sure that is correct. I have it in SQL as Cancel_Reason
protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
string Item = ddlCancelReason.SelectedValue;
if (Item == "Non-Payment")
{
tbReturn.Visible = false;
}
}
Did you bind a SelectedIndexChanged event to your DropDownList? If you did:
In your case it won't work because you didn't enable the AutoPostBack-property of the DropDownList.
Change your DropDownList code from:
<asp:dropdownlist id="ddlCancelReason" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
To:
<asp:dropdownlist id="ddlCancelReason" AutoPostback="true" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
Just add AutoPostback="true".
Then this will work:
protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
string Item = ddlCancelReason.SelectedValue;
if (Item == "Non-Payment")
{
tbReturn.Visible = false;
}
}
Related
I have a CheckBoxList nested in a Repeater that is dynamically populated in the Repeater's OnItemDataBound. On a button click I would like to save the changes to each check box list, but the selected property is always TRUE after postback.
HTML:
<asp:Repeater id="rptFields" runat="server" OnItemDataBound="rptField_OnItemDataBound">
<ItemTemplate>
<asp:HiddenField id="hFieldID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ID")%>' />
<asp:Label id="lblDesc" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Description")%>' />
<asp:CheckBoxList id="chkOptions" runat="server" ></asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
<asp:Button id="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
rptFields.DataSource = Fields;
rptFields.DataBind();
}
}
protected void rptField_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
|| (e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBoxList chkOptions = (CheckBoxList)e.Items.FindControl("chkOptions");
Field ThisField = (Field)e.Item.DataItem;
for (int i = 0; i < ThisField.Options.Length; i++)
{
ListItem liOption = new ListItem (ThisField.Options [i].Description, ThisField.Options [i].ID.ToString ());
liOption.Selected = ThisField.Options [i].Selected;
chkOptions.Items.Add (liOption);
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rptItem in rptFields)
{
int iFieldID = int.parse(rptItem.FindControl("hFieldID"));
Field ThisField = GetFieldByID(iFieldID);
CheckBoxList chkOptions = (CheckBoxList)rptItem.FindControl("chkOptions");
foreach(Option ThisOption in ThisField.Options)
{
for(int i = 0;i < chkOptions.Items.Count; i++)
{
int OptionID = int.parse(chkOptions.Items[i].Value);
if(ThisOption.ID == OptionID)
{
//HERE IS THE PROBLEM
//SELECTED IS ALWAYS TRUE
ThisOption.Selected = chkOptions.Items[i].Selected;
ThisOption.Save();
}
}
}
}
}
All the data from the repeater persists, including the text and value fields of the check box list items. All except the selected property.
I have searched and searched for an answer and none exist. Every similar issue was people re-binding the list in each postback. As you can see that is not the case here. And for what it's worth, the exact same solution works for radio button lists no problem.
EDIT: For the record this project is running Mono.net 4.5
UPDATE: The same solution works in another project we have built using .Net 2.0. I'm not sure if it's a Mono issue or there is a configuration setting that changes the behaviour of the post back.
I had a similar problem recently, to solve it you can store it in querystring.
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.
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 datalist contains checkboxlist.
<asp:DataList ID="dtlstfilter" runat="server">
<ItemTemplate>
<asp:CheckBoxList ForeColor="Gray" AutoPostBack="true" OnSelectedIndexChanged="chklist_SelectedIndexChanged" ID="chklist"
runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
can i get the rownumber of datalist on the SelectedIndexChanged event of the checkbox ie;if i have checkbox list control repeated 4times and if i check on the second one how can i get the value 2?
You should try fetch parent of checkboxlist like this
Protected void dtlstfilter_SelectedIndexChanged(object sender, EventArgs e)
{
var control= ((Control)sender).Parent;
if(control is DataListItem)
{
int index=((DataListItem)control).ItemIndex;
}
}
if you not get DataListItem as parent then try parent of parent.
U can Try Following. I added +1 as it is 0 based index..
Protected void dtlstfilter_SelectedIndexChanged(object sender, EventArgs e)
{
lblSelectedIndex.Text = (dtlstfilter.SelectedIndex + 1).ToString();
}
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>