I have a web user control with a repeater and inside the repeater i have a dynamic number of radiobuttons, On page_load i am trying to call the onDataItemBound event of the repeater to find the radiobuttons and binding the oncheckedchanged event to a method in my code but cant get it to work, here is my code:
protected void Page_Load(object sender, EventArgs e)
{
Repeater_Select.ItemDataBound += Repeater_Select_OnItemDataBound;
}
protected void Repeater_Select_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
customRbtn.CheckedChanged += RadioButton_Select_OnCheckedChanged;
}
}
I've managed to solve my problem, the problem was that i have another method being called before this OnItemDataBound which called DataBind() on the repeater, so I simply reversed the order and put the call for OnItemDataBound before the other method like so:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRadioOnChecked();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetFeaturePackData();
}
}
private void BindRadioOnChecked()
{
foreach (RepeaterItem item in Repeater_Select.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CustomRadioButton customRbtn = (CustomRadioButton)item.FindControl("RadioButton_Select");
customRbtn.CheckedChanged += RadioButton_Select_OnCheckedChanged;
}
}
}
Now it's inside Page_Init so fires before Page_Load
Related
I have a Repeater which has it's data set up on Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}
}
When I come to save my data on postback, myRepeater.Items contains zero elements when I know there are several.
protected void btnSave_Click(object sender, EventArgs e)
{
....
// why does this contain zero elements>
foreach (RepeaterItem item in myRepeater.Items)
{
Any suggestions as to what may be the issue?
In this case, binding the repeater even on postback fixed the issue
protected void Page_Load(object sender, EventArgs e)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}
I want to change my button text on page load after retrieving the list view values.
For example,
<asp:Label ID="favouriteLabel" runat="server" Text='<%# Eval("favourite") %>' />
If this label value is 1, the button will change to Favourited.
I have retrieved the list view values by binding the listview
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label activity = (Label)e.Item.FindControl("favouriteLabel");
activityID = activity.Text;
}
}
then, I get the activityID and do a simple if-else check on the page load
protected void Page_Load(object sender, EventArgs e)
{
if (activityID == "1")
{
Button4.Text = "Favourited";
}
else
{
Button4.Text = "Favourite";
}
}
However it does not work. Anybody?
Do that inside a PostBack check in the load event, for instance:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (activityID == "1")
{
Button4.Text = "Favourited";
}
else
{
Button4.Text = "Favourite";
}
}
}
Read more about postback here
Page_Load happens before your ItemDataBound event so the activityId you are looking at in the Page_Load will never be 1.
Just put the code you have in the Page_Load into the ItemDataBoundEvent
I have a Datalist that has a delete button in it , I want to check if the user is admin, shows this button for each item and if not dont show that. This is my ItemDataBound, my problem is that for each item it connects to database and check if user is admin or not and I am worried about getting slow for large number of items. How can I fix this problem?
protected void GroupMembersDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton deletButton = (LinkButton)e.Item.FindControl("DeletFromGroup");
// here first connecet to database to get group name
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
// here connect to database and find groups admin ID and checks that are the same or not ?
BusinessLayer.Group_Table GObject = new BusinessLayer.Group_Table(GroupID);
if (Convert.ToInt32(Session["ID"].ToString()) == GObject.Id)
{
deletButton.Visible = true;
}
else
{
deletButton.Visible = false;
}
}
}
Edit solution:
private BusinessLayer.Group_Table GObject;
private int GroupID;
protected void Page_Load(object sender, EventArgs e)
{
try
{
int ID = Convert.ToInt32(Session["ID"].ToString());
GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
}
catch( Exception ee )
{
Response.Redirect("~/Home.aspx");
}
if (!IsPostBack)
{
getdata();
}
}
You could initialize a field in the class in Page_Load or the DataList's DataBinding event. Then you can access this field from ÌtemDataBound.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
DataBindDataList(); // code that calls DataBind
}
}
private BusinessLayer.Group_Table GObject;
Another way is to store this value in the Session.
i have a gridview with tempaltefield buttons,
i want to create a session with value of a cell in selected button row ,
can anyone help me i tryed this but didnt work:
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
Session["mysession"] = GridView1.SelectedRow.Cells[1].Text;
}
First of all, if it's just a imagebutton in a templatefield, actually you don't select de row. This line will problably throw an exception because SelectedRow is null.
But if you are using a command to select, that's correct. Maybe your event (ImageButton1_Click1) is not assigned to your image (OnClick).
You can try something like this:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Add and event RowDataBound
grvGrid.RowDataBound += new GridViewRowEventHandler(grvGrid_RowDataBound);
}
catch
{
//...throw
}
}
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
//...
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add an ImageButton foreach row in GridView
ImageButton ibtImageAlt = new ImageButton();
ibtImageAlt.ImageUrl = "App_Images/y.gif";
//ImageButton's ID will be the index of the row
ibtImageAlt.ID = e.Row.RowIndex.ToString();
ibtImageAlt.ForeColor = System.Drawing.Color.White;
ibtImageAlt.Font.Overline = false;
ibtImageAlt.Click += ibtImageAlt_Click;
}
}
catch
{
//...throw
}
}
protected void ibtImageAlt_Click(object sender, EventArgs e)
{
try
{
//Catch the ImageButton ID and the row in GridView
//An example to catch the value of the row selected by the ImageButton
Int32 intIndexRow = Convert.ToInt32(((ImageButton)sender).ID);
String strTest = grvGrid.Rows[intIndexRow].Cells[0].Text;
}
catch
{
//...throw
}
}
protected virtual void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
this.list = (DropDownList)e.Item.FindControl("edit_list");
if (list != null)
{
list.SelectedIndexChanged += new EventHandler(List_SelectedIndexChanged);
}
}
List is assigned, but selectedIndex eventHandler won't work
if i make RepairsStateList.BackColor = Color.Black; it is working
protected void List_SelectedIndexChanged(object source, System.EventArgs e)
{
Response.Write("<script>alert('vv') </script>");
}
AutoPostBack property of this dropdown must be set to true...
than you code must be
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e) {
// get reference to the row
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
// Get the reference of this DropDownlist
DropDownList dropdownlist1 = (DropDownList) gvr.FindControl("dropdownlist1");
}
Edit
Replace this line with
this.list = (DropDownList)e.Item.FindControl("edit_list");
this
DropDownList list = (DropDownList)e.Item.FindControl("edit_list");
if (list != null)
{
list.SelectedIndexChanged += new EventHandler(List_SelectedIndexChanged);
}