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.
Related
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 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>
I stored the selected value in a DDL list on Page 1 in query string variable and then tried to assign it on page 2, to the same drop down list coming from user control page. But while assigning the value to the DDList on page 2 I am getting either an array index out of bound exception or a null value exception.
I have debugged and verified that the query string is correct, but it is unable to assign this value to the ddl list.Code pasted below:
<telerik:RadComboBox
ID="cmbSearchOaO"
runat="server"
AutoPostBack="true"
AppendDataBoundItems="true"
Width="200px"
DataSourceID="odsOwnedAndOperated"
DataTextField="Owned_And_Operated_Nm"
DataValueField="Owned_And_Operated_Id"
OnSelectedIndexChanged="PopulateApplicationTypeDropDown">
</telerik:RadComboBox>
ddl2.SelectedValue = Request.QueryString["No2"];
ddl2.FindItemByValue(Request.QueryString["No2"].ToString()).Selected = true;
The correct value is populated in Request.QueryString["No2"] , but I need to store it on LHS i.e on ddl list.
Try this solution. In my example I used ASP.NET DropdownList control
User control that holds the dropdownlist to be used by page1 and page 2
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="DDlUserControl.ascx.cs" Inherits="WebApplication2.DDlUserControl" %>
<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="True" Height="20px"
Width="223px">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
<asp:ListItem Value="6">Item 6</asp:ListItem>
<asp:ListItem Value="7">Item 7</asp:ListItem>
<asp:ListItem Value="8">Item 8</asp:ListItem>
</asp:DropDownList>
Page1 html page
<p>
<uc1:DDlUserControl ID="DDlUserControl1" runat="server" />
</p>
<p>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Go to Page 2" />
</p>
Page1 code behind
protected void btnSubmit_Click(object sender, EventArgs e)
{
var ddl = DDlUserControl1.FindControl("ddlTest") as DropDownList;
Response.Redirect("Page2.aspx?no="+ddl.SelectedValue);
}
Page2 html
<div>
This is page 2<br />
<br />
<uc1:DDlUserControl ID="DDlUserControl1" runat="server" />
</div>
Page2 code behind
protected void Page_Load(object sender, EventArgs e)
{
var selectedVal = Request.QueryString["no"];
var ddl = DDlUserControl1.FindControl("ddlTest") as DropDownList;
ddl.SelectedValue = selectedVal;
}
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;
...
}