How to select a radio button by default in asp.net - c#

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
}
}

Related

Asp.net Dropdownlist selected item value

I have a problem with the SelectedItem in the DropDownList
<asp:DropDownList ID="Etkin_Drop" runat="server" OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged">
<asp:ListItem Text="Seç" Value="-1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Aktif" Value="1"></asp:ListItem>
<asp:ListItem Text="Deaktif" Value="0"></asp:ListItem>
</asp:DropDownList>
First list item value is -1 but when I want to check in the if statement its not working
protected void Etkin_Drop_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(Etkin_Drop.SelectedItem.Value) == -1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Lütfen Bir Seçim Yapınız');", true);
}
else
{
Label4.Text = Etkin_Drop.SelectedItem.Value;
}
}
I could not define the problem
Add AutoPostBack Property to your DropDownList and set this property to True. Like this:
<asp:DropDownList ID="Etkin_Drop" runat="server"
OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged" AutoPostBack="True">

Step by Step dropdown selection

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);
});

Capturing changed values from a Repeater on postback

This is plaguing me and I've read countless solutions that all seem to be off a bit. I have a repeater that has 2 drop downs per item and a few items. These all have potential values 0-8 that the user can select and I want to gather the data on a postback when the user click a button on the bottom of the page.
<asp:Repeater ID="rptPricing" runat="server" OnItemDataBound="rptPricing_OnItemDataBound">
<ItemTemplate>
<div class="Row clear">
<div class="Column Cat"><%# ((Price)Container.DataItem).AgeCategory %></div>
<div id="dWasPrice" runat="server" class="Column Was">
<asp:Literal ID="litPreviousPrice" runat="server" /><span class="strike"></span>
</div>
<div class="Column Now">$<%# ((Price)Container.DataItem).FullPrice %></div>
<div class="Column Deposit">
<asp:DropDownList ID="ddlCountForPart" runat="server" skucode="<%# ((Price)Container.DataItem).PartHeaderCode %>">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<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:DropDownList>
</div>
<div class="Column Full">
<asp:DropDownList ID="ddlCountForFull" runat="server" skucode="<%# ((Price)Container.DataItem).FullHeaderCode %>">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<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:DropDownList>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
Here is the binding.
private void BindDetails()
{
SetDiscountContent();
rptPricing.DataSource = _Detail.Prices;
rptPricing.DataBind();
}
Here is where the data is not present.
if ( IsPostBack && UIControlUtil.GetPostBackControl(Page) == lnkAddToCart)
{
//populate the cart without javascript
//find the drop down with items selected and create Order Elements
try
{
var orders = new Order[0];
foreach (RepeaterItem item in rptPricing.Items)
{
var dl = item.FindControl("ddlCountForFull");
if (dl != null)
{
Array.Resize(ref orders, orders.Length + 1);
orders[orders.Length - 1] = new Order() {
Quantity = Convert.ToInt32(dl.SelectedValue),
Sku = dl.Attributes["skucode"]};
I've tried moving the call to BindDetails() around from PageLoad to OnInit and also playing with the postback flag. I have also played with the ViewState flags to no avail.
EDIT (Adding Page Load), please note I have played with moving this to the OnInit.
protected void Page_Load(object sender, EventArgs e)
{
if (ContentItem == null) return;
if (!IsPostBack)
{
var control = (ContentManagement.Library.Sitecore.PreDefinedControl)ContentItem;
_Detail = (Details)control.DataElements[0];
BindDetails();
}
if (ShoppingCartHelper.GetProductsOfTInCart<blah>() > 8)
{
lnkAddToCart.Enabled = false;
lnkAddToCart.CssClass = lnkAddToCart.CssClass + " disabled cartmax";
}
}
First off make sure you are doing a:
if (!IsPostBack)
{
BindDetails();
}
This will ensure you are not binding with every postback and losing your data.
Also, I do not understand the need for:
if ( IsPostBack && UIControlUtil.GetPostBackControl(Page) == lnkAddToCart)
Why do you not just implement the OnClick handler for a Button or LinkButton and move all of your code to pull the data out of the repeated into the handler?
Also make sure ViewState is enabled or you will not be able to iterate through the Repeater to find the changed values.
EDIT:
Check that you have not disabled ViewState in on of the containers that the Repeater exists in. You should have the data. The only reason you wouldn't is if ViewState is disabled on the control or one of it's parents or you are wiping the values in some way such as rebinding. Can you post more of your code like your Page_Load and any event that will occur after it?
Try putting a TextBox after the Repeater and enter soem text in it and then post your Repeater. Can you see the text in the TextBox that you entered? If not then a parent control has disabled ViewState or at the page level. If you can see the value, then somewhere you are rebinding to Repeater unintentionally and wiping your data out. You need to put a breakpoint where you do the binding and ensure that it is NOT occurring when you post back.
There isn't much more help I can give you at this point.

how to change imageurl after selection from dropdownlist

Hi I have a dropdownlist and id like upon selecting one of the four choices to set the imageurl of Image2 in code behind?
An example. In your markup:
< <asp:DropDownList ID="TestDropDownList" runat="server"
onselectedindexchanged="TestDropDownList_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="http://url.com/image1.png" Text="Option 1"></asp:ListItem>
<asp:ListItem Value="http://url.com/image2.png" Text="Option 2"></asp:ListItem>
<asp:ListItem Value="http://url.com/image3.png" Text="Option 3"></asp:ListItem>
<asp:ListItem Value="http://url.com/image4.png" Text="Option 4"></asp:ListItem>
</asp:DropDownList>
<asp:Image ID="TestImage" ImageUrl="" runat="server" />
In your code-behind:
protected void TestDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
Image i = this.TestImage;
i.ImageUrl = ((DropDownList)sender).SelectedValue;
}
You have to enable AutoPostBack property for the dropdownlist. Then each time a selection changes postback will be send to server, so you codebehind will be executed. If I remeber corectly DropDownList control has an event for changed selection.
Add a OnSelectedIndexChanged eventhandler, and set AutoPostBack to true:
<asp:DropDownList ID="Options" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="Options_SelectedIndexChanged">
<asp:ListItem Value="Item1">Text 1</asp:ListItem>
<asp:ListItem Value="Item2">Text 2</asp:ListItem>
</asp:DropDownList>
In the code behind you implement the method that handles the event:
protected void Options_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = this.Options.SelectedValue;
...
}

C#: DataBase Null in RadioButtonList

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;
}

Categories

Resources