Assign value to dropdown list via query string - c#

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

Related

How to show or hide the text box depends on the drop down selection

When the selection of the dropdown is changed by the user, the corresponding textbox will show depends on user's selection. Let's say when user select "A" from the dropdown, the Textbox "A" will shown, while the other textbox will be invisible.
The issue is when user select "A" in the drop down, the other two text box won't disappear.
aspx
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:DropDownList>
A <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
B <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
C <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
aspx.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "A")
{
TextBox2.Visible = false;
TextBox3.Visible = false;
}
}
Make all textbox properties set to visible false and then try in dropdown selection chagned event.
<asp:TextBox ID="TextBox1" runat="server" visible="false"></asp:TextBox>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "A")
{
TextBox2.Visible = false;
}
else
{
TextBox2.Visible = true;
}
}
It's not working because you should refresh your page. You should add AutoPostBack="true" property to your DropDownList.
Also, you can use UpdatePanel to don't need to refresh all your page.
In aspx use this:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
I hope it helps you
If your goal is just to toggle visibility of the textboxes based on drop down selection, you should write a JavaScript function on your page which will take index as parameter. Then it will change the visibility of the indexed text box to true and false for the other text boxes.
Something like this:
Dropdown event:
on-click=“return toggleText(selectedIndex);”
<script>
function toggleText(index) {
// show or hide textbox by setting it’s style display:block or display:none respectively
return false; // ensures the page is not posted-back to server
}
</script>

How to access the data in a Dropdown list which is inside a repeater when button is clicked

I have a dropdown list inside a repeater which has data in its list item I need to access the selected data when the button inside the repeater is clicked. my html code is as follows
<asp:Repeater runat="server" ID="rptrData" OnItemCommand="rptrData_ItemCommand">
<ItemTemplate>
<tr role="row" class="odd">
<td>
<asp:DropDownList ID="ddlProgress" runat="server">
<asp:ListItem Value="0">No Basement</asp:ListItem>
<asp:ListItem Value="1">Basement</asp:ListItem>
<asp:ListItem Value="2">Lintel</asp:ListItem>
<asp:ListItem Value="3">Roof</asp:ListItem>
</asp:DropDownList></td>
<td>
<div class="btn-group btn-group-xs">
<asp:Button ID="Update" runat="server" Text="Update" UseSubmitBehavior="False" CommandName="Update" />
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
You can use FindControl on the RepeaterCommandEventArgs Item since the Repeater is the sender.
protected void rptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//use findcontrol to locate the DDL and cast it
DropDownList drp = e.Item.FindControl("ddlProgress") as DropDownList;
//show result
Label1.Text = drp.SelectedValue;
}
Note that all your values in ddlProgress are 0, that can cause problems. Make those unique.

Why my CheckBox.Checked Property will reset?

Using: .NET 3.5SP1, VS2008
I was editting someone else asp.net script, he did the Data retreving at the Page_Load while Page is not postback.
I could see the data was populated into the DropDownList properly even after I refresh, navigates, postback in the page.
I added couples more DropDownList and some CheckBoxes into the script, only the DropDownList I added got populated properly. But not the CheckBox.
So I do a test in a new project, which is similar to its script structure:
ASPX:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
%>
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
}
%>
</div>
</form>
Code-Behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.CheckBox1.Checked = true;
this.CheckBox1.Text = "Hello CheckBox";
this.TextBox1.Text = "Hello TextBox";
this.DropDownList2.SelectedValue = "Item2";
}
}
}
So as you see the code, when the page first load, the CheckBox1's text will change, Checked will be true, so as other TextBox and DropDownList2
After I select DropDownList1's item to Item2, when the CheckBox1, TextBox1, DropDownList2 nothing got setted, except the CheckBox1.Text.
Why is this happen?
EDIT:
I tried to put them into Panel, in this way it work. But the problem is the program I am editting is using the format above.. So I am not allow to change them all to Panel.
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
this.MyPanel.Visible = true;
}
else
{
this.MyPanel.Visible = false;
}
%>
<asp:Panel ID="MyPanel" runat="server" Visible="false" >
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
</div>
</form>
Try out setting EnableViewState and ViewStateMode, perhaps some of the parent controls has disabled it so default Inherit value has been applied.
MSDN:
The default value of the ViewStateMode property for a Web server
control in a page is Inherit. As a result, if you do not set this
property at either the page or the control level, the value of the
EnableViewState property determines view-state behavior.
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1"
EnableViewState="true"
ViewStateMode="Enabled" />

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

Categories

Resources