Setting content Page.Title dynamically from blog post title - c#

I'm building a tiny blog app, and what I need to do is show blog post title for dynamically generated content page Item.aspx in page title.
In other words, i need data that is bound to this label in FormView to also be shown in page title when page loads.
<asp:Label ID="lblPostTitle" runat="server" Text='<%# Eval("PostTitle") %>' />
I'm using ObjectDataSource to get the data.
Tried a bunch of things, tried this (http://goo.gl/zWz1) to access Eval in code behind, but nothing worked.
Edit:
OK, i got the value from returned DataTable, it's easy
protected void odsItem_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
DataTable dt = (DataTable)e.ReturnValue;
string postTitle = dt.Rows[0]["PostTitle"].ToString();
}
But when I pass it to Page.Title nothing happens.
Please help.
Thank you.

Ok, i found the problem. Earlier, I added this code to Master page code-behind.
protected override void Render(HtmlTextWriter writer)
{
Page.Title = "Site name" + Page.Title;
base.Render(writer);
}
Once I commented it out, i was able to pass dynamic page title value to Page.Title.
This code worked great for appending the page title when content page Title was read from aspx file, but now i have to find another way to append the title.

Related

Pag.Previous page returns null

I have tried many times and in different ways to see why "Page.PreviousPage" returns null but I did not get anywhere. I checked most of posts here in stackoverflow and some other website. None of them worked. Anybody has any idea? Following is the asp code in the source page which I named it "sourcePage.aspx".It shows the button that does the cross page post:
<asp:Button ID="btnCrossPagePostBack" runat="server" Text="Cross page post back navigation" PostBackUrl="~/targetPage.aspx" Width="270px"/>
The following is the code in the target page which I named it "targetPage.aspx":
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = Page.PreviousPage;
if (previousPage != null && previousPage.IsCrossPagePostBack)
{
lblName.Text = ((TextBox)previousPage.FindControl("txtName")).Text;
lblEmail.Text = ((TextBox)previousPage.FindControl("txtEmail")).Text;
}
else
{
lblStat.Text = "You loaded this page using a technique other than Cross Page Post Back";
}
}
"txtName" and "txtEmail" are two text boxes in the source page. I know how to do this by using "Server.Transfer" or Strongly typed reference. I am looking for doing by Corss page post back and not any other way. Somebody said I should use Empty page and then add web forms and not template web forms but there is not empty page and visual studio only provides web forms. Any clue?
Thanks!
The point was correct. When you want to start a new project you should choose "Empty" and not "Web Form" or etc.

EnableViewState for html tag

I am quite new and I will like to ask a question. Please see the html and codebehind.
HTML
<ul id="menu" runat="server" EnableViewState="True"></ul>
CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var liItem = new HtmlGenericControl("li");
var aItem = new HtmlGenericControl("a");
liItem.Attributes.Add("class", "test");
aItem.Attributes.Add("href", "#");
aItem.InnerText = "please work";
liItem.Controls.Add(aItem);
menu.Controls.Add(liItem);
}
}
Once postback, the UL data is lost though I have enabled the viewstate. I remember it works last time but now it is not. Anybody can advise? Thanks a lot
This happens because you are dynamically adding data via your first load (!IsPostback), and thereafter (when Page_Load runs again) the data is being lost. You have to keep in mind that EnableViewState is a ASP.NET specific property, So it will only work on server controls inheriting from System.Web.UI.Control
The only way you can achieve this is by either creating your html tags on each and every page load (i.e remove your !IsPostBack check)
or adding a ASP.NET control to the page which supports ViewState (Gridview, ListView, Label, Button etc).

Display web page according to html string

I have HTML script string stored in database. I am fetching this string in one variable.
I want to display page out of this html string below gethtml button in below .aspx page.
I searched how to display sub html page inside main aspx page.
I came to know with webbrowser control, but not finding it anywhere in tool box.
Is there any other control or way to show html page out of html script in above main aspx page?
Please help me.
<div id="htmlpage" runat="server"></div>
in code behind
protected void Page_Load(object sender, EventArgs e)
{
string ss="<h1>Hello world</h1>";//your html string
htmlpage.InnerHtml = ss;
}

ASP.Net Post User control Post to new page

I am creating a custom user control that will act as a search feature. I want to easily be able to add this to multiple pages without having to modify much code.
I thought the best method to do this would be to create a simple user control that I can inject anywhere with one line of code and then have this control postback to a different URL. So wherever the search function is, it will always post back to the same page. My control looks like this:
<asp:TextBox ID="searchTextBox" runat="server" MaxLength="350"></asp:TextBox>
<asp:Button ID="submit" runat="server" Text="Search" PostBackUrl="~/myPostBackPage.aspx" />
myPostBackPage.aspx.cs looks like this, but it isn't grabbing the text.
protected void Page_Load(object sender, EventArgs e)
{
content.InnerHtml = ((TextBox)PreviousPage.FindControl("searchTextBox")).Text;
}
But it isn't pulling anything from the searchTextBox field and I get:
Object reference not set to an instance of an object.
Is there a better way to do this or how should I fix my code? Thanks!
I don't know where the TextBox is declared, if you for example use MasterPages the NamingContainer of it would be the ContentPlaceHolder of the master instead of the Page. Therefore just cast the PreviousPage property to the correct type:
YourPageType page = PreviousPage as YourPageType;
if(page != null)
{
content.InnerHtml = page.SearchText;
}
You have to provide a public property since the TextBox is protected(best-practise anyway):
public string SearchText
{
get { return searchTextBox.Text; }
}
I tested your code and PreviousPage is null. Try switch to Page.

PostBackTrigger for FileUpload functionality inside a GridView

I have an asp:GridView control inside a .aspx page that the user can add several rows of data to. The user must also be able to attach a file to each row of data added.
For this I use the following inside the GridView:
<asp:TemplateField HeaderText="Upload" HeaderStyle-Width="120px">
<EditItemTemplate>
<asp:FileUpload ID="fuUploadLocation" runat="server" Width="98%" TabIndex="18" />
</EditItemTemplate>
</asp:TemplateField>
Then to save the location of the file upload I use the RowUpdating event in code-behind to set the value etc.
The problem is I can't register a PostBackTrigger for the control on the html as it doesn't pick it up as it's inside the GridView. I've tried setting it dynamically from other examples but can't seem to get this to work, the result being my FileUpload's FileName is always empty and the file then doesn't save correctly.
Any suggestions would be awesome.
Thanks
On Gridview row databound you have to find the file upload control and then add it to your UpdatePanel postback trigger.
I've not found a solution to my problem but I did work around it (sort of). I know just add a link to the grid, when user clicks it sends ID through via querystring, a new page opens that handles the entire upload process and returns the url of the saved document.
Not ideal but it works and saved me hours of frustration.
In the OnItemDataBound event handler of the grid need to call ScriptManager.GetCurrent(this).RegisterPostBackControl(ControlID);
This is an old post but for anyone that is stuck with it, you need to add the following to your control as stated in the other answers.
protected void ItemDataBound(object sender, EventArgs e)
{
Button myButton = (Button)e.Item.FindControl("myButton");
if (myButton != null)
ScriptManager.GetCurrent(Page).RegisterPostBackControl(myButton);
}
You also need to change the enctype in your form tag to the following e.g.:
protected void Page_PreRender(object sender, EventArgs e)
{
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
It should then work without an issue.

Categories