RadioButtonList Inside Repeater OnSelectedIndexChanged Not Firing - asp.net

I have a RadioButtonList inside a Repeater. I have AutoPostback set to "true" and the OnSelectedIndexChanged defined. When I selected a different radiobutton in my list the page does postback, but my defined OnSelectedIndexChanged event is not catching or firing. Not sure what I am missing. Here is my markup and codebehind:

Use the repeater's itemcreated event to bind your eventhandler:
protected void Repeater!_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
e.item.FindControl("TaskRadioButtonList").SelectedIndexChanged += new EventHandler(TaskRadioButtonList_OnSelectedIndexChanged);
}
}

Related

Find Linkbutton in Repeater Itemtemplate when on page load

I'm using a LinkButton in Repeater ItemTemplate but if my LinkButton is NULL then I don't want to show this Linkbutton. Can I control this LinkButton on Page Load?
<asp:Repeater ID="rptSlider" runat="server" >
<ItemTemplate>
<li>
.....
<asp:LinkButton ID="lb_url" PostBackUrl='<%#Eval("button_url") %>' runat="server">Go</asp:LinkButton>
...
</li>
</ItemTemplate>
Try this way in page-load event
LinkButton linkButton= (LinkButton)Repeater1.Items[0].FindControl("lb_url");
linkButton.Visible = false;
but I will suggest to use ItemDataBound event to set visibility of link-button.
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
var linkButton= e.Item.FindControl("lb_url") as LinkButton;
// set link-button visibility
}
}
You may check the same under ItemCommand. Please check whether the following code works or not.
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string url=((LinkButton)e.CommandSource).Text;
if (string.IsNullOrEmpty(url))
((LinkButton)e.CommandSource).Visible=false;
else
((LinkButton)e.CommandSource).Visible=true;
}

Referencing webcontrols in an UpdatePanel in EditItemTemplate of a Gridview

I am getting null reference exception when trying to reference a web control which is within an AJAX UpdatePanel, which itself is within and EditItemTemplate of a Gridview. I am doing this in the RowDataBound event handler of the gridview as follows:
protected void gvIntakes_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = (DataRowView)e.Row.DataItem;
UpdatePanel up = (UpdatePanel)e.Row.FindControl("Status_UpdatePanel"); //**** up is showing null on debug
Panel pnlSelectStaff = (Panel) up.FindControl("pnlSelectStaff") ;
Panel pnlSchedule = (Panel)up.FindControl("pnlSchedule");

Respond to Button Events inside Repeater ItemTemplate

so i have removed the datasouce and just have DataBind() then my page is still blowing and refreshing the page and not in a EDIT mode.
what i am trying to do is when the user click on Edit button then make it inline editing the repeater row.
END UPDATE
onItemCommand i have added DataBind()
rpt.DataSource = mydatasource;
rpt.DataBind();
after i do that my page is not in edit mode and it blow away and everyting is refreshed
i have on page_load
if (!IsPostBack)
{
rpt.DataSource = mydatasource;
rpt.DataBind();
}
end update
I've used repeaters many times without problems but something is going on here. I have a repeater and I'm subscribing to the itemDatabound event, But when i click the button (which is a linkbutton inside my repeater itemtemplate) it does not go to the ItemDataBound
<asp:Repeater ID="rpt" runat="server" OnItemCommand="rpt_OnItemCommand" OnItemDataBound="rpt_OnItemDataBound">
<ItemTemplate>
<li>
<asp:Label ID="Label" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CommandArgument='<%# Eval("MyID") %>'
Text='<%# Eval("Title") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
}
protected void rpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == EditIndex)
{
// never come to this line.... after the user click on LinkButton
}
}
}
Don't know if this helps but you must call DataBind() in order for for the OnItemDataBound event to fire. Also my guess is you are trying to set the EditIndex in the OnItemCommand and then use the value in the OnDataBind event. The events fire in the order OnItemDataBound then OnItemCommand so the EditIndex wouldn't be correct anyway in that situation.
Add rpt.DataBind to the OnItemCommand. This workded when I tried it from your code, NOTE that you will be binding twice if you aren't using !IsPostBack for original data bind.
rpt.DataSource = strings;
if (!IsPostBack)
{
rpt.DataBind();
}
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
rpt.DataBind();
}
You must change your rpt_OnItemCommand function.
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
else if (e.CommandName == "Complete")
{
// your function goes here
}
}
Why do you think that the ItemDataBound is raised when you click your LinkButton? ItemDataBound is only fired when Repeater.DataBind() is called.
Actually the repeater's ItemCommand event is raised instead.
I'm a little confused, but from the example above it looks like you've got it backwards. The button click would never fire the ItemDataBound event. The ItemDataBound event is only called after each item is bound to the repeater.
The button click should fire the ItemCommand event however, and if that's not happening I would check to make sure you've actually assigned the ItemCommand handler, and also make sure that the command name is valid.
On a side note, this behavior can also happen when the repeater is bound at every postback. Make sure that you're binding the repeater when !Page.IsPostBack.

Setting selectedvalue for a dropdownlist in GridView

I have a dropdownlist in my Gridview and I am binding a datasource to the gridview.
Though all the records are displaying properly the dropdown value is not selected.
How do I set something like
<%# Bind("Country") %> for a dropdownlist in the Gridview in ASP.net.
Thanks
You can hook into the RowDataBound event for the grid view, find the control and set the value.
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
dropdownList .SelectedIndex = SET_VALUE_HERE;
}
Setting DropDownList value from a Datasource should be like:
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
}
}

How to access datasource fields in an ASP.NET Repeaters ItemDataBound event?

I have a Repeater control that is being bound to the result of a Linq query.
I want to get the value of one of the datasource's fields in the ItemDataBound event, but I'm not sure how to do this.
Depending on the DataSource...
If your DataSource is a DataTable, then your DataItem contains a DataRowView:
protected void rptMyReteater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button b = e.Item.FindControl("myButton") as Button;
DataRowView drv = e.Item.DataItem as DataRowView;
b.CommandArgument = drv.Row["ID_COLUMN_NAME"].ToString();
}
}
You can use: e.Item.DataItem.
Example: Repeater.ItemDataBound Event
// This event is raised for the header, the footer, separators, and items.
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if (((Evaluation)e.Item.DataItem).Rating == "Good")
{
((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
}
}
}
The data that is used for the current item can be found from the EventArgs.
RepeaterItemEventArgs e
e.Item.DataItem

Resources