I have a usercontrol DmsRegisterPod which is being output within a repeater which is itself, inside an UpdatePanel. I have an event on the DmsRegisterPod called OnUpdated which I'm subscribing to in the repeaters ItemDataBound event e.g:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
private void dmsRegisterPod_OnUpdated(object sender, EventArgs e)
{
this.DataBind();
}
The event is setup as such in the usercontrol:
public event EventHandler OnUpdated;
private void Updated(EventArgs e)
{
if (this.OnUpdated != null)
{
OnUpdated(this, e);
}
}
It's being raised in the Accept click handler:
protected void btnAccept_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
this.DealershipIRLink.dms_account_number = txtDmsNumber.Text;
this.DealershipIRLink.id_dealer_ir_link_status = DealerIRLinkStatus.DealerIRLinkStatusIdentifier.Approved;
this._irLinkService.UpdateDealershipIRLink(this.DealershipIRLink);
this.Updated(e);
}
}
However, the handler, OnUpdated is always null so the event never gets raised. It's as though the control is losing the event binding somehow. Can anybody see what I've done wrong here?
Register the event handler in ItemCreated instead of ItemDataBound which is triggered only when you databind the control and not on every postback(required):
protected void rptPendingDmsRequests_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
All other logic that depends on the datasource belongs to ItemDataBound:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
}
}
Related
I have a DataGrid with a ComboBox column and I need to add items to that combobox only when it get focus. I handle the event GotFocus, but still have a problem. In fact once I select an item from the dropdown the event will fire again. Any workaround on how to fix this issue.
private void CmbxGotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !this.returnedFocus)
{
//dowork
}
}
private void CmbxLostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
this.returnedFocus = cb.IsDropDownOpen;
}
}
}
I have a DataList and I am binding it in page load when it IS NOT a postback, but still I receive a null reference exception when I try to access the DataItem in the ItemCreated event, any suggestion?
protected void Page_Load(object sender, EventArgs e)
{
AppPath = MapPath(HttpContext.Current.Request.ApplicationPath);
MainDS.ReadXml(AppPath + FileName);
DataView MyDV = new DataView(MainDS.Tables[0]);
DataList1.DataSource = MyDV;
DataList1.DataBind();
}
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
Response.Write(e.Item.DataItem.ToString());
}
You need to check the item is ordinary item, not header or footer:
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Response.Write(e.Item.DataItem.ToString());
}
}
protected void lnk_Add_Click(object sender, DataListCommandEventArgs e)
{
Label id = (Label)e.Item.FindControl("lbl_PID");
Label lbl_P_Name = (Label)e.Item.FindControl("lbl_PN");
Image P_Image = (Image)e.Item.FindControl("Img");
LinkButton lnkbtn = (LinkButton)e.Item.FindControl("lnk_Add");
lnkbtn.Enabled = false;
}
I am using above method for Disabling a link button after it has been clicked once but the problem I am facing is that when ever I click on other link button(In other Row) the previous link button which was disable gets enable.
What I want is to disable a linkbutton until I don't enable it from any other event or method.
I suggest you to use ItemDataBound event of your Datalist
void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
var lnkbtn = (LinkButton)e.Item.FindControl("lnk_Add");
lnkbtn.Enabled = false;
}
}
Based on this link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx
Just bind your grid in If(!IsPostBack) at your page_load event
I have written this code, but the attribute is failed to be added to the markup. what is the problem? thanks
protected void Page_Load(object sender, EventArgs e)
{
PycDBDataContext db = new PycDBDataContext();
IEnumerable<seller_profile> profs = from rows in db.seller_profiles select rows;
ProfilesView.DataSource = profs;
ProfilesView.ItemCreated += new DataListItemEventHandler(ProfilesView_ItemCreated);
ProfilesView.DataBind();
}
void ProfilesView_ItemCreated(object sender, DataListItemEventArgs e)
{
e.Item.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightblue';");
}
What you really want is the ItemDataBound event and not the ItemCreated event.
Rewrite like this and you would be fine.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList ProfilesView;
PycDBDataContext db = new PycDBDataContext();
IEnumerable<seller_profile> profs = from rows in db.seller_profiles select rows;
ProfilesView.DataSource = profs;
ProfilesView.ItemDataBound += new DataListItemEventHandler(ProfilesView_ItemDataBound);
ProfilesView.DataBind();
}
}
private void ProfilesView_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor = 'lightblue';");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor = 'white';");
}
}
I had datalist and in datalist gridview and in gridview div and I wanted to find this div I did my code but error apeared (object refrence....) here (Techgr1.Attributes.Add("Class", "ff");
)
protected void Datalist_Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
string LanguageID = Globals.GetSuitableLanguage(Page);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Techgr1 = e.Item.FindControl("TechnologyGr") as HtmlGenericControl;
}
GridView gridfeature = (GridView)e.Item.FindControl("grid_features");
foreach (DataControlField column in gridfeature.Columns)
{
column.HeaderText = Globals.Translate(column.HeaderText, LanguageID);
Techgr1.Attributes.Add("Class", "ff");
}
}
Try this:
protected void Datalist_Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
string LanguageID = Globals.GetSuitableLanguage(Page);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Techgr1 = e.Item.FindControl("TechnologyGr") as HtmlGenericControl;
}
GridView gridfeature = (GridView)e.Item.FindControl("grid_features");
foreach (DataControlField column in gridfeature.Columns)
{
column.HeaderText = Globals.Translate(column.HeaderText, LanguageID);
if(Techgr1 != null)
{
Techgr1.Attributes.Add("Class", "ff");
}
}
}