I want to make first item of dropdownlist non selectable following is my code.
<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Bug</asp:ListItem>
<asp:ListItem>Enhancement</asp:ListItem>
<asp:ListItem>Tasks</asp:ListItem>
<asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>
Please help me how to do this.
You can also make it disabled in code behind on page load event
protected void Page_Load(object sender, EventArgs e)
{
IssueList.Items[0].Attributes["disabled"] = "disabled";
}
also make your first item selected
<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
<asp:ListItem Selected="true" Value="">Select One</asp:ListItem>
<asp:ListItem>Bug</asp:ListItem>
<asp:ListItem>Enhancement</asp:ListItem>
<asp:ListItem>Tasks</asp:ListItem>
<asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>
You can add disabled attribte.try with this:
<asp:DropDownList runat="server" ID="id">
<asp:ListItem Text="Test" Value="value" disabled="disabled" />
</asp:DropDownList>
You can add an 'Empty' value item:
<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Bug</asp:ListItem>
<asp:ListItem>Enhancement</asp:ListItem>
<asp:ListItem>Tasks</asp:ListItem>
<asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>
Then you would check for
if (string.IsNullOrEmpty(IssueList.SelectedValue))
You could use attribute as in combination of disabled and selected. may this will help to get resolve your issue.
<asp:ListItem Selected="True" disabled="disabled">Select One</asp:ListItem>
^^^^^^^ ^^^^^^^
Related
I have a form with textbox, radiobutton etc. and would like to have a validationgroup attached to some of the input fields when one dropdown changes to a specific value. Is that possible.
lets say i have this form;
<form>
Name:
<asp:textbox runat="server" id="nametextbox" ValidationGroup="myvalgroup"/>
<asp:RequiredFieldValidator id="nametextboxFieldValidator" runat="server" ErrorMessage="Name?" ValidationGroup="myvalgroup" ControlToValidate="nametextbox" />
Gender: <asp:RadioButtonList runat="server" ID="gender" RepeatDirection="Horizontal" ValidationGroup="myvalgroup">
<asp:ListItem Text="Select gender" Value="" Selected="True"/>
<asp:ListItem Text="Male" Value="Male"/>
<asp:ListItem Text="Female" Value="Female"/>
</asp:RadioButtonList>
<asp:RequiredFieldValidator id="genderFieldValidator" runat="server" ErrorMessage="Gender?" ValidationGroup="myvalgroup" ControlToValidate="gender" />
The answer to life the universe and everything:
<asp:textbox runat="server" id="theanswer" />
Are you finished?
<asp:RadioButtonList runat="server" ID="finished" RepeatDirection="Horizontal" onchange="add-validation-group()" >
<asp:ListItem Text="Are you done?" Value="" Selected="True"/>
<asp:ListItem Text="Yes" Value="Yes"/>
<asp:ListItem Text="No" Value="No"/>
<asp:ListItem Text="Absolutly not, i need 600 more years to think!" Value="NOT-IN-600-YEARS"/>
</asp:RadioButtonList>
<asp:button runat="server" id="submitbutton" />
</form>
Codebehind
function add-validation-group()
if (finished.SelectedValue.ToString() == "Yes")
{
theanswer.Attributes.Add("ValidationGroup", "myvalgroup");
}
end
Ok so my codebehind contains a mixup of everything but just to show what i would like to acomplish, sorry for the codemixing!
So can this be done, add validation to inputfields when onchange on a dropdownlist?
Point me in the right direction, please! :)
Finally i found a solution to my question.
Quite simple i might add.
<script type=text/javascript>
$(document).ready(function () {
$("#<%= finished.ClientID %>").change(function () {
if ($('option:selected', this).text() == 'Yes') {
ValidatorEnable(document.getElementById('<%=theanswerValidator.ClientID%>'), true);
}
});
});
</script>
<form>
Name:
<asp:textbox runat="server" id="nametextbox" ValidationGroup="myvalgroup"/>
<asp:RequiredFieldValidator id="nametextboxFieldValidator" runat="server" ErrorMessage="Name?" ValidationGroup="myvalgroup" ControlToValidate="nametextbox" />
Gender: <asp:RadioButtonList runat="server" ID="gender" RepeatDirection="Horizontal" ValidationGroup="myvalgroup">
<asp:ListItem Text="Select gender" Value="" Selected="True"/>
<asp:ListItem Text="Male" Value="Male"/>
<asp:ListItem Text="Female" Value="Female"/>
</asp:RadioButtonList>
<asp:RequiredFieldValidator id="genderFieldValidator" runat="server" ErrorMessage="Gender?" ValidationGroup="myvalgroup" ControlToValidate="gender" />
The answer to life the universe and everything:
<asp:textbox runat="server" id="theanswer" />
<asp:RequiredFieldValidator id="theanswerValidator" Enabled="false" runat="server" ErrorMessage="Your answer?" ValidationGroup="myvalgroup" ControlToValidate="theanswer" />
Are you finished?
<asp:DropDownList runat="server" ID="finished" >
<asp:ListItem Text="Are you done?" Value="" Selected="True"/>
<asp:ListItem Text="Yes" Value="Yes"/>
<asp:ListItem Text="No" Value="No"/>
<asp:ListItem Text="Absolutly not, i need 600 more years to think!" Value="NOT-IN-600-YEARS"/>
</asp:DropDownList>
<asp:button runat="server" id="submitbutton" />
</form>
I added required field validators to theanswer textbox and added Enabled="false" i then track a onchange event with javascript to see if user selects Yes and if they do enable the requiredfieldvalidator.
In my question i had a Radiobuttonlist for the (are you finished?), But changed that to a DropdownList instead for other reasons but i cant think that this is impossible if you have radiobutton list.
OK, it's not in codebehind but it works.
I did try to do it codebehind but it dident feel user friendly to have a Postback on select change.
If someone is intrested in the codebehind method i did it this way, and it worked but (in my opinion not so user friendly)
On my asp:Dropdownlist i added
OnSelectedIndexChanged="finished_SelectedIndexChanged" AutoPostBack="true"
and in my codebehind i added this:
protected void finished_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
if (ddl.SelectedItem.ToString().Equals("Yes"))
{
theanswerValidator.Enabled = true;
}
else // If someone selects yes and then regret it and selects no then disable validator again
{
theanswerValidator.Enabled = false;
}
}
Anyone know who to display NO text in a radiobutton item but have a value for each?
Here is my radio button list:
<asp:RadioButtonList ID="rdoD4_1" runat="server" RepeatDirection="Horizontal" Width="407px">
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
</asp:RadioButtonList>
Problem is that it displays 0,1,2 etc next to each list item - I want to keep the value but show no text - any ideas?
Try explicitly setting the Value and Text attributes.
<asp:RadioButtonList runat="server">
<asp:ListItem Value="1" Text="" runat="server"></asp:ListItem>
<asp:ListItem Value="2" Text="" runat="server"></asp:ListItem>
</asp:RadioButtonList>
To be brief, I need the Textbox with ID="textComments" to only appear when the dropdown option of 'Other' is selected from the dropdown, and then disappear if another selection is made. I could do this with JS but it needs to be in C#.
<asp:DropDownList runat="server" ID="dropEnquiryType" CssClass="dropdownRequestList">
<asp:ListItem Value="Customer Services" Text="Customer Services"></asp:ListItem>
<asp:ListItem Value="Website" Text="Website"></asp:ListItem>
<asp:ListItem Value="Contract" Text="Contract Hire"></asp:ListItem>
<asp:ListItem Value="Other" Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:label runat="server" ID="lblComments" AssociatedControlID="textComments" CssClass="textLabel">Comments:</asp:label>
<asp:TextBox runat="server" MaxLength="200" TextMode="MultiLine" Columns="40" Rows="4" ID="textComments" Wrap="true" CssClass="textComments"></asp:TextBox>
And help would be greatly appreciated.
Make the DropDownList first AutoPostBack=true
handle it's SelectedIndexChanged-event
Switch visibility of both controls there
aspx:
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="dropEnquiryType_Changed" runat="server" ID="dropEnquiryType" CssClass="dropdownRequestList">
<asp:ListItem Value="Customer Services" Text="Customer Services"></asp:ListItem>
<asp:ListItem Value="Website" Text="Website"></asp:ListItem>
<asp:ListItem Value="Contract" Text="Contract Hire"></asp:ListItem>
<asp:ListItem Value="Other" Text="Other"></asp:ListItem>
</asp:DropDownList>
codebehind:
protected void dropEnquiryType_Changed(Object sender, EventArgs e)
{
lblComments.Visible = dropEnquiryType.SelectedValue == "Other";
textComments.Visible = lblComments.Visible;
}
If you absolutely have to do this within C#, then a simple check in PreRender should be sufficient:
textComments.Visible = (dropEnquiryType.SelectedValue == "Other");
This will also require you to set AutoPostback on dropEnquiryType, though, which uses ... JavaScript!
I have a few buttons within an update panel. I have now been asked to replace the buttons with
a drop down list . I see that the selectedindexchanged event on the drop down list is much slower
than the button click event. I have the code below. Can anyone point me as to why it is happenning ?
or what I can do to make the selectedindexchanged event response faster .
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<div class="ui-widget-header" style="display: inline;">
<asp:Label ID="lblRefresh" runat="server" Text="Refresh Interval:" CssClass="label"
ForeColor="Black"></asp:Label>   
<asp:LinkButton ID="btnOFF" runat="server" OnClick="btnOFF_Click" Text="Off">
</asp:LinkButton> 
<asp:LinkButton ID="btn60SEC" runat="server" OnClick="btn60SEC_Click" Text="1Min">
</asp:LinkButton> 
<asp:LinkButton ID="btn5MIN" runat="server" OnClick="btn5MIN_Click" Text="5Min">
</asp:LinkButton> 
<asp:LinkButton ID="btn10MIN" runat="server" OnClick="btn10MIN_Click" Text="10Min">
</asp:LinkButton> 
<asp:LinkButton ID="btn15MIN" runat="server" OnClick="btn15MIN_Click" Text="15Min">
</asp:LinkButton>
<asp:DropDownList ID="ddlRefresh" runat="server" onselectedindexchanged="ddlRefresh_SelectedIndexChanged">
<asp:ListItem Text="OFF" Value="0"></asp:ListItem>
<asp:ListItem Text="5MIN" Value="5"></asp:ListItem>
<asp:ListItem Text="10MIN" Value="10"></asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlRefresh" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
Well what seems to work for me right now is to set AutoPostback = "true" for the dropdownlist .
<asp:DropDownList ID="ddlRefresh" runat="server" onselectedindexchanged="ddlRefresh_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="OFF" Value="0"></asp:ListItem>
<asp:ListItem Text="5MIN" Value="5"></asp:ListItem>
<asp:ListItem Text="10MIN" Value="10"></asp:ListItem>
</asp:DropDownList>
I had the same problem and I found that the problem is with _destroyTree. Check it here (https://siderite.dev/blog/very-slow-updatepanel-refresh-when.html).
How can i set each list item value dynamically in radio button list?
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
In above radio button list the list items i want to set from database.
your help will be highly appreciated.
You must find ListItem by text and change it. Let's think, you have control :
<asp:RadioButtonList ID="yourrdblist" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" CssClass="tarpiukai">
<asp:ListItem Selected="True" Value="0" Text="0"></asp:ListItem>
<asp:ListItem Value="0" Text="1"></asp:ListItem>
<asp:ListItem Value="1" Text="2"></asp:ListItem>
</asp:RadioButtonList>
In this case you can easy change value of ListItem with text "1" to text "bambargija" :
yourrdblist.Items.FindByText("1").Text = "bambargija";
That's easy. Have a look at the basiscs of the RadioButtonList-Control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.aspx
If you wanna have access to the inner controls, the ListItems then acces them by using the controls property Items.
Greetings,
Like this:
<asp:RadioButtonList ID="inputs" runat="server" Width="50px">
</asp:RadioButtonList>
..and...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
inputs.Items.Add("input1");
inputs.Items.Add("input2");
inputs.Items.Add("input3");
inputs.Items.Add("input4");
}
}