Why is DataItem always null in the Page_Load handler? - asp.net

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)

Related

get selected index of databound list control on user control

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"

Repeater Control finding the object that raises the event

Inside repeater control HeaderTemplate i have some LinkButtons and Checkbox
I want to findout the object (Linkbutton or checkbox) that raises the event.
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch(e.CommandSource)
{
case LinkButton:some work here;
case CheckBox :some work here;
}
}
When i write such code i received error as
A switch expression or case label must be a bool,
char, string, integral, enum, or corresponding nullable type
How to achieve this?
As the error messages states you could use switch with bool, char, string, integral, enum or corresponding nullable type. In your case you want to compare types. This could be achieved with an if statement:
if (e.CommandSource is LinkButton)
{
}
else if (e.CommandSource is CheckBox)
{
}
First, I pretty sure the checkbox won't ever fire the Repeater_ItemCommand event, as it is not a Button (or something that inheriets from Button) as only buttons create the ItemCommand event inside a Repeater. You can set the CheckBox's AutoPostBack property to true and handle it's OnClick event, though you'll have to be careful to be able to figure out which CheckBox fired the event b/c all the CheckBoxes will have the same event handler in your code behind files and they won't have some of the nice information inside the EventArgs that events raised by the Repeater will have.
In addition, checking the control type in the ItemCommand event handler seems both inefficent and limiting. As your code would break if there were ever mulltiple controls of the same type in the Repeater row that needed different processing. For controls that will actually raise the ItemCommand event, setting either the CommandName or the CommandArgument property of button will allow you to uniquely identify the control that raised the event, without taking the performance hit of the type check, plus it will be more maintainable.
Use this code within the ItemCommand event handler:
switch(e.CommandName)
{
case "LinkButtonCommandName1":
.......
break;
case "LinkButtonCommandName2":
.......
break;
}

How to use a UserControl inside the EditTemplate of a ListVIew?

Here is the situation:
I have a ListView showing just a list of concatenated strings obtained from different field of the objects of the datasource.
A LinkButton (with CommandName="Edit") in each row
Event handlers for OnItemDataBound and OnItemEditing
A UserControl in EditTemplate.
Now the problem is, I don't know how to use Bind expression in the UserControl. I mean, how to populate this usercontrol when the linkbutton is clicked? (I tried capturing the control in the
OnItemEditing handler. But FindControl returned null, as that handler is called before going to edit mode.)
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
TheClass theControl = (TheClass)e.Item.FindControl("theControl)";
theControl.someProperty = "bla bla bla";
}
Finally got an answer from asp.net forum. The solution is:
Modify the UserControl so that, it supports DataBinding. To do that, implement the DefaultBindingPropertyAttribute. Details here.

Should a DropDownList within a CompositeControl remember selected item?

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

ASP.NET WebControl & Page - Adding controls (like TextBox) dynamically

I'm trying to create a custom server control (WebControl) with a text box.
I add asp.net textbox to the custom control in CreateChildControls override. In OnInit override I add event handler to TextBox.TextChanged.
Everything works, except that TextChanged never fires. I looked at viewstate and it looks like my textbox never saves its Text property in the viewstate. I've tried to set Text in various places, including constructor, but nothing works.
How can I get TextBox dynamically added to WebControl to save it's Text in viewstate and get TextChanged event to fire?
I would greatly appreciate an example of WebControl code behind with TextBox being added dynamically and TextChanged event being fired.
The dynamically created control must be created again in each post back, (the pageInit event is the better option) for the event to be fired.
BTW, if you want the TextChanged event to generate a postback you must also set the AutoPostback of the control to true.
fixed it. dynamic control must be created and added in Init event. It must be assigned an ID without special ASP.NET symbols ('$' or ':' inside custom ID will break things). All properties must be assigned after control is added to the controls tree.
here's a working example for Page codebehind:
private readonly TextBox _textBoxTest = new TextBox();
protected void Page_Init( object sender, EventArgs e )
{
this.form1.Controls.Add( _textBoxTest );
_textBoxTest.Text = "TestBoxTest";
_textBoxTest.ID = "TestBoxTestId";
_textBoxTest.TextChanged += this._textBoxTest_TextChanged;
}
void _textBoxTest_TextChanged( object sender, EventArgs e )
{
_textBoxTest.Text = "Worked";
}
for WebControl place init code in OnInit override
This will help you out. In short, you need to handle the viewstate for your Dynamically added control on your own.

Resources