I need to add a condition to a DropDownList where a method can be executed by button click only if the user has selected a value different than the listItem (default value).
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource5"
DataTextField="proj_name" DataValueField="proj_name">
<asp:ListItem Text="Select a project to clone" Value="" />
</asp:DropDownList>
How can I structure an if condition to validate that the selected value is not the ListItem (default value)?
You can use asp.net delivered validation controls
Ex:
<asp:RequiredFieldValidator id="rfv1"
ControlToValidate="DropDownList1"
Display="Static"
ErrorMessage="* Select a value"
InitialValue="DefaultValueHere"
runat="server"
ValidationGroup="V1"/>
Then edit your button markup to use ValidationGroup
<asp:Button Id="button1" ValidationGroup="V1" .../>
In your codebehind button click code add this
protected void button1_onlick(Object sender, EventArgs e)
{
If(Page.IsValid)
{
// your existing code here
}
}
See sample code below
if (DropDownList1.SelectValue == "")
{
// Write your code here
}
you can also have:
if (DropDownList1.Text == "Select a project to clone")
{
// Write your code here
}
Related
The drop down list contains three countries, How do you set the minimum number of digits to be entered in the postcode text-box based on what country is selected.
For example, if user selects America, then the postcode text-Box should be set to "[0-9]{6}". But if they select Australia then the post code should be "[0-9]{4}".
<asp:DropDownList ID="dropDownList" runat="server">
<asp:ListItem Value="-1">Country</asp:ListItem>
<asp:ListItem>America</asp:ListItem>
<asp:ListItem>Australia</asp:ListItem>
<asp:ListItem>Sweden</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidateCountry"
initialValue="-1"
ForeColor="Red"
runat="server"
ErrorMessage="Please choose Country"
ControlToValidate="dropDownList"/>
<asp:Label ID="Label1" runat="server" Text="PostCode"></asp:Label>
<asp:textbox id="TextBox1" runat="server" ></asp:textbox>
<asp:CustomValidator id="CustomValidator1" runat="server"
OnServerValidate="TextValidate"
ControlToValidate="TextBox1"
ErrorMessage="Invalid postcode.">
</asp:CustomValidator>
<asp:Button ID="BtnNext" runat="server" Text="Next" />
You can do this on the SelectedIndexChanged events of the dropdownlist. Set the AutoPostback to true in the dropdownlist write the similar described logic according to your need.
private void dropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
{
//your logic, something like that
if(dropDownList.SelectedText =="America")
{
TextBox1.MaxLength= your value ...
}
}
<asp:DropDownList ID="dropDownList" runat="server" AutoPostback = "true">
EDIT:
You can check the total number of character of textbox with your predefined value.
int minlength = 5;
if(textbox1.text.length < minlength)
{
//Flag error on validation label
}
else
{
//your stuff
}
You also have client side events for the asp .net dropdown. You can set a javascript function to execute if the dropdown selection changes without posting back to the server. This will make the experience more streamlined and you can alter the min and max length directly with jquery.
You can do it like this:
DropDownList1.Attributes.Add("onChange", "return OnSelectedIndexChange();")
I added a DropDownList from the Toolbox to the Login page on a website I'm working on.
Once I choose a ListItem in the DropDownList (in my case lets say
Gym for example...), when clicked, I want that three bars we'll be opened below my DropDownList(for example, the bars that we'll be opened are Username, Password and ID), I mean three TextBoxes beneath each other.
I think you can either try the SelectedIndexChanged event or Javascript to display the textboxes without a postback.
<select>
<option value="1">Gym 1</option>
<option value="2">Gym 2</option>
<option value="3">Gym 3</option>
<select>
At firest you put your textboxes in a panle then hide this panel and
you should set Autopostback property of you drobdownlist to True and after selecting a item in DropDownList, postback will accur. So you can show that panel include text boxes.
What you might really be needing is dynamic text boxes.
In the html part:
<asp:DropDownList runat="server" ID="DDL1" autopostback = "true" onselectedindexchanged="DDL_SelectChanged" />
<asp:PlaceHolder runat="server" ID="PH1">
</asp:PlaceHolder>
In codebehind:
void DDL_SelectChanged(object sender, EventArgs e)
{
if (DDL1.SelectedIndex == 1)
{
for (int i = 0; i < 3; i++)
{
TextBox newTB = new TextBox();
newTB.ID = "TB" + i;
PH1.Controls.Add(newTB);
}
}
}
You could use MultiveView control. And set active view index in Dropdown's selectedIndexChanged event.
I wrote some example code for you:
ASPx side:
<asp:MultiView ID="multiView" ActiveViewIndex="-1" runat="server">
<asp:View ID="viewGym" runat="server">
<asp:TextBox ID="txtBxUserName" runat="server" />
<asp:TextBox ID="txtBxPassword" runat="server" />
<asp:TextBox ID="txtBxId" runat="server" />
</asp:View>
</asp:MultiView>
<asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true"
onselectedindexchanged="Dropdownlist1_SelectedIndexChanged">
<asp:ListItem Text="Choose one club" Value="0" />
<asp:ListItem Text="Gym" Value="1" />
<asp:ListItem Text="Shoppers" Value="2" />
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if ( IsPostBack ) //don't forget :)
return;
}
protected void Dropdownlist1_SelectedIndexChanged( object sender, EventArgs e )
{
if ( Dropdownlist1.SelectedValue == "1" ) //Gym item selected
{
multiView.ActiveViewIndex = 0; //Gym view active
}
}
If you want to any view not active when first page load then you set the ActiveViewIndex with -1 in aspx code.
I have a drop down list with multiple values. I have a list item named "Other". When selecting other I want to generate a text box with required field validator. I have written like this:
Markup:
<asp:DropDownList ID="dl_test_name" runat="server"
OnSelectedIndexChanged="SelectedIndexChanged"
Height="22px" Width="103px">
<asp:ListItem>Science</asp:ListItem>
<asp:ListItem>Maths</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="tb_other" runat="server" Width="94px" Visible="False">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="tb_other" ErrorMessage="*">
</asp:RequiredFieldValidator>
Code-behind:
protected void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownList = (DropDownList)sender;
if (dropDownList.SelectedValue == "Other")
{
tb_other.Enabled = true;
tb_other.Text = string.Empty;
tb_other.Visible = true;
}
else
{
tb_other.Enabled = false;
tb_other.Text = dropDownList.SelectedValue;
}
}
but when selecting on any list item ,control doesn't go to SelectectedIndexChanged event. Only after reloading the page does it work.
What is the problem?
To make your DropDownList post back to the server you need to use the AutoPostback property, like this:
<asp:DropDownList ID="dl_test_name" runat="server"
OnSelectedIndexChanged="SelectedIndexChanged"
Height="22px" Width="103px" AutoPostBack="true">
Arathy, make AutopostBack property of dropdown to true in aspx
First of all what I want to do is to pre select a value in my RadComboBox ,and if this value is not selected something else is selected then change the visibility to of some specific fields hidden.
My problem is that I'm able to make my pre select but somehow I can not change the status of my visibility for my specific fields when this pre selected value has changed.
What I have tired is to do it with a standard event OnSelectedIndexChanged but some how this is not triggering why so ever.. I have also added AutoPostBack=true as well as ViewStateMode=Enabled"
First my field's
Here comes my preslect as well here I would like to trigger the visibility change
<div class="formRowDiv">
<asp:Label ID="Activitylbl" runat="server" Text="Activity" CssClass="formLabel" />
<telerik:RadComboBox ID="rcbActivity" CssClass="rowForm" ViewStateMode="Enabled" runat="server" Width="260px" EmptyMessage="- Activity -"
DataTextField="ActivityId" DataValueField="ActivityId" AutoPostBack="true" OnSelectedIndexChanged="rcbActivity_SelectedIndexChanged">
</telerik:RadComboBox>
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="rcbActivity"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
What I want to hide:
<div class="formRowDiv">
<asp:Label ID="ActivityDescription" runat="server" Text="ActivityDescription" CssClass="formLabel" Visible="false"/>
<telerik:RadTextBox runat="server" ID="rtbActivityDescription" Wrap="true" Height="50" TextMode="MultiLine" AutoPostBack="true" CssClass="rowForm" ReadOnly="true" Visible="false" />
</div>
How I do my pre selection :
In my databind method that is called in my Page_Load
I firrst loop and then do a pre select
foreach (Activity item in ctx.Activity.OrderBy(l =>l.Code))
{
rcbActivity.Items.Add(new RadComboBoxItem(item.FullActivity, item.ActivityId.ToString()));
if (rcbActivity.Items.FindItemByValue("4") != null)
{
rcbActivity.SelectedIndex = rcbActivity.Items.IndexOf(rcbActivity.Items.FindItemByValue("4"));
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
}
Here is how I would hide my Fields
protected void rcbActivity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
In case your controls are in an update panel then try removing it if the update panel is not so important and see if the changes u make to the controls in the server side are getting affected properly
Here I have a DropDownList and Button ok that when clicked, it will enable visibility of other search textbox and search button. Also, this depend on what I select DropDownList item. Say, in my DropDownList, I have ProductName and ProductCode.I now select ProductName Next to this ddlist is button ok. When I click button ok, control such as label, textboxName and buttonSearchName will appear below of it.
How can I accomplish this?
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>ProductName</asp:ListItem>
<asp:ListItem>ProductCode</asp:ListItem>
<asp:ListItem>Category</asp:ListItem>
<asp:ListItem>SellingPrice</asp:ListItem>
<asp:ListItem>Quantity</asp:ListItem>
<asp:ListItem>BrandName</asp:ListItem>
<asp:ListItem>ReOrderQty</asp:ListItem>
<asp:ListItem>ReOrderLevel</asp:ListItem>
<asp:ListItem>Ordered</asp:ListItem>
<asp:ListItem>Allocated</asp:ListItem>
<asp:ListItem>FreeQty</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnOK" runat="server" onclick="btnOK_Click" Text="OK" />
<br />
ProductName<asp:TextBox ID="txtSearchProductname" runat="server"></asp:TextBox>
<asp:Button ID="btnSearchProductName" runat="server" Text="search"
onclick="btnSearchProductName_Click" />
<br />
To answer your question, one way to do it is (as answered before me) to add an update panel and set visibility to False, but then you would also require a ScriptManager and if you have other controls in the same page (such as a FileUpload control) that will not work properly with the presence of a ScriptManager.
You could alternatively use the same TextBox to search for all of the fields, by implementing a method that detects the value selected in your DropDownList and based on that value, the search algorithm changes accordingly.
So I just renamed your txtSearchProduct to just txtSearch and I added a universal method to search all criteria named btnSearch_Click
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>ProductName</asp:ListItem>
<asp:ListItem>ProductCode</asp:ListItem>
<asp:ListItem>Category</asp:ListItem>
<asp:ListItem>SellingPrice</asp:ListItem>
<asp:ListItem>Quantity</asp:ListItem>
<asp:ListItem>BrandName</asp:ListItem>
<asp:ListItem>ReOrderQty</asp:ListItem>
<asp:ListItem>ReOrderLevel</asp:ListItem>
<asp:ListItem>Ordered</asp:ListItem>
<asp:ListItem>Allocated</asp:ListItem>
<asp:ListItem>FreeQty</asp:ListItem>
</asp:DropDownList>
<br />
Search: <asp:TextBox ID="txtSearch" runat="server">
</asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="search"
onclick="btnSearch_Click" />
<br />
and here is an example of what would the btnSearch_Click look like
protected void btnSearch_Click(object sender, EventArgs e)
{
string searchText = this.txtSearch.Text;
switch (this.DropDownList1.SelectedValue.ToString) {
case "ProductName":
string sql = "select * from products where ProductName like '%" + searchText + "%'";
// the rest of your code goes here
break;
case "ProductCode":
string sql = "select * from products where ProductCode like '%" + searchText + "%'";
// populate some other control with your productcode search here
break;
}
}
Lots of ways to do this, but since your just starting the easiest thing to do would be to put your controls in a panel and change the visibility in your "btnOK_Click" event.
Example:
<asp:Panel id="searchPanel" runat="server" visible="false">
your controls here....
</asp:Panel>
To make this visible, in your event use the following syntax.
searchPanel.Visible = True;