Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Server Control Inheriting from GridView Events Not Firing

Status
Not open for further replies.

gav1ns

Programmer
Jun 11, 2007
2
GB
Afternoon all,

Apologies if this is in the wrong forum.

I have created a server control which inherits from GridView,and essentially freezes the header and scrolls the contents of the grid as opposed to Pagination.

This grid lives in side an update panel which is refreshed asyncronously.

This all works fine,I click Update this puts the Grid into Edit Mode, but when I click Cancel, ie to set EditIndex to -1 this event doesn't fire but alternatively clears the entire row , until I rebind the grid.

The Postback seems to occur after initial load but after that the events do not hit my break points even though the page posts back.

I suspect i have to reattach events somewhere but can not find nor work out how to achieve this, Delegates maybe? and do I have to attach every possible event that I may wish to use.

Code below


[ToolboxData("<{0}:ScrollingGridView runat=server></{0}:ScrollingGridView>")]
public class ScrollingGridView : GridView, IPostBackDataHandler
{

private int ScrollPosition
{
get
{
if (ViewState["ScrollPosition"] != null)
{
return Convert.ToInt32(ViewState["ScrollPosition"].ToString());

}
else
{
return 0;
}

}
set
{
ViewState["ScrollPosition"] = value;

}

}
[Browsable(true)]
public int DivWidth
{
get
{
if (ViewState["DivWidth"] != null)
{
return Convert.ToInt32(ViewState["DivWidth"].ToString());
}
else
{
return 0;
}

}
set
{
ViewState["DivWidth"] = value;

}

}

[Browsable(true)]
public int DivHeight
{
get
{
if (ViewState["DivHeight"] != null)
{
return Convert.ToInt32(ViewState["DivHeight"].ToString());
}
else
{
return 0;
}

}
set
{
ViewState["DivHeight"] = value;

}

}

#region IPostBackDataHandler Members

public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{

string postedValue = postCollection[this.ClientID + "_scroll"];

if (!String.IsNullOrEmpty(postedValue))
{

int presentValue = this.ScrollPosition;

this.ScrollPosition = Convert.ToInt32(postedValue);

return !presentValue.Equals(this.ScrollPosition);
}
return false;

}

protected override void OnInit(EventArgs e)
{
if (this.Page != null)
{
this.Page.RegisterRequiresPostBack(this);

}

}

protected override void OnLoad(EventArgs e)
{

}


protected override void OnPreRender(EventArgs e)
{

string key;
ScriptManager sm = ScriptManager.GetCurrent(this.Page);
sm.RegisterAsyncPostBackControl(this);
StringBuilder script = new StringBuilder();

if (sm.IsInAsyncPostBack)
{

this.HeaderRow.Style.Add("position", "relative");
this.HeaderRow.Style.Add("z-index", "10");
this.HeaderRow.Style.Add("top", this.ScrollPosition.ToString());


key = String.Format("{0}_setScrollPos", this.ClientID);
script = new StringBuilder();
script.AppendLine(" setScrollPos('" + String.Format("{0}_scroll", this.ClientID) + "','" + String.Format("{0}_div", this.ClientID) + "');");
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), key, script.ToString(), true);

if (sm != null)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), key, script.ToString(), true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), key, script.ToString(), true);
}


}
else
{

key = "ScrollingGridView";
script.AppendLine("function saveScrollPos(whereID,whatID){");
script.AppendLine("document.getElementById(whereID).value = document.getElementById(whatID).scrollTop;");
script.AppendLine("}");
script.AppendLine("function setScrollPos(whereID, whatID){");
script.AppendLine("document.getElementById(whatID).scrollTop = (document.getElementById(whereID).value.length > 0) ? document.getElementById(whereID).value : 0;");
script.AppendLine("}");

if (sm != null)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), key, script.ToString(), true);
ScriptManager.RegisterHiddenField(this.Page, String.Format("{0}_scroll", this.ClientID), this.ScrollPosition.ToString());
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), key, script.ToString(), true);
this.Page.ClientScript.RegisterHiddenField(String.Format("{0}_scroll", this.ClientID), this.ScrollPosition.ToString());
}

this.HeaderRow.Style.Add("position", "relative");
this.HeaderRow.Style.Add("z-index", "10");
this.HeaderRow.Style.Add("top", this.ScrollPosition.ToString());

key = String.Format("{0}_setScrollPos", this.ClientID);
script = new StringBuilder();
script.AppendLine(" setScrollPos('" + String.Format("{0}_scroll", this.ClientID) + "','" + String.Format("{0}_div", this.ClientID) + "');");
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), key, script.ToString(), true);

if (sm != null)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), key, script.ToString(), true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), key, script.ToString(), true);
}

}

}

protected override void Render(HtmlTextWriter writer)
{
if (this.Page != null)
{
this.Page.VerifyRenderingInServerForm(this);
}

this.PrepareControlHierarchy();

if (!this.DesignMode)
{
if (String.IsNullOrEmpty(this.ClientID))
{
throw new HttpException("ScrollingGridView must be parented!");
}
writer.AddAttribute(HtmlTextWriterAttribute.Id, String.Format("{0}_div", this.ClientID), true);
writer.AddAttribute("onScroll", "saveScrollPos('" + String.Format("{0}_scroll", ClientID) + "', '" + String.Format("{0}_div", this.ClientID) + "');");
writer.AddStyleAttribute(HtmlTextWriterStyle.MarginTop, "0px");
writer.AddStyleAttribute(HtmlTextWriterStyle.OverflowY, "auto");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, this.DivHeight.ToString() + "px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.DivWidth.ToString() + "px");
writer.RenderBeginTag(HtmlTextWriterTag.Div);

}

this.RenderContents(writer);


if (!this.DesignMode)
{

writer.RenderEndTag();

}

}

#endregion



#region IPostBackDataHandler Members


public void RaisePostDataChangedEvent()
{
this.DataBind();
}

#endregion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top