VB.NET display label text only on default.aspx page only - asp.net

Have a message that currently displays on every page of our website. I'd like to change this so that it only appears on the homepage which is default.aspx. Please let me know what I can try. the below code lives in the code behind of the master page of the site. We use vb.net but if someone can write it in C# I can convert it.
Previous code that displays label text on every page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End Using
End Sub
Code I have written that isn't working, what am I missing?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
Dim appPath As String = Request.PhysicalApplicationPath
If appPath = "/Default.aspx" Then
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End If
End Using
End Sub

Ended up wrapping the control in a panel with ID of Message and using the following code to display only if on the homepage.
If Page.Title = "Home" Then
Message.Visible = True
Else
Message.Visible = False
End If

Related

Jump to Page in C1PrintDocument

I have a form in my application that is used for previewing a report. It has a C1Ribbon at the top which contains navigation buttons, and a C1PrintPreview displaying in a PreviewPane.
I want the navigation buttons in the ribbon (first, previous, next, last), as well as a text box in which to enter a specific page number, to navigate through the preview of the report accordingly. So far, all the documentation and samples I've found have dealt only with adding hyperlinks directly to the report itself...so I'm having a hard time adapting it to my use.
Below is what I have so far...I don't get any build or run-time errors, it just doesn't do anything.
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Dim nextPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Next)
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
Dim lastPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Last)
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
Dim prevPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Previous)
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
Dim firstPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.First)
End Sub
Private Sub Navigation_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPageNum.KeyDown
If e.Modifiers = Keys.Enter Then
Dim setPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Absolute)
'setPage. = CInt(txtPageNum.Text)
If setPage.PageNo = 0 Then
'what to do if number entered is not a page in document
End If
End If
End Sub
I realize it's likely just because even though I make a C1LinkTargetPage, I don't tell the app what to do with it after that. But I'm not sure how to go about doing that - it's not like there's a "jumptopage" method for the C1PrintPreview (wish it were that easy). Buttons in the ribbon don't have a hyperlink property, so I can't set that when the form loads as in all the samples I found. Not sure where to go from here...
Also I don't even know how I'm supposed to be able to use the Absolute PageJumpTypeEnum...PageNo is read only.
Thank you!
UPDATE 2/25:
I learned that I should be dealing with properties of the Preview Pane...not the C1PrintDocument. With the code below, the navigation buttons and specifying a page number work. My only problem now then is displaying the current page in that page number box. With what I have, PreviewPane.CurrentHistoryEntry just goes to 1 (even if I was on a page other than 1 before-hand).
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
PreviewPane.DoGoNextPage()
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
Dim lastPage As DocumentLocation = New DocumentLocation(report.Pages(report.Pages.Count - 1))
PreviewPane.GotoDocumentLocation(lastPage)
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
PreviewPane.DoGoPreviousPage()
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
Dim firstPage As DocumentLocation = New DocumentLocation(report.Pages(0))
PreviewPane.GotoDocumentLocation(firstPage)
End Sub
Private Sub Navigation_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPageNum.KeyDown
If e.KeyValue = Keys.Enter Then
Dim pageNum As Integer = CInt(txtPageNum.Text)
If (pageNum > 0) And (pageNum <= report.Pages.Count) Then
Dim setPage As DocumentLocation = New DocumentLocation(report.Pages(pageNum - 1))
PreviewPane.GotoDocumentLocation(setPage)
Else
txtPageNum.Text = PreviewPane.CurrentHistoryEntry.ToString
End If
End If
End Sub
They key was "PreviewPane.StartPageIdx"
txtPageNum.Text = (PreviewPane.StartPageIdx + 1).ToString

Failure to update textbox.text

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.

VB.net/Asp.net loading a control programatically with a public property to be set

I have a vb.net asp application where I'm loading a control (from another control on a page). I want to set a variable between the two controls when it loads.
This is where I load the control:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
phCompanies.Controls.Add(AuditControl)
End Sub
On the control that's being loaded i've exposed the item i want to change as a property
Public Property resultType() As String
Get
Return m_resultType
End Get
Set(ByVal value As String)
m_resultType = value
End Set
End Property
Basically all it is doing is setting a parameter for my table adaptor
Public Sub Load_Data()
dtblog = New dsDECorp.ClientInfoDataTable
dtblog = tablog.GetAudits(m_resultType)
For Each rClient As dsDECorp.ClientInfoRow In dtblog
CID = rClient.ClientID
ClientName = rClient.CompanyName
Next
dtblog.Dispose()
End Sub
How do I pass the parameter through the property from the first control to the second when it loads?
Thanks
Tom
I'm a C# guy so forgive me if I make any syntax errors but the following should work for you:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
Dim AuditControlType As Type = AuditControl.GetType()
Dim AuditField As FieldInfo = AuditControlType.GetField("resultType")
AuditField.SetValue(AuditControlType, "Your Value")
phCompanies.Controls.Add(AuditControl)
End Sub

User enters code into TextBox, code gets added to URL (using session)

I'm using ASP.net 4.0 VB :)
I am using a session variable to add a user entered code into the url of each page. I can get the code to show up at the end of the page's URL that my textbox is on, but what do I add to every page to make sure that the session stays at the end of every URL that person visits during their session? This is the code from the page that the user enters their user code.
Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles IBTextBoxButton.Click
Session("IB") = IBTextBox.Text
Dim IB As String = Session("IB")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ProductID.Value = Request.QueryString("id")
If Session("IB") Is Nothing Then
tab4.Visible = "False"
Else
tab4.Visible = "True"
End If
End Sub
This is what I have in the page load of one of the other pages. What else do I add to make sure that variable is added to the URL of that page?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim IB As String
IB = Session("IB")
End Sub
string url = Request.Url.ToString();
string newUrl = url + url.Contains("?") ? "&" : "?" + "ib=" + Server.UrlEncode(IBTextBox.Text);
Response.Redirect(newUrl);
return;
The approach I might use would be to create a base page class that all of your pages can inherit. The base page would then inherit the System.Web.UI.Page.
Within your base page class, create a property for IB and also handle the page load event.
In that event, check if the QueryString has the IB parameter in it. If it does, set the property to the value in the parameter.
Private _IB As String
Public Property IB() As String
Get
Return _IB
End Get
Set(ByVal value As String)
_IB = value
End Set
End Property
Public Function GetIB(ByVal url As String) As String
If Not(_IB = String.Empty) Then
If (url.Contains("?")) Then
Return "&IB=" & _IB
Else
Return url & "?IB=" & _IB
End If
Else
Return url
End If
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (String.IsNullOrEmpty(Request.QueryString("IB"))) Then
_IB = Request.QueryString("IB")
End If
End Sub
Finally in your markup you would need to place something like the following at the end of all of your links:
next page
I threw this code into the Master Page to make sure that every page knows whether or not the Session is there. Thanks for all the help everyone!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Session("IB") Is Nothing Then
IBText.Visible = True
IBTextBox.Visible = True
IBTextBoxButton.Visible = True
Else
IBText.Visible = False
IBTextBox.Visible = False
IBTextBoxButton.Visible = False
lblIB.Visible = True
lblIB.Text = "Welcome, " + Session("First_Name") + " " + Session("Last_Name")
End If
End Sub

problem with asp textbox control

i have a textbox control in asp.net. textbox have a search button beside it. On clicking the search button i redirect to new page with the value in textbox. the new page also hase textbox and button beside it. I set the value sent from previous page to the textbox on new page. If i change the value on new page and click the search button it should take the new value. But it takes the previous value.
on page load method wrote the following code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End Sub
On button click following code
Protected Sub bsrcnew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsrcnew.Click
Dim s As String
s = txtsrch.Text
If (flag.Equals(0)) Then
Response.Redirect("newSearch.aspx?str1=" + s)
ElseIf (flag.Equals(1)) Then
Response.Redirect("termsnew.aspx?str1=" + s)
End If
end sub
can any1 tell me how do i get changed value in textbox?
Try assigning it in a
If(!IsPostBack)
{
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
}
On buttonclick its again assigning the value from query string.
Use IsPostBack to set your text box only on the first run in the second page. Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End If
End Sub

Resources