Hide button on drop down selection -1 - c#

I need my create button to be hidden unless a facility is selected in my dropdown. When it is at -1 message i need my button to be hidden.
Code for button
<asp:Button ID="btnCreate" runat="server" Text="Create New" Width="89px" Font-Size="X-Small" OnClick="btnCreate_Click" />
Drop down code
private void ResetForm()
{
try
{
//facility dropdown
ddlFacility2.Items.Clear();
ddlFacility2.DataSource = this.DataLayer.model.MS_spGetFacilityInfo(null).OrderBy(x => x.FacilityName);
ddlFacility2.DataTextField = "FacilityName";
ddlFacility2.DataValueField = "FacilityID";
ddlFacility2.DataBind();
ddlFacility2.Items.Insert(0, new ListItem("All Facility Records..", "-1"));
BindGrid();
}
catch (Exception ex)
{
this.SetMessage(ex.ToString(), PageMessageType.Error);
AISLogger.WriteException(ex);
}
}

in first time page load if the default value selected is -1 you can set your button visible false as default.
in your droupdown list selected index change event you can enable/dissable button based on droupdown list selected value.

Add a OnSelectedIndexChange event to your dropdown list or add a clientside event to your dropdownlist. Double Click on your ddl you will see a function named ddlFacility2_OnSelectedIndexChanged in you code behind and add the below code to it.
Add AutoPostBack=true to you ddl
protected void ddlFacility2_OnSelectedIndexChanged(object sender, EventArgs e)
{
if(ddlFacility2.SelectedIndex>-1)
{
btnCreate.Enabled = true;
}
else
{
btnCreate.Enabled = false;
}
}

You can wire up a JQuery script that can bind to your DropDownList's selected value...
In this example, the button's visibility is bound on a click from another button:
$('#Button1').bind("click", function() {
$("#Button2").hide();
});
I dont know the exact syntax to use for the binding to selected value, but the above code should be a good place to start.

Related

get value on edititemtemplate from codebehind

I have a detailsview where I get couple of data from membership profile and I display it on detailsview...this works fine:
<ItemTemplate>
<asp:label ID="FirstName" runat="server" />
</ItemTemplate>
But when I click the edit button, nothing shows up on the field. This is what I am doing on Edit template:
I call ItemUpdating like this:
protected void DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e)
{
//I get my memberprofle here
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
Label labelfName = DetailsView1.FindControl("FirstName") as Label;
labelfName.Text = memberp.fName;
}
Should I be using Itemupdated instead? Or is there another method that I should call when the edit button is clicked that will populate the firstname field on edit? Also, the reason I am keeping it as "LABEL"(usually it would be textbox) on edit mode is that this field has to be read only.
The event ItemUpdating is not fired when you enter on edit mode. You must use DataBound event to set properly required text value.
If neccesary, you can ask CurrentMode property of DetailsView to know if you are editing or displaying.
The result looks like this:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
Label l = DetailsView1.FindControl("FirstName") as Label;
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
//obtained from your sample
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
l.Text = memberp.fName;
}
else
{
l.Text = "Another text or nothing";
}
}
Be sure to define DataBound event in you Detailsview1 control.
REMARK: It can be affected depending the data bind mode. If so, let me know and put an example.
Add RowUpdating and RowEditing event to your gridview.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

Updating a databound dropdownlist control when another databound dropdownlist changes

I have two databound dropdown lists:
if (!IsPostBack)
{
ddlSelectProgram.DataSource = new PEIATableAdapters.programTableAdapter().GetData();
ddlSelectProgram.DataBind();
ddlSelectSurveyType.DataSource = new PEIATableAdapters.ParticpantSurveyFormIDsTableAdapter().GetFormIDsByProgramID(ProgramID);
ddlSelectSurveyType.DataBind();
BindData();
}
The call to BindData is correctly populating grdResults when the page loads:
private void BindData()
{
PEIATableAdapters.ParticipantSurveysTableAdapter adapter = new PEIATableAdapters.ParticipantSurveysTableAdapter();
grdResults.DataSource = adapter.GetDataByFormID(FormID);
grdResults.DataBind();
}
However, when the page is posted back and DataBind is called again by clicking a Submit button, the value selected in ddlSelectProgram is not being passed in. The click event for the Submit button is merely calling Databind() and the event is firing.
Here are the Properties for ProgramID and FormID:
protected int ProgramID
{
get
{
return Convert.ToInt32(ddlSelectProgram.SelectedValue);
}
set
{
ddlSelectProgram.SelectedValue = value.ToString();
}
}
protected int FormID
{
get
{
return Convert.ToInt32(ddlSelectSurveyType.SelectedValue);
}
set
{
ddlSelectSurveyType.SelectedValue = value.ToString();
}
}
How do I tie the two controls together so that when ddl_SelectProgram is changed then ddlSelectSurveyType is changed?
Add the property AutoPostBack="True" to ddlSelectProgram in the markup and add an event handler to the ddlSelectProgram's SelectedIndexChanged event in the code-behind. Bind ddlSelectSurveyType in that event.
When page loads populate ddlSelectSurveyType only. Then use autoPostBack and catch event when value of ddlSelectSurveyType is changed. Then after you know ProgramID was selected populate your data.

Dynamically Created DropDownList within a List of Panels is not allowing for Event Handling

I am trying to create a UI that creates rows that can be dynamically populated.
I am at the point where I can add a Panel to my list that contains a DropDownList and has an associated Remove Button.
The Remove Button has an OnClick event bound that allows me to remove that specific panel.
I am having an issue when trying to bind a SelectedIndexChanged EventHandler to my DropDownList it does not.
I suspect this had something to do with how I am recreating my controls after every Postback.
I am not asking for or expecting a straightforward "Add this or Change this in the code." I really want to have a understand where I am going wrong, sorry for asking so much! :s
protected void Page_Load(object sender, EventArgs e)
{
//Showing this for the the poster that asked.
if (!IsPostBack)
{
GetClustersFromDB(user);
BindGrid();
BindState();
}
if (Session["persistControls"] != null)
{
persistControls = (List<Panel>)Session["persistControls"];
int count = 0;
foreach (Panel dynamicControl in persistControls)
{
DropDownList list = new DropDownList();
list.ID = "list" + count;
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.AutoPostBack = true;
list.Items.Add(new ListItem("", "0"));
list.Items.Add(new ListItem("Title", "1"));
dynamicControl.ID = "panel" + count;
Button btnRemove = new Button();
btnRemove.Click += new EventHandler(btnDelete_Click);
btnRemove.Text = "Remove";
btnRemove.CommandArgument = count.ToString();
myPlaceholder.Controls.Add(dynamicControl);
myPlaceholder.Controls.Add(btnRemove);
count++;
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
DropDownList list = new DropDownList();
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.AutoPostBack = true;
list.Items.Add(new ListItem("", "0"));
list.Items.Add(new ListItem("Title", "1"));
Panel panelContainer = new Panel();
panelContainer.ID = "panel" + persistControls.Count;
panelContainer.Controls.Add(list);
Button btnRemove = new Button();
btnRemove.Click += new EventHandler(btnDelete_Click);
btnRemove.Text = "Remove";
btnRemove.CommandArgument = persistControls.Count.ToString();
myPlaceholder.Controls.Add(panelContainer); // Pushes the Panel to the page.
persistControls.Add(panelContainer);// Adds our Panel to the Control list
myPlaceholder.Controls.Add(btnRemove); // Pushes our Button to the page.
Session["persistControls"] = persistControls; // put it in the session
}
catch
{
throw;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
int deleteThisOne = int.Parse(((Button)sender).CommandArgument);
persistControls.Remove(persistControls[deleteThisOne]);
Session["persistControls"] = persistControls;
Response.Redirect(Request.Url.ToString());
}
catch
{
throw;
}
}
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
//aspx File Snippets
<asp:Button ID="btnAdd" runat="server" Text="Add Control" onclick="btnAdd_Click" />
<asp:Button ID="btnClear" runat="server" Text="Reset" onclick="btnClear_Click"/>
<asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder>
Maintaining controls like this is a real pain. You could try an alternative approach:
Create a (small) class that encapsulates the data you want to display in each Panel.
Maintain a List of these objects in Session.
Use an <asp:Repeater> to display the items by data-binding it to your list.
In the repeater's ItemTemplate, you can write out your <asp:Panel> that can contain a <asp:DropDownList>. Use the ItemCommand property of the drop-down list to bind it to a server-side event handler. Similarly you can put an <asp:Button> in the template and bind the ItemCommand property of this to another server-side event handler.
When you click Add, you'll postback to the server, add to your list, save it in session, re-bind the repeater.
When you click a Remove, you'll postback to the server, remove the item from your list, save it in session, re-bind the repeater.
This way lets ASP.Net handle all the control creation for you. However, if you really want to do all that yourself, then this Infinities Loop blog post is required reading.
I don't think this will work. If you create the dropdownlists in the PageLoad event, the viewstate will never get bound to it, and you will never see the option selected by the user.
You should create the controls OnInit, then when the PostBack occurs the ViewState data will get bound to the control and you will be able to access it.

disable or hide listbox by dropdown selection in C#

HELLO
i have one dropdown box and one listbox ,
my dropdown box values is
1-ALL
2-CUSTOM
my listbox values is retrieved from sql database
email addresses
what i want to do is if i select ALL from the dropdown box , it will disable or hide the list box in the web page. , if i choose "CUSTOM" it will enable it again.
i tried this code, but it doesn't work
if (DropDownList1.Text == "CUSTOM")
{
ListBox1.Visible = true;
}
note : i put visible = false in the properties of the listbox1
where is the problem exactly ? and where should i put this condition on the .cs page ?
You need to add an event to the dropdown box, so that your code is executed when the event fires. If you are using the designer, select the dropdown then above where the properties are there should be a little lightning symbol. Click on that and you will see all the events that the dropdown can fire. In there look for SelectedIndexChanged. Double click in there and it will create some code for you, which will look something like:
protected void mycombobox_SelectedIndexChanged(object o, EventArgs e)
{
}
Put your code in that section.
UPDATED:
If the text in the dropdown is CUSTOM then use this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "CUSTOM")
{
ListBox1.Visible = true;
}
}
You will also need to set the AutoPostBack="true" on the DropDownList1.

End user add values to a dropdownlist?

I'm populating a dropdownlist in c# asp.net-MVC from a SQL table using Linq2Sql. I'd like for the user to be able to enter something that isn't in the list into the drop down and have it add to the table. Is this possible?
Sounds like you need to add a radio button labeled "Other". When the user clicks the radio button a text box would appear that allows the user to input a new value that you can save to your DB and display in the drop down.
EDIT:
Quick snippet to enable the control using JavaScript:
<script language="javascript" type="text/javascript">
function radioclicked() {
textObj = document.getElementById('<NAME OF TEXT BOX');
textObj.disabled = false;
}
</script>
You can use a check box instead of a radio button so that the enabled property can be toggled.
To completely hide the text box then you will have to look into jQuery/Ajax.
Why can't we use a lightweight Add-on like www.combodropdown.info for this purpose? You can even consider AutoComplete plugin from jQuery, if your app already references jQuery.
Also a combobox will allow a user to enter a value in addition to picking from a list.
My MVC is not so so, but I assume this still applies as MVC is just model view controller.
What if you throw a drop down on your form visible=true, and a textbox on your form visible =false.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
Fill your drop down:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> s = Enumerable.Range(1, 10).ToList();
DropDownList1.DataSource = s;
DropDownList1.DataBind();
DropDownList1.Items.Add("Other");
}
}
Add an event to handle if someone selects other. If they do make the textbox visible:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.DropDownList1.SelectedItem.Text)
{
case "Other":
this.TextBox1.Visible=true;
break;
default:
this.TextBox1.Visible=false;
break;
}
}
Now you can enter your value and re-store back to the db.

Categories

Resources