How to bind the page title to the datasource - asp.net

I need to bind title to the datasource so the web page title will show depends on the data in datasource.
When i'm using the Formview and the hiddenfield to get the data from datasource the code cannot compile
How to create a script that the pagetitle can be display based on the data in datasource?
<Script runat = "server">
Protected sub Page_load(Byval sender as Object, Byval e As System.eventargs)
Title = Hiddenfield1.value
End sub
</Script>
<asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value= '<%#eval ("PageTitleConstruct") %>' />
</ItemTemplate>
</asp:FormView>

Try this
Sub FormView2_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim HiddenField1 As HiddenField = CType(FormView2.FindControl("HiddenField1"), HiddenField)
Page.Title = HiddenField1.Value;
End Sub

In the Page_Load function. You'll have to get the value earlier than you are though.
Page.Title = value here.

You should be able to listen for the ItemCreated event, and set the title in that callback.

I have tried the script, but when using the
FormView2_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim HiddenField1 As HiddenField = CType(FormView2.FindControl("HiddenField1"), HiddenField)
Page.Title = HiddenField1.Value;
End Sub
The page.title value only nothing
But correct if i change the Event to
Private Sub FormView2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView2.Load
Dim hiddenfield1 As HiddenField = CType(FormView2.FindControl("hiddenfield1"), HiddenField)
Page.Title = hiddenfield1.Value
End Sub

Related

vb.net TextBox submit not changing

I've got a legacy VB.Net app that I'm modifying in VS 2010; here's the relevant code:
<form id="form1" runat="server">
<asp:GridView ID="gvCommentsEdit" runat="server" ShowHeader="False">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
*** On
<asp:Label ID="lblTimestamp" runat="server" Text='<%# Bind("comment_date") %>'></asp:Label>
<asp:Label ID="lblUpdateBy" runat="server" Text='<%# Bind("update_by") %>'></asp:Label> commented:<br />
<asp:TextBox ID="txtEditCommentPopup" runat="server" Columns="55" Rows="10" Text='<%# bind("text") %>' TextMode="MultiLine"></asp:TextBox>
<asp:LinkButton ID="lnkSaveComment" runat="server" OnClick="lnkSaveComment_Click">Save</asp:LinkButton>
<asp:LinkButton ID="lnkCancelEdit" runat="server" OnClick="lnkCancelEdit_Click">Cancel</asp:LinkButton>
<asp:HiddenField ID="hdnCommentID" Value='<%# Bind("comment_id") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
Code Behind is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim IdNumber As Integer = sender.ClientQueryString
Dim mydata As New Profile_Data
gvCommentsEdit.DataSource = mydata.returnCommentsById(RequestedUsername, IdNumber)
gvCommentsEdit.DataBind()
End Sub
Sub lnkSaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myRow = sender.parent
Dim mydata As New Profile_Data
Dim IdNumber As String = CType(myRow.FindControl("hdnCommentID"), HiddenField).Value
Dim text As String = CType(myRow.FindControl("txtEditCommentPopup"), TextBox).Text
mydata.UpdateComment(IdNumber, text)
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CloseWindowScript", "window.opener.location.href = window.opener.location;window.close();", True)
End Sub
End Class
This page is a popup from the main page and populates fine. The problem is when I change the txtEditCommentPopup TextBox, which then calls lnkSaveComment_Click(), it has the original textbox values, not the changed value. I'm not using AutoPostBack. Any ideas on why? Thanks!
Assuming the database update is working correctly, the problem is that you are binding the GridView on every PostBack, but not after the data is updated.
When you click your button, you should see that your Page_Load method is called, your GridView is bound, then your lnkSaveComment_Click method is called, updating the database. But afterwards, you are not rebinding with the new data.
So my guess is that if you were to click the update button again (or cause another PostBack somehow), your new data would show up.
The fix would be to make sure you bind your GridView again after you update the data.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Sub lnkSaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myRow = sender.parent
Dim mydata As New Profile_Data
Dim IdNumber As String = CType(myRow.FindControl("hdnCommentID"), HiddenField).Value
Dim text As String = CType(myRow.FindControl("txtEditCommentPopup"), TextBox).Text
mydata.UpdateComment(IdNumber, text)
BindGridView()
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CloseWindowScript", "window.opener.location.href = window.opener.location;window.close();", True)
End Sub
Sub BindGridView()
Dim IdNumber As Integer = Page.ClientQueryString
Dim mydata As New Profile_Data
gvCommentsEdit.DataSource = mydata.returnCommentsById(RequestedUsername, IdNumber)
gvCommentsEdit.DataBind()
End Sub
I added this in your page load
If Not IsPostBack Then
'your code
End If
so replace your Page_Load with this:-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim IdNumber As Integer = sender.ClientQueryString
Dim mydata As New Profile_Data
gvCommentsEdit.DataSource = mydata.returnCommentsById(RequestedUsername, IdNumber)
gvCommentsEdit.DataBind()
End If
End Sub
Hope this will help you.

ASP.NET: Unable to retrieve values for controls located in a panel after postback

I've placed some controls in a panels. When the page postback, I'm trying to retrieve the posted values, but only the older value seems to be there.
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim _txtFName As TextBox = FindControl("editNamePanel").FindControl("txtFName")
Dim _txtMName As TextBox = FindControl("editNamePanel").FindControl("txtMName")
Dim _txtLName As TextBox = FindControl("editNamePanel").FindControl("txtLName")
End Sub
Even when I hover over the e EventArgs is null. Am I missing something?
EDIT
I'm getting new values when I put the above code in the page load event handler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Page.IsPostBack Then
'The above code here ...
End If
End Sub
Thank for helping
In your aspx
<asp:Panel ID="editNamePanel" runat="server">
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
In your code behind
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim _txtFName As TextBox = txtFName
Dim _txtMName As TextBox = txtMName
Dim _txtLName As TextBox = txtLName
End Sub

ItemCommand on nested Repeater

I have a simple aspx page which contains 2 nested repeaters. Each of these have buttons on them. When the user clicks on a button on the outer repeater, I can capture the ItemCommand in my codebehind, however when the user clicks on an inner one, I cannot capture it.
I have read in other threads about needing to attach the event manually to the inner repeater, but cannot work this out.
Can anyone help?
Here is the aspx. I'm using vb.net code behind
<asp:Repeater runat="server" ID="ParentRepeater">
<ItemTemplate>
<li id="P<%#DataBinder.Eval(Container, "DataItem.id")%>">
<%#DataBinder.Eval(Container, "DataItem.name")%>
<asp:Button runat="server" ID="adedit" Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>'
class="pages-edit" />
<asp:Button runat="server" ID="addel" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
class="pages-delete" />
<ul class="page-section sub innerdrag">
<asp:Repeater runat="server" ID="childrepeater">
<ItemTemplate>
<li id="<%#DataBinder.Eval(Container, "DataItem.id")%>,">
<%#DataBinder.Eval(Container, "DataItem.name")%><asp:Button runat="server" ID="ad_edit"
Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>' class="pages-edit" />
<asp:Button runat="server" ID="ad_del" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
class="pages-delete" />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
Still not firing so I've put the updated VB here too
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim DBFunctions As New DBFunctions.Functions
Dim dstmp As New DataSet
Dim dstmp2 As New DataSet
dstmp = DBFunctions.SQLDataSet("SELECT id,name, 'x'+cast(id as varchar(50)) as xid from pages where parent = 0 and coalesce(active,1)=1 order by orderby asc", "data")
dstmp2 = DBFunctions.SQLDataSet("SELECT id,name , 'x'+cast(id as varchar(50)) as xid,parent from pages where parent >0 and coalesce(active,1)=1 order by orderby asc", "data2")
Dim allData As New DataSet
allData.Tables.Add(dstmp.Tables(0).Copy)
allData.Tables.Add(dstmp2.Tables(0).Copy)
allData.Relations.Add(New DataRelation("Children", allData.Tables(0).Columns("ID"), allData.Tables(1).Columns("parent")))
ParentRepeater.DataSource = allData
ParentRepeater.DataBind()
sdhfunctions.Close()
End If
End Sub
Protected Sub repMenu1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ParentRepeater.ItemDataBound
Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If dv IsNot Nothing Then
Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
If repSubMenu IsNot Nothing Then
AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
repSubMenu.DataSource = dv.CreateChildView("Children")
repSubMenu.DataBind()
End If
End If
End Sub
Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If dv IsNot Nothing Then
Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
If repSubMenu IsNot Nothing Then
AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
repSubMenu.DataSource = dv.CreateChildView("Children")
repSubMenu.DataBind()
End If
End If
End Sub
Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
Dim stophere As String = ""
End Sub
I put a breakpoint on stophere, and it never gets hit.
Sorry guys, I'm really confused :(
Try this:
Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
Dim childRepeater As Repeater = DirectCast(e.Item.FindControl("childrepeater"), Repeater)
AddHandler childRepeater.ItemCommand, AddressOf childrepeater_ItemCommand
childRepeater.DataSource = "the data source for childRepeater"
childRepeater.DataBind()
End Sub
Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
End Sub

Asp.Net pass data from code-behind to code-infront?

I have this in my code behind on my Asp.Net page
Public Class display
Inherits System.Web.UI.Page
Protected Property pID As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("id") = "") Then
Response.Redirect("Default.aspx")
Else
pID = Request.QueryString("id")
End If
End Sub
End Class
Now, I want to get the ID to influence items in the code infront when the page loads, so i added this:
<asp:Label ID="Label1" runat="server" Text='<%# pID %>'></asp:Label>
as a test object. when ran 'Label1' is just a blank span. what am i doing wrong?
You don't need <%# pID %>, Just use
Label1.Text = pID
As Label1 is a server control and is accessible in page load.
Change your code as
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("id") = "") Then
Response.Redirect("Default.aspx")
Else
pID = Request.QueryString("id")
Label1.Text = pID
End If
End Sub
EDIT:
I would suggest you to use LinkButton
<asp:LinkButton OnClick="LinkButton_Click" />
CS Code
Sub LinkButton_Click(sender As Object, e As EventArgs)
Response.Redirect(piccom.displayLink(pID))
End Sub
Keep things simple and organized like #saptal is suggesting.
pID = Request.QueryString("id")
Label1.Text = pID
If you need an anchor control you can use the HyperLink control like this on your markup:
<asp:HyperLink ID ="genHyperLink" CssClass="genLink" runat="server" />
code behind:
genHyperLink.NavigateUrl = piccom.displayLink(pID)

Add Textbox TemplateField Column To GridView Programmatically

How do I add a TextBox column to a GridView from code behind?
Use a TemplateField and add a TextBox to the ItemTemplate/EditItemTemplate.
<ItemTemplate>
<asp:TextBox ID="TxtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</ItemTemplate>
You can bind it on the aspx as in the example above or in GridView.RowDataBound:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim dr As DataRow = DirectCast(DirectCast(e.Row.DataItem, DataRowView).Row)
Dim TxtFirstName As TextBox = DirectCast(e.Row.FindControl("TxtFirstName"), TextBox )
TxtFirstName.Text = dr("FirstName").ToString
End Select
End Sub
Edit: Here is a simple example on how to add a TemplateField dynamically:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim txtColumn As New TemplateField
txtColumn.ItemTemplate = New TextColumn
Me.GridView1.Columns.Add(txtColumn)
End If
End Sub
Class TextColumn
Implements ITemplate
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim txt As New TextBox
txt.ID = "MyTextBox"
container.Controls.Add(txt)
End Sub
End Class
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx

Resources