I have bound a datapager control to a listview.
I would like to scroll to the first item of the listview control on the DataPager click. I think this should be done with javascript. It seems that the datapager does not allow that.
What options do I have? How can I scroll to a specific anchor when clicking on the DataPager?
you can use the basic html named anchor to scroll to a specific anchor.
You can use the javascript function scrollIntoView for that on client side or on "server side":
http://www.codeproject.com/KB/aspnet/ViewControl.aspx
Thanks Tim!
And for the lazy guys out there (just like me ;), here is the VB.NET equivalent.
It contains typo corrections and the new RegisterClientScriptBlock Method
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FocusControlOnPageLoad("Label1", Me.Page)
End Sub
Public Sub FocusControlOnPageLoad(ByVal ClientID As String, ByVal page As System.Web.UI.Page)
Dim csName As String = "ScrollViewScript"
Dim csType As Type = Me.GetType
Dim cs As ClientScriptManager = page.ClientScript
If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
Dim csText As New StringBuilder()
csText.Append("<script>function ScrollView(){")
csText.Append("var el = document.getElementById('" & ClientID & "');")
csText.Append("if (el != null){")
csText.Append("el.scrollIntoView();")
csText.Append("el.focus();}}")
csText.Append("window.onload = ScrollView;")
csText.Append("</script>")
cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
End If
End Sub
Related
I have a relatively simple ASP.NET problem (I should think) that regrettably I am unable to solve by myself. What I am trying to do is the following:
On a page I load a number of controls (Text Boxes) programmatically;
following this load the user should be able to select a value to load into the Textbox from a panel control that is added to the page following the click of a button
Once the panel is closed, the selected text from the panel should be loaded into the textbox
However, in the vb.net statements below when run the "test" string never makes it to the textbox - any help with resolving this would be greatly appreciated.
Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Controls_Load()
End Sub
Public Sub Controls_Load()
Dim ttf_tb As New TextBox With {.ID = "ttf_tb"}
Master_Panel.Controls.Add(ttf_tb)
Dim ttf_button As New Button
Master_Panel.Controls.Add(ttf_button)
AddHandler ttf_button.Click, AddressOf TTF_BUTTON_CLICK
End Sub
Public Sub TTF_BUTTON_CLICK(sender As Object, e As EventArgs)
Dim str As String = sender.id
Dim panel As New Panel
panel.ID = "TTF_Panel"
panel.Width = 300
panel.Height = 300
Master_Panel.Controls.Add(panel)
panel.BackColor = Drawing.Color.Black
panel.Style.Add(HtmlTextWriterStyle.Position, "absolute")
panel.Style.Add(HtmlTextWriterStyle.Left, "200px")
panel.Style.Add(HtmlTextWriterStyle.Top, "100px")
panel.Style.Add(HtmlTextWriterStyle.ZIndex, "100")
Dim CL_Button As New Button
CL_Button.ID = "TTF_Close_" & Replace(str, "TTF_Button_", "")
panel.Controls.Add(CL_Button)
AddHandler CL_Button.Click, AddressOf TTF_Close_Button_Click
End Sub
Public Sub TTF_Close_Button_Click(sender As Object, e As EventArgs)
Dim ttf_tb As TextBox = Master_Panel.FindControl("ttf_tb")
ttf_tb.Text = "Test"
Dim panel As Panel = FindControl("TTF_Panel")
Master_Panel.Controls.Remove(panel)
End Sub
End Class
I think you need to re-create your controls in the Page_Init method. It's been a while since I've done web forms but I think it's something like:
When a user clicks the button a post back is fired. This re-creates a new instance of your class, creates the controls on the page, assigns any form values then calls your Page_Load event.
The problem is you are creating your controls too late, so the forms values are never assigned correctly.
You should create / recreate your dynamic controls in the Init event:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Controls_Load()
End Sub
This should allow you to maintain their state across PostBacks.
For more information about this topic, see the MSDN article on the ASP.NET Page Life Cycle.
This has got to be the simplest question ever. Im trying to use a link button with the "postbackurl" to post a value to another page. It works with a button, but it is blank with a linkbutton. I dont want to use querystring. Any ideas?
default.aspx:
<asp:LinkButton ID="LinkButton" postbackurl="~/2.aspx" Text="ThisIsATest" runat="server">ThisIsATest</asp:LinkButton>
2.aspx:
Dim test = Request.Form("LinkButton")
Response.Write(test)
Try with this code
var text = ((LinkButton)PreviousPage.FindControl("LinkButton")).Text;
Use PreviousPage.FindControl to get the control reference
Dim lb As LinkButton = DirectCast(PreviousPage.FindControl("LinkButton"), LinkButton)
If lb IsNot Nothing Then
Response.Write(test.ID) ' display linkbutton ID
Response.Write(test.Text) ' display linkbutton Text
End If
Try:
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton1.Click
Response.Redirect("2.aspx?id=" + somestring)
End Sub
I've created some dynamic controls on page load and added an event handler to handle the click event of a dynamic link button. Within the sub of the click event handler, I need to reference some other (non-dynamic) controls on the page and change their value. However, I get a null reference exception - object not set to an instance of an object - each time I try to reference a control on the page (in this case label1). What am I doing wrong in creating these dynamic controls or with my event handler? Thanks!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the data to populate the controls
Dim oMySqlData As New MySqlDataProvider
PlaceHolder1.Controls.Add(CreateExistingNotesHTML(oMySqlData.GetParentNotes("104628"), oMySqlData.GetChildNotes("104628")))
End Sub
Public Sub OnCommentClick(ByVal sender As Object, ByVal e As EventArgs)
'The event handler for the link buttons
Label1.Text = "You clicked " & DirectCast(sender, LinkButton).ID
End Sub
Public Function CreateExistingNotesHTML(ByVal dtParent As DataTable, ByVal dtChild As DataTable) As HtmlGenericControl
'The routine that creates the dynamic controls
Dim divContainer As New HtmlGenericControl("div")
For Each drParent As DataRow In dtParent.Rows()
divContainer.Controls.Add(WriteNote(drParent.Item("NoteId").ToString(), drParent.Item("UserName").ToString(), drParent.Item("ItemNote").ToString, CDate(drParent.Item("InsertDate")), "note"))
Next
Return divContainer
End Function
Private Function WriteNote(ByVal NoteId As String, ByVal UserName As String, ByVal ItemNote As String, ByVal InsertDate As DateTime, ByVal DivClass As String) As HtmlGenericControl
Dim div As New HtmlGenericControl("div")
div.ID = "d" & NoteId
div.Attributes.Add("class", DivClass)
div.Controls.Add(New LiteralControl(" ยท "))
'Add the dynamic link buttons
Dim lnkComment As New LinkButton
lnkComment.ID = "l" & NoteId
lnkComment.Text = "Comment"
lnkComment.Style("Text-decoration") = "none"
AddHandler lnkComment.Click, AddressOf oNotes.OnCommentClick
div.Controls.Add(lnkComment)
Return div
End Function
With this line
Dim oNotes As New EbayItemNotes
AddHandler lnkComment.Click, AddressOf oNotes.OnCommentClick
You are handling this event outside of your page and within the EbayItemNotes class, how does this class know anything of Label1 that resides in your page?
Could it be that you need to pass your label in as well..
Dim oNotes As EbayItemNotes = new EbayItemNotes(label1)
inside EbayItemNotes constructor
public sub New(lbl as Label) //store this label for use in event handler..
I'm not sure if this is at all possible (there may be a different way to acheive it) but is there a way to iterate though all hyperlinks on Page_PreRender and if the NavigateUrl matches the file name then I can add a class to the link to show this as the active page.
Or even better, iterate through all hyperlink NavigateUrls within a certain DIV.
I can do it individually but that would take too long as there are so many links and be too hard to manage:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim filePath As String = System.Web.HttpContext.Current.Request.Path
If filePath = "/" & hMembership.NavigateUrl Then
hMembership.CssClass = "active"
End If
End Sub
You can do something like this in the Page_PreRender:
Dim filePath As String = System.Web.HttpContext.Current.Request.Path
For Each Control As Control In Me.Form.Controls
If TypeOf (Control) Is HyperLink Then
With TryCast(Control, HyperLink)
If .NavigateUrl = filePath Then
.CssClass = "active"
End If
End With
End If
Next Control
Can I get some help posting across different pages from a custom control?
I've created a custom button that raises it's own click event through the following code:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Const EventName As String = "button_click"
Const ArgName As String = "__EVENTARGUMENT"
If Page.IsPostBack _
AndAlso Request.Params IsNot Nothing _
AndAlso Request.Params(ArgName).Trim = EventName Then
Me.OnClick(Me.this_button, New EventArgs)
Else
Me.this_button.Attributes.Add("OnClick", Page.ClientScript.GetPostBackEventReference(Me.this_button, EventName))
End If
End Sub
How would I go about modifying this to let me post to a different page?
I'd like it to act as close as possible to the System.Web.UI.WebControls.Button property PostBackUrl.
You can use Cross Page Postbacks.
You can also use the WebForm_DoPostBackWithOptions js method to postback the current page to another page.