I have a radiobuttonlist that lives on a user control. This user control lives in a repeater on a parent user control, and that user control lives on a page with a submit button.
So something like this:
<page>
<UserControl1>
<Repeater>
<UserControl2>
<radiobuttonlist>
</UserControl2>
</Repeater>
</UserControl1>
<Submit button />
</page>
The radiobuttonlist is dynamically populated in the code-behind of UserControl2. The problem is that when I submit the form, I need to access the SelectedValue of the radiobuttonlist, and that value is always empty. Even if I first fire the methods that populate the radiobuttonlist, the selectedvalue of the RBL is empty. I have a SelectedIndexChanged event handler on the RBL, but it never fires.
What do I need to do to be able to get the SelectedValue of the radiobuttonlist when I cause the parent page to postback?
I got it working. I guess it was an order of operations issue. The fix was to dynamically declare the event handler of the radiobuttonlist in the OnInit() of UserControl2.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rblOptions.SelectedIndexChanged += new EventHandler(ctrlOptions_SelectedIndexChanged);
}
Once I did that, the event started firing, even though I was re-instantiating the UserControl on postback. Since the event was firing, I was able to obtain the Selected Index without needing to keep it in ViewState.
When you post your datas, you re-bind your datas so you erase your selected event or values.
Try with this code in your Page_Load (Of your User Control)
If(! IsPostBack)
{
//You build RadioButtonList
}
And persist your datas with ViewState, EnableViewState="true"
Related
I've created an ASP.net web page with an ASP Repeater. I bind the ASP Repeater with an IOrderedEnumerable<int> DataSource.
I need to access the Repeater DataItems at inside the Page_Load event handler. However, when I try to get the value of repeater.Items[x].DataItem, I always get null. There should always be an integer value here.
In spite of this, the page otherwise renders fine. Why can't I access the DataItem property of my RepeaterItems inside the Page_Load event handler?
Your Repeater doesn't databind until later in the page lifecycle. If you want to reference Repeater.Items[i].DataItem in a Page.Load handler, try to early-bind the Repeater first:
repeater.DataBind()
Its only available during databinding.
As everyone else has said, it's only available during databinding. You will need to wire up the OnItemDataBound event of the repeater. In its event handler, you can do this:
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
WhateverType Item = e.Item.DataItem as WhateverType;
break;
}
}
The data items only exist after the databinding process has taken place. The only thing that is preserved across postbacks in the repeater are the control properties that are serialized in viewstate. If you need the data in those items, you can do like pseudocoder said and databind earlier or if that is not possible you could write a utility function that takes a repeater data item and extracts it from the controls in your repeater (assuming you stored all the information you need in those controls somehow)
I have a doubt over the items available in a drop down list after postbacks.
I created a custom web server control deriving from DropDownList:
public class StateListControl : DropDownList
{
public StateListControl()
{
this.Items.Add(new ListItem("New York", "NY"));
this.Items.Add(new ListItem("Nebraska", "NE"));
this.Items.Add(new ListItem("Texas", "TX"));
}
}
I added the control to a page, the in the Page_Load event did the following:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StateListControl1.Items.Add(new ListItem("Michigan", "MI"));
}
}
Now this item will be added when there is no postback. I added button control with no attached event.
Now when I repeatedly click my button, the fourth entry is visible after every postback.
My doubt relates to where is the information saved that the drop down list has a fourth item, when that item is added only on first request and is still available after repeated postbacks?
On first page load, dropdownlist is populated with all four values and these are stored in the viewstate. When you post the page back, dropdownlist is initialized again and values are loaded from the viewstate. Since no changes are made in the server side load event to dropdownlist values, the values remain as the four entries in viewstate.
If you disable viewstate for the dropdown, you will see that only 3 entries are available when you postback, depending on whether the initialize code is run on every page load.
A control has a ControlState - similar to a ViewState - that retains the information during a post back.
ControlState vs. ViewState
When you add the new list item - it is added to the dropdownlist and saved in viewstate.
I have a checkbox that when clicked, calls a javascript program that calls grid.PerformCallback(), where grid is the client instance name of my ASPxGridView gridview. This gridview also has a custom callback method which databinds the table. However when i click my checkbox, instead of only performing callback on the gridview, my page does a full postback, which posts the form. How do i make it so that it only updates the gridview?
function toggle()
{
productGrid.PerformCallback();
}//end toggleExch()
<dx:ASPxGridView ClientInstanceName="productGrid" Width="100%" ID="productGrid" runat="server"
DataSourceID="ProductSQL" EnableCallBacks="true" OnCustomCallback="productGrid_OnCustomCallback">
</dx:ASPxGridView>
protected void productGrid_OnCustomCallback(object sender,
DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
{
System.Diagnostics.Debug.WriteLine("in postback");
productGrid.DataBind();
}//end productGrid_OnCustomCallback()
so basically the debug line is not printed and the page goes into full postback - how do i only postback and databind the grid? (i need to do more server side processing before databinding or directly binding from jquery is out of the question)
found the answer - should use iscallback instead of ispostback
Unfortunately, you did not post the aspx markup of the button. However, if this is the ASPxButton, make certain that its AutoPostback property is false ...
Given the following
public class MyControl : CompositeControl
{
private DropDownList myList;
protected override void CreateChildControls()
{
base.CreateChildControls();
myList = new DropDownList();
myList.AutoPostBack = true;
this.Controls.Add(myList);
if (!Page.IsPostBack)
{
myList.DataSource = MyBLL.SomeCollectionOfItems;
myList.DataBind();
}
}
}
I find that the items in the list persist properly, but when a different control is rendered and then this one is rendered again, the last selected item is not persisted. (The first item in the list is always selected instead)
Should the last selected item be persisted in ViewState automatically, or am I expecting too much?
I think this is a hidden ViewState issue. You create and bind a control in CreateChildControls. You should only create the control at this place. Move the binding code to the classes load event and use EnsureChildControls.
Here is the solution which is best recommended. It lies in understandng the Page Life Cycle correctly!! Postback Controls like Drop Down List restore their posted state (the selected item of a Drop Down List posted). It forgets its selected value because you are rebinding it in Page_Load event, which is after the Drop Down List has been loaded with posted value (because View State is loaded after Page_Init event and before Page_Load event). And in this rebinding in Page_Load event, the Drop Down List forgets its restored selected item. The best solution is to perform the Data Binding in the Page_Init event instead of Page_Load event.
Do something like the below...
Suppose Drop Down List name is lstStates.
protected void Page_Init(object sender, EventArgs e)
{
lstStates.DataSource = QueryDatabase(); //Just an example.
lstStates.DataTextField = "StateName";
lstStates.DataValueField = "StateCode";
lstStates.DataBind();
}
ASP.NET loads control's View State after Page_Init event and before Page_Load event, so Drop Down List's selectedIndex will not be affected, and you will get desired results magically!!
Using VS2005, ASP.Net 2.0, AjaxControlToolKit
I have a LinkButton in a Panel that contains an UpdatePanel with a GridView. The link button is outside the UpdatePanel. The OnClick event has this code:
protected void lnkOk_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdProductSearch.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}
Server.Transfer(Page.Request.RawUrl);
}
I need it to pass the selected values of the grid back to the Parent page in a post back. But all it does is close the Panel.
Any ideas why this is happening? or how can I achieve what I am trying to do?
Try putting everything inside the UpdatePanel. Sometimes this control messes up the page normal behavior.
I've seen several times that UpdatePanel control affect somehow (it's difficult to say where and how) the javascript of the page. For example, When you have the UpdatePanel declared as ChildrenAsTriggers="false" and declared AsyncPostBack triggers, if you have a Validation Summary inside, it never shows the validation errors done in the server.