I would like to have a radiobuttollist were you can select value null.
Something like this:
<asp:RadioButtonList ID="rblCD" runat="server" SelectedValue='<%# Bind("tblCD") %>'>
<asp:ListItem Value="RW">RW</asp:ListItem>
<asp:ListItem Value="R">R</asp:ListItem>
<asp:ListItem Value="DBNull">None</asp:ListItem>
</asp:RadioButtonList>
Thanks a lot,
Vincent
THe values in a radiobuttonlist are always strings. You would have to do something like this
<asp:ListItem Value="">None</asp:ListItem>
Then when you read the data from the control do something like
if (rblCD.SelectedValue == string.Empty)
{
MyDataRow["Column"] = DBNull.Value;
}
yo should use:
<asp:RadioButtonList runat=server ID="rd"
SelectedValue='<%# Eval("myField").GetType() == typeof(DBNull) ? null : Eval("myField") %>'>
<asp:ListItem Text="yes" Value="1"></asp:ListItem>
<asp:ListItem Text="no" Value="2"></asp:ListItem>
</asp:RadioButtonList>
yes you can do , in your example
if(rblCD.SelectedValue== "DBNull")
{
DataRow["Column"] = DbNull.Value;
}
Related
In ASP.NET project I have to load the content( TextBlock,Buttons etc) based on item selction
<asp:DropDownList ID="ddl_FieldType" CssClass="form-control" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_FieldType_SelectedIndexChanged">
<asp:ListItem Text="Single Line Text" Value="0"></asp:ListItem>
<asp:ListItem Text="Multiple Line Text" Value="5"></asp:ListItem>
<asp:ListItem Text="Date Time" Value="2"></asp:ListItem>
<asp:ListItem Text="Date Only" Value="4"></asp:ListItem>
<asp:ListItem Text="Single Select" Value="1"></asp:ListItem>
<asp:ListItem Text="Multiple Select" Value="6"></asp:ListItem>
<asp:ListItem Text="Yes / No" Value="3"></asp:ListItem>
</asp:DropDownList>
I faced error while changing item selection related to postback or callback:
I would probably try to achieve this with javascript.
Something like this should work:
<asp:DropDownList ID="ddl_FieldType" CssClass="form-control" runat="server" Onchange=findselected()>
<asp:ListItem Text="Single Line Text" Value="0"></asp:ListItem>
<asp:ListItem Text="Multiple Line Text" Value="5"></asp:ListItem>
<asp:ListItem Text="Date Time" Value="2"></asp:ListItem>
<asp:ListItem Text="Date Only" Value="4"></asp:ListItem>
<asp:ListItem Text="Single Select" Value="1"></asp:ListItem>
<asp:ListItem Text="Multiple Select" Value="6"></asp:ListItem>
<asp:ListItem Text="Yes / No" Value="3"></asp:ListItem>
<script type="text/javascript">
function findselected() {
var selMenu = document.getElementById('<%=DropDownList1.ClientID%>');
var someField = document.getElementById('<%=TB1.ClientID%>');
var anotherField = document.getElementById('label1');
var btnField= document.getElementById('<%=ExportBtn.ClientID%>');
if (selMenu.value == '2' || selMenu.value =='3') {
someField.style.display = "none";
anotherField.style.display = "none";
btnField.style.display = "none";
}
else {
someField.style.display = "";
anotherField.style.display = "";
btnField.style.display = "";
}
}
</script>
Hope this helps,
Apex
I have a radio button and I would like to default the first item as checked by default. How do I go about doing that? Below is my source code for the radio button.
<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="High">High</asp:ListItem>
<asp:ListItem Value="Mid">Mid</asp:ListItem>
<asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
ASPX Markup
<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="High" Selected="True">Select</asp:ListItem>
<asp:ListItem Value="High">High</asp:ListItem>
<asp:ListItem Value="Mid">Mid</asp:ListItem>
<asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
Selecting default value via ASPX markup:
<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="High" Selected="True">Select</asp:ListItem>
<asp:ListItem Value="Mid">Mid</asp:ListItem>
<asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
Selecting default value via C# codebehind:
if (!IsPostBack) // prevent from selecting default value during postback
{
if (radRiskLevel.SelectedIndex == -1) //-1 is the indication of none selected
{
radRiskLevel.SelectedIndex = 0; // the index of items in radiobutton in the list you want to select
//radRiskLevel.Items.FindByText("Select").Selected = true; //can also be selected by text, need to pass text name as parameter
//radRiskLevel.Items.FindByValue("High").Selected = true; //can also be selected by value, need to pass value name as parameter
}
}
Please help me if you have an idea on autopostback in asp.net using C#
i have 4 dropdown lists in one page
if i selects 1st dropdownlist then it enables 2nd dropdownlist
and after selection an option in 2nd dropdownlist 3rd dropdownlist enables
and after selection an option in 3rd drop down list 4th dropdown list enables
please let me know to do this task...
please help me
Try this:-
ASPX
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
ASPX CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Enabled = true;
DropDownList2.Enabled = false;
DropDownList3.Enabled = false;
DropDownList4.Enabled = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Enabled = true;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Enabled = true;
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList4.Enabled = true;
}
you can use SelectedIndexChanged event of drop down list to do the server side functionality and set AutoPostBack = true so that on selection change your page will get re-loaded.
Check out this Fiddle .
Important part is to handle the change event of dropdown list and to show another dropdownlist.
$("#one").change(function()
{
$("#two").toggleClass("show",true);
});
I have 4 drop downs: here is the drop down code.
<asp:DropDownList ID="DDL_TimeFromMon" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
<asp:ListItem Text="9" Value="9"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="11" Value="11"></asp:ListItem>
<asp:ListItem Text="12" Value="12"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_MonFromAMPM" runat="server">
<asp:ListItem Text="AM" Value="AM"></asp:ListItem>
<asp:ListItem Text="PM" Value="PM"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" ForeColor="Gray" Text="to"></asp:Label>
<asp:DropDownList ID="DDL_TimeToMon" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
<asp:ListItem Text="9" Value="9"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="11" Value="11"></asp:ListItem>
<asp:ListItem Text="12" Value="12"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_MonToAMPM" runat="server">
<asp:ListItem Text="AM" Value="AM"></asp:ListItem>
<asp:ListItem Text="PM" Value="PM"></asp:ListItem>
</asp:DropDownList>
one drop down is for from time in 12 hours the other is AM/PM. same repeats for to drop down. How can a compare validator work, so that we can verify that to time is greater then from time. AM/PM. I tried Compare validator on both, but it is giving error when i select drop with whatever values. please point me to right direction. If this is not possible with compare validator, then sample code for jquery or javascrpt for validation.
Thanks in advance
You could use a customvalidator like this:
Markup:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Select a valid time" OnServerValidate="TimeValidate">
</asp:CustomValidator>
CodeBehind:
protected void TimeValidate(object source, ServerValidateEventArgs args)
{
int fromVal = int.Parse(DDL_TimeFromMon.SelectedValue)
int toVal = int.Parse(DDL_TimeToMon.SelectedValue)
string fromAMPM = ddl_MonFromAMPM.SelectedValue
string toAMPM = ddl_MonToAMPM.SelectedValue
if(fromAMPM == "AM" && toAMPM == "PM")
args.IsValid = True
else if(toAMPM == "AM" && fromAMPM == "PM")
args.IsValid = False
else
args.IsValid = fromVal < toVal
}
I am trying to do validation on multiple fields with the custom validator in ASP.net using JQuery but I can't get it to work.
There is a drop down box which contains two values, based on these values other items appear.
So if the user selects option 1, then a text box appears, if they select option 2 the text box disappears and two drop down boxes appear.
Depending on what option they choose I want to validate their input.
What I have so far that is not working is
.ASPX
<asp:DropDownList ID="drpHeight" runat="server">
<asp:ListItem Value="CMs">CMs</asp:ListItem>
<asp:ListItem Value="Feet">Feet</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtCm" runat="server" Width="100px"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height"
ClientValidationFunction = "ValidateHeight" Display="Dynamic" ControlToValidate="txtCm" ValidateEmptyText="true"></asp:CustomValidator>
<asp:RegularExpressionValidator ID="revCm" runat="server"
ControlToValidate="txtCm" Display="Dynamic"
ErrorMessage="Please enter a valid number"
ValidationExpression="^([0-9]*|\d*\.\d{1}?\d*)$" SetFocusOnError="True"></asp:RegularExpressionValidator>
<br />
<br />
<asp:DropDownList ID="drpFeet" runat="server">
<asp:ListItem Value="-1">Select...</asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
<asp:ListItem Value="5"></asp:ListItem>
<asp:ListItem Value="6"></asp:ListItem>
<asp:ListItem Value="7"></asp:ListItem>
<asp:ListItem Value="8"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblFeet" runat="server" Text="Ft"></asp:Label>
<asp:DropDownList ID="drpInches" runat="server">
<asp:ListItem Value="-1">Select...</asp:ListItem>
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
<asp:ListItem Value="5"></asp:ListItem>
<asp:ListItem Value="6"></asp:ListItem>
<asp:ListItem Value="7"></asp:ListItem>
<asp:ListItem Value="8"></asp:ListItem>
<asp:ListItem Value="9"></asp:ListItem>
<asp:ListItem Value="10"></asp:ListItem>
<asp:ListItem Value="11"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblInches" runat="server" Text="Inches"></asp:Label>
<br />
<br />
JQuery
function ValidateHeight(sender, args) {
if ($('#<%=drpHeight.ClientID%>').val = "CMs") {
if ($('#<%=txtCm.ClientID%>').val().length > 0) {
args.IsValid = true;
}
else {
args.IsValid = false;
return;
}
}
else if ($('#<%=drpHeight.ClientID%>').val = "Feet") {
args.IsValid = true;
}
}
</script>
At the moment it is not working, I did have it validating the CM b but then it stopped working and now I can't figure out why.
What is the best way to approach this? I've got another one for Weight to do as well, that one has 3 input possibilities.
I came across the answer finally, I will provide the answer for anyone else who wants to know how to do this.
Changed my customer validator to:
<asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height"
ClientValidationFunction = "ValidateHeight" Display="Dynamic" ValidateEmptyText="true"></asp:CustomValidator>
Then my JQuery became
<script type="text/javascript">
function ValidateHeight(sender, args) {
var drpHeight = $('#<%=drpHeight.ClientID%>').val();
if (drpHeight == "CMs") {
if ($('#<%=txtCm.ClientID%>').val().length > 0) {
args.IsValid = true;
return;
}
else {
args.IsValid = false;
return;
}
}
else if (drpHeight == "Feet") {
var drpFeet = $('#<%=drpFeet.ClientID%>').val();
var drpInches = $('#<%=drpInches.ClientID%>').val();
if (drpFeet == -1 || drpInches == -1) {
args.IsValid = false;
return;
}
else {
args.IsValid = true;
return;
}
}
}
</script>