Category Archives: HyperLink

Back button functionality in web page

Hi,


In one of the pages of my web applicaiton developed using VS 2005, I have a textbox. When the user enters soem text in the textbox and clicks search button, then I retrieve data pertaining to teh search string and display the same in a datagrid. In teh datagrid, each row represents a report and there is a hyperlink column. Clickign on this column would take the user to another page which displays the complete details of the report. Now I want to provide a back button in the 2nd page(where the complete details of the report are displayed) so that the user can go back to the previous page where he entered the search criteria. I have placed the back button in the second page. In the Click event handler of the  back button, when I give a Response.redirect or Server.Transfer, it takes me to the previous page. This is the expected expected behaviour. But I want the page to be the same as it was before leaving it.

For example, If in Page1, I entered the search criteria as msdn and clicked on search, then a datagrid pertainign to this would be dispalyed with say 10 rows. I click ont eh hyperlink in one of the rows ato view the complete details. In Page 2, click Back button. Now I should be redirected to page 1 and the page 1 should contain the search criteria as msdn and the datagrid pertainign to this should also be displayed. How do I achive this?

Thanks,
Sandhya

Dear Sandhya, Please try the following steps. 1. Add a ASP button to a page namely e.g btnBack 2. In page load event write btnBack.Attributes.Add("onclick","history.back()"); Try it. i hope this will help you
Hi,
Try the following code... 
Response.Redirect(Request.UrlReferrer.ToString())
Or 

use history.go(-2)  it take you  two pages back.

eg.
<asp:LinkButton ID="LinkButtonBack" runat="server" CausesValidation="false" PostBackUrl="javascript:history.go(-1);">Back</asp:LinkButton><br/>

Hope it helps...

~Jagadeesh

 

--- Mark as answer if the post meets your requirement! 

 

 

If page1 is simply displaying search results then you can pass the search term back from page2 using querystring.

1. Page1 passes search term to Page2 ( Response.Redirect("Page2.aspx?searchterm=msdn&otherparameters=xyz") )

2. Page2 stores searchterm in viewstate and displays details

3. Page2 displays aBack button, which on click calls Response.Redirect("Page1.aspx?searchterm=msdn")

4. Page1's page load event checks if a searchterm has been passed, if so then get the results

page1's page load event

if(!Page.IsPostBack)

{

if (Request.QueryString["searchterm"] != null)

{

LoadResults for search term

}

}

 

I had the exact same problem. I placed the following in the Client script. No Server side code was needed:

 

<asp:LinkButton ID="LinkButtonClose" runat="server" CausesValidation="false" PostBackUrl="javascript:history.go(-1);">Close</asp:LinkButton><br/>
 

Hi,

I tried the same. But history.back() does n't work forme. So I tried adding a link button and set the PostBackUrl property to history.back(-1) and it worked. Why didn't it work for the button control? Any idea?

 Thanks,

Sandhya

 Thank u.

 Hi,

 Use below  example

protected void Page_Load(object sender, EventArgs e)
    {      
        if (!IsPostBack)
        {

        if (Convert.ToString(Session["Action"]) == "search")
        {
            txtSearch.Text = Convert.ToString(session["SearchText"]);
                

//Call the appropriate function for search

           Search();

        }

    }

}


//HyperLink OnCommand=" OnCommand_lnkName"

protected void OnCommand_lnkName(object sender, CommandEventArgs e)
    {

session["SearchText"] = txtSearch.Text;
Session["Action"] = "Search";

    }

thanx Bill .......   this is perfect code n it worked for me :) :)Cool 

[RESOLVED] creating hyperlinks dynamically.

I want to create hyperlink dynamically whenever i click the button below is my code

int j = 1;
        for (int i = 0; i < j; j++)
        {
            //HyperLink links = new HyperLink();
            HyperLink hplink = new HyperLink();
            hplink.NavigateUrl = "http://www.asp.net";
            hplink.Text = "Click Here" + "<br /><br />";
            ph.Controls.Add(hplink);
        }

but i am unable to get the hyperlinks dynamically because the loop is going infinite.

for (int i = 0; i < j; j++) 


--> for (int i = 0; i < j; i++) 


Replace j++ with i++, since i is your looping variable. 

Ya when i have done what you have said but i am getting only one hyperlink when i press the button,i want to createhyperlinks whenever i click the button, i mean how many times i click the button that many times i have to get hyperlink. 

Here is a way to do that


1;) in your aspx markup :

<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <br />
    <asp:Literal runat="server" ID="LinksPlaceHolder" />
    </form>
</body>


2;) in the code-behind :

    protected void Button1_Click(object sender, EventArgs e)
        {
            LinksPlaceHolder.Text += "<a href=\"http://www.asp.net/\">go to the asp.net website</a><br/></br>";
        }


if this  is not what you want, explain exactly what your are trying to do


This video shows one way of doing something like that: http://www.asp.net/ajax/videos/how-to-dynamically-add-controls-to-a-web-page 

There is a major problem with creating a new hyperlink each time a button is pressed.  Dynamic controls have to be redrawn each time the page loads.  In order to redraw a different hyperlink each time a button is pressed you will need to have a lists of hyperlinks in a DataTable or HybridDictionary that create the hyperlinks each time the button click causes a postback.  Your redraw loop is close but it should use a For Each and loop through the collection of hyperlink data and draw a control for each one during the Page Init method.  In the button click simply add the new hyperlink data to the collection.  Dynamic controls are pain and should be avoided whenever possible. 

Are you saying about tables rows and columns and adding the control to column and generating whenever the button is clicked?? but i want to achieve with a for loop is it possible?? 

For example the DataTable would contain columns CONTROL_ID, URL, TEXT.  Each time the button is clicked you create a new row from the DataTable fill in the fields and add the row to the data and store it in the ViewState.  Each time the page load you read the DataTable with your for each Row loop and add your hyperlinks to the placeholder.  This will make sure the drawing is constant each time and you only have to debug the data is getting updated and added properly.   

I think a little bit of code can explain much than big sentences.

Invalid loop for (int i = 0; i < j; j++)  

Write loop 

>> for (int i = 0; i < j; i++)  

Hi,

Http is stateless, Dynamically created control can not maintain itself after postback.

If you dynamically add a HyperLink Control to the page during button click, be aware that every PostBack is a new Request .They need to be recreated each time the page loads.

Check this two links, they give you some example and explanation:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23207581.html

http://forums.asp.net/t/1186195.aspx

Hope this can help you.

Here you have increamented j++, that should be i++. so in your case conditon not get false at any time so that is goes in infinate looks

change as per below code

int j = 1;
        for (int i = 0; i < j; i++)
        {
            //HyperLink links = new HyperLink();
            HyperLink hplink = new HyperLink();
            hplink.NavigateUrl = "http://www.asp.net";
            hplink.Text = "Click Here" + "<br /><br />";
            ph.Controls.Add(hplink);
        }


 

Please mark as answer if this is helps

 

[RESOLVED] Regarding URL rewrite

for url rewite when my hyperlink point the url like www.mysite.com/content?ID=20 but i want to know how to write logic that url  www.mysite.com/content?ID=20 will become www.mysite.com/mycontent of hyperlink when user mouse over the link.

i have another quetsion that how to write logic very easyly that will redirect the request to www.mysite.com/content?ID=20 internally when the url is actually click www.mysite.com/mycontent.


please guide me in detail with sample code. i am working with asp.net webform version 3.5.


thanks


I would suggest using a url rewriting library such as UrlRewriter.net.

I have written an article that should hopefully help you solve the second part, turning clean url requests into page?id=20 types of requests.

 

To create these links in your page so that they are clickable is simply a matter of changing the way you generate your urls. So if you create a link by using some kind of template like page.aspx?id={0} and inserting the id into it then you just have to change this to pull different data in instead of the id. There are two ways to approach this. One is to add an extra field in your database so that you can actually type in the friendly url portion of the address (referred to technically as the "slug").

Alternatively you can use String.Replace() to attempt to automate this by replacing spaces with dashes or underscore and removing other undesired characters.

mou_inn

have another quetsion that how to write logic very easyly that will redirect the request to www.mysite.com/content?ID=20 internally when the url is actually click www.mysite.com/mycontent.

You can convert www.mysite.com/myconten/20 to www.mysite.com/content?ID=20 using url rewriting.

For complete understading on URL rewriting please see following link

http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/


[RESOLVED] Disable a Image button or Hyperlink button based on ASP.Net Role

Ok, here's my issue I have four Imagebuttons that I want to disable depending on the Role the user belongs too.  I know how do do this in a gridview or detailsview but for some reason it isn't working on just a plain form.

So when a User who is not in the "Admin" role logs onto the page he either doesn't see  the button or the hyperlink has been disabled thus not allowing him to proceed any further.

 

Anyone got any ideas?

 

Any assistance would be greatly welcomed.

Here is an example that i would use:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Not User.IsInRole("Admin") Then
                Image1.Visible = False
            End If
        End If
    End Sub


 

or in c#:

protected void Page_Load(object sender, System.EventArgs e)
{
	if (!Page.IsPostBack) {
		if (!User.IsInRole("Admin")) {
			ImageButton1.Visible = false;
                        hyperlink1.visible = false;
		}
	}
}


 

Well that seemed to take care of it for the most part.  I do have 1 issue still, I actually have 4 Roles (Admin, Supervisor, PowerUser, User).  I can get each to work individually from each other is there a way that I could do something like this:

If not Page.IsPostBack then
If Not User.IsInRole("Admin") or (User.IsInRole("Supv") then
Image1.visible = false
End if
end if


  

 you can do this:

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
	If Not Page.IsPostBack Then
		If (Not User.IsInRole("Admin")) OrElse (Not User.IsInRole("Supv")) Then
			ImageButton1.Visible = False
			hyperlink1.visible = False
		End If
	End If
End Sub


 

that doesn't seem to work at all, nothing is showing up.

Here's the code based of your sample:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If (Not User.IsInRole("Admin")) OrElse (Not User.IsInRole("GADS")) Then
                GADSItemButton.Visible = False
            End If
        End If
    End Sub


Any Ideas why this isn't working.  And "yes", GADS is a role.

 

I figured it out.  Thanks 

Did you check: there are any links/image for 'Admin' or 'GADS' role ? and also debug the code to see what it's actually returning....