Im trying to validate a dropdownlist and if the selectedValue = "0" then the Isvalid = false but it doesn't seem to be working does any one know what todo here
protected void valCountry_ServerValidate(object sender, ServerValidateEventArgs e)
{
if ((e.Value == "0"))
{
e.IsValid = false;
MasterPage master = Page.Master;
AjaxControlToolkit.ModalPopupExtender popupExtender = (AjaxControlToolkit.ModalPopupExtender)master.FindControl("ModalPopupExtender1");
popupExtender.Show();
}
}
<asp:DropDownList ID="ddlCountries" runat="server">
<asp:ListItem Value="0" Text="Please Choose" />
<asp:ListItem Value="New Zealand" Text="New Zealand" />
</asp:DropDownList>
<asp:CustomValidator ID="valCountry" runat="server"
ControlToValidate="ddlCountries"
Display="Dynamic"
ErrorMessage="You must select a Country."
SetFocusOnError="true"
ValidationGroup="UserInfo"
OnServerValidate="valCountry_ServerValidate">*</asp:CustomValidator>
Try using ddlCountry.options[ddlCountry.selectedIndex].value instead of e.Value.
Related
I have dropdownlist1 containing items 2wheeler, 3wheeler.
If I select 2 wheeler dropdownlist2 should be open with items gear and non gear.
if I select gear I want to open textbox to write bike model name.
else if I select non gear I want to open just label.
Your design page:
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ShowDDL2" />
<asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="ShowControls" Visible="false" />
<asp:TextBox ID="TextBox1" runat="server" Visible="false" />
<asp:TextBox ID="TextBox2" runat="server" Visible="false" />
<asp:Label ID="Label1" runat="server" Visible="false" />
<asp:Label ID="Label2" runat="server" Visible="false" />
Your code behind:
protected void ShowDDL2(object sender, EventArgs e){
ddl2.Visible = true;
}
protected void ShowControls(object sender, EventArgs e){
TextBox1.Visible = TextBox2.Visible = Label1.Visible = Label2.Visible = true;
}
Is this what you need? But I don't see a logic in this...
Is there any way to send extra parameter to SelectedIndexChanged function?
<asp:RadioButtonList
ID="rblMeetingPlace"
SelectedValue = '<%# Bind("intMtgLoc") %>'
*OnSelectedIndexChanged = "Validate('txtMeetPlaceOther')"*
runat="server"
RepeatDirection="Horizontal"
>
<asp:ListItem Value="1">Workshop</asp:ListItem>
<asp:ListItem Value="2">Service provider agency</asp:ListItem>
<asp:ListItem Value="3">Advocacy organization</asp:ListItem>
<asp:ListItem Value="4">Public Space</asp:ListItem>
<asp:ListItem Value="5">Other (specify): </asp:ListItem>
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
<asp:TextBox ID="txtMeetPlaceOther" Text='<%# Bind("strMtgLocOth") %>'
runat="server" />
I have couple of radiobuttonlist and I want to enable the textboxes when "Other" is selected.
I am thinking of sending the textbox's id to enable it.
Any idea?
You can easily do it this way:
<asp:radiobuttonlist id="rbl1" runat="server"
RepeatDirection="Horizontal"
AutopostBack="true"
SelectedValue='<%# Bind("intMtgLoc") %>'
OnselectedIndexChanged="rbl1_SelectedIndexChanged">
<asp:ListItem Value="1">Workshop</asp:ListItem>
<asp:ListItem Value="2">Service provider agency</asp:ListItem>
<asp:ListItem Value="3">Advocacy organization</asp:ListItem>
<asp:ListItem Value="4">Public Space</asp:ListItem>
<asp:ListItem Value="5">Other (specify): </asp:ListItem>
<asp:ListItem Value="" Text="" style="display:none" />
</asp:radiobuttonlist>
<asp:textbox id="txtMeetPlaceOther" text='<%# Bind("strMtgLocOth") %>' runat="server" />
<asp:textbox id="TextBox1" enabled="false" runat="server"></asp:textbox>
<asp:textbox id="TextBox2" enabled="false" runat="server"></asp:textbox>
and in code-behind:
protected void rbl1_SelectedIndexChanged(object sender, EventArgs e)
{
*yourValidatorName*.Validate();
if (Convert.ToInt32(rbl1.SelectedValue) == 5)
{
TextBox1.Enabled = true;
TextBox2.Enabled = true;
}
else
{
TextBox1.Enabled = false;
TextBox2.Enabled = false;
}
}
reply to comment:
First of all you should set OnSelectedIndexChanged for all RadioButtonLists event handlers. Here - rbl_SelectedIndexChanged.
Then in code behind:
protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox[] textboxes = new TextBox[] { TextBox1, TextBox2 };//all your textboxes.
RadioButtonList whoCallEvent = sender as RadioButtonList;
string last = whoCallEvent.ID.ToString().Substring(whoCallEvent.ID.ToString().Length - 1, 1);//get the last symbol of object (TextBox) ID, who call event.
int index = Convert.ToInt32(last);
if (Convert.ToInt32(whoCallEvent.SelectedValue) == 5)
{
textboxes[index - 1].Enabled = true;
}
else
{
textboxes[index - 1].Enabled = false;
}
}
But I think this is conceptually wrong to do it this way. The best way is to create rbl_SelectedIndexChanged for all the radioButtonList you have on my page.
I have two dropdownlists, if one of them contain a value (not default value, which is empty string), the other should also have a value (like requredvalidator). But if no one has a value the page should validate 'true'.
I cannot solve it with a validation group, because the button that trigger the validation is already triggering other validations.
Could you just use a custom validator control and put your logic in the server side validation method. Keep it in the same validation group.
<asp:CustomValidator ID="valCust" runat="server"
ControlToValidate="ddlControl" ErrorMessage="error Message"
ValidationGroup="Group"
onservervalidate="valCust_ServerValidate" >*</asp:CustomValidator>
code behind
protected void valCust_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = MethodToWorkOutValidation();
}
please check below code snippet.
function EnableDisableValidator() {
var DropDownList1 = document.getElementById('DropDownList1');
var DropDownList2 = document.getElementById('DropDownList2');
var RequiredFieldValidator12 = document.getElementById('RequiredFieldValidator12');
if (DropDownList1.selectedIndex > 0 && DropDownList2.selectedIndex > 0) {
ValidatorEnable(document.getElementById('RequiredFieldValidator12'), true);
}
else {
ValidatorEnable(document.getElementById('RequiredFieldValidator12'), false);
}
}
..........
<asp:DropDownList ID="DropDownList1" runat="server" onchange="EnableDisableValidator();">
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" onchange="EnableDisableValidator();">
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
</asp:DropDownList>
<div style="display: none;">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="TextBox1"
Enabled="false" ErrorMessage="*************" ></asp:RequiredFieldValidator>
Let me know if any concern
I have got a dropdown list populated with products from a product table. I have a button named ADD Product, so when the user wants to add a new product instead of selecting product from the drop down list, one should click on add new then panel 1 should show which includes a textbox. Panel1 visibility is set to false by default and it should be visible when the user clicks on the button. Also I want a validation on dropdown list and textbox(if panel is visible). Below is the code which is not working :
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<%--<asp:TextBox ID="txtMake" runat="server" CssClass="txtStyle"></asp:TextBox>--%>
<asp:Button ID="btnAdd" runat="server" />
<asp:Panel ID="panel1" Visible="false" runat="server"><asp:TextBox ID="txtAddnew"></asp:TextBox></asp:Panel>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Select a Product" ValidationGroup="insert" ControlToValidate="DropDownList1" ForeColor="Red"></asp:RequiredFieldValidator><br />
codebehind:
protected void btnAdd_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MultiView1.ActiveViewIndex = 0;
DropDownList1.DataSource = caravans.GetProductNames();
DropDownList1.DataBind();
}
}
You can use this concept for validation on dropdownlist. I am given you an example please check this...
<asp:DropDownList runat="server" ID="ddl">
<asp:ListItem Value="-1" Text="Select"></asp:ListItem>
<asp:ListItem Value="1" Text="One"></asp:ListItem>
<asp:ListItem Value="2" Text="Two"></asp:ListItem>
</asp:DropDownList>
<asp:RegularExpressionValidator ID="reg1" runat="server" ControlToValidate="ddl"
Display="Dynamic" ErrorMessage="Select Proper" SetFocusOnError="true" Text="Select Proper"
ValidationGroup="check" ValidationExpression="^\d{0,5}$" />
<asp:Button ID="btn" runat="server" ValidationGroup="check" Text="Submit" />
I have three DropDown List Controls.each dropdown contains the static values 1,2,3,4
what i need to do is when a item is selected on the first dropdown(on SelectedIndexChanged Event) same items should be selected in other two dropdowns
Here's what you do.
In the ASPX code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
In the codebehind:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
string value = ddl.SelectedValue;
SetValue(DropDownList1, value);
SetValue(DropDownList2, value);
SetValue(DropDownList3, value);
}
protected void SetValue(DropDownList ddl, string value)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
}
Just typing ddl1.SelectedValue = ddl2.SelectedValue won't work, because DropDownList.SelectedValue is read-only.
Note that I didn't just set the SelectedIndex of all the DDLs to that of the sender. You can use that in your example scenario, but if one of your DDLs ever has its ListItems in a different order from the others, the code will break. In my opinion, this makes it dangerous practice, but YMMV.
Also, if you decide to generalize the SetValue method (I often add it as an extension method to the DropDownList control across the entire project), you should handle cases where the target value isn't found in the DDL, presumably by throwing an exception. You can also make a SetText version using FindByText.
Set the selectedvalue property on 2nd and 3rd drop down to the selected value on the first. Set the DropDown to autopostback="true" so that it posts back to the server, and you can set it appropriately.
OR, use client-side JavaScript to change the selectedindex property on the other select elements when the change client event fires for the first.
All you need to do is set the SelectedIndex of your other drop down lists like this:
protected void DropDownList1SelectedIndexChanged(Object sender, EventArgs e)
{
dropDownList2.SelectedIndex = dropDownList1.SelectedIndex;
dropDownList3.SelectedIndex = dropDownList1.SelectedIndex;
}
Try the following:
Markup:
<asp:DropDownList ID="dd1" OnSelectedIndexChanged="dd1_SelectedIndexChanged" AutoPostBack="true" runat="server" />
<asp:DropDownList ID="dd2" runat="server" />
<asp:DropDownList ID="dd3" runat="server" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = dd1.SelectedValue;
dd1.SelectedValue = dd2.SelectedValue = dd3.SelectedValue = selected;
BindData();
}
private void BindData()
{
int[] values = { 1, 2, 3, 4 };
dd1.DataSource = dd2.DataSource = dd3.DataSource = values;
dd1.DataBind();
dd2.DataBind();
dd3.DataBind();
}