Category Archives: LinkButton

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] Image & Text under Linkbutton


Asslam-o-Alaikum

I have a Link Button in asp.net C# inside it i set an image...........

when i click on image its works... But when i click on text it donts work , here is my code

<asp:LinkButton runat="server" ID="lnkViewCart" PostBackUrl="~/Basket.aspx" style="text-decoration:none" >
<table>
<tr>
<td>
<asp:Image ID="Image5" runat="server" ImageUrl="~/images/Basket/shopping_cart yellow.png" ToolTip="Back to Previous Page"/>
</td>
<td class="labels_Blck">Shopping Cart</td>
</tr>
</table>
</asp:LinkButton>


Please help me

ws

i don't get ur question really...

i don't see any event bound to ur image or text.

 

W/A,

Just try below code and I believe above code will work fine with FireFox.

<asp:LinkButton runat="server" ID="lnkViewCart" PostBackUrl="~/Basket.aspx"   onclick="lnkViewCart_Click" >
<asp:Image ID="Image5" runat="server" ImageUrl="~/images/Basket/shopping_cart yellow.png" ToolTip="Back to Previous Page"/>
<span class="labels_Blck">Shopping Cart</span>


</asp:LinkButton>


Regards

shabir

walekum salam.

you may try using UL-LI list instead of table like below:

</asp:LinkButton>


hope it helps./.

Hi,

Please have a look at this link:

http://weblogs.asp.net/justinsaraceno/archive/2008/11/05/linkbutton-with-image-and-text.aspx

This link has the clear description of the problem you are into. If you are facing any issues post this resolution then i would appreciate if you can give the codebehind code and the snapshot of the issue you are facing during runtime.

Thanks 

Thanks for your Reply but using Image & Span under Link button i loss format between image and text...................

thats the problem

Just find answer here 

asp.net - Why doesn't a LinkButton work with a table as it's ...

<button></button><button></button>

'Regards


[RESOLVED] LinkButton and Event

Hello

I have a small problem, namely, i create dynamicly few LinkButtons like this:  

LinkButton lb = new LinkButton();
lb.CommandName = username;
  lb.CommandArgument = username;
  lb.Text = username;
   lb.Command += new CommandEventHandler(lb_Command);
}
}

And event code looks like this:

private void hpUser_Command(object sender, CommandEventArgs e)
{
txtTo.Text = e.CommandArgument.ToString();
    } 

 

So what exactly i want is, after pushing on the linkbutton i want to see the text appear in the textbox from the event of that linkbutton.

But i never get it there :( really strange, if i debug the code, the event handeling does execute, everything works fine, but i dont see the actual text in the textbox

i also tried to put page.databind() in the event it help but the text appears there only after i click on some button, no on other linkbutton but ordinary button.

Can anyone help me, and tell me what i should do , to get the text immidiately visible in the textbox.

I thank you in advance for your help 

Debug and see what's the username. It might be empty and that's why Text is blank. Where are you adding the control in that function?

Check this example

<asp:Panel runat="server" ID="Panel1">
</asp:Panel>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
protected void Page_Load(object sender, EventArgs e)
{
    List<string> list = new List<string>() { "user 1""user 2""user 3""user 4""user 5" };
    foreach (string s in list)
    {
        LinkButton lb = new LinkButton() { CommandName = "Click", CommandArgument = s, Text = s };
        lb.Command += new CommandEventHandler(lb_Command);
        Panel1.Controls.Add(lb);
    }
}

protected void lb_Command(object sender, CommandEventArgs e)
{
    Label1.Text = "You clicked user " + e.CommandArgument.ToString();
}

server events to client click events in webforms are faked.

when you click on a link button in the browser, it call javascript to fill in 2 hidden fields, one with the name, one with the command arg, then does a form.submit() (postback)

on the server side, during postback event processing, asp.net looks for a control with the same UniqueID as the one in the hidden field, and passes it the command arg. the control then fires its click event (or whatever is the correct event). if there is no control with the same name as the value in the hidden field, nothing happens.

this means for dynamic control to fire events, they must be recreated on postback with the same UniqueID, and early enough in the page life cycle to receive the postbackevent data.

 

Dynamic controls need to be created on every postback.....that is the most common problem for events not behaving properly....check out your code for that...

Yes, all those controls are created ont he page_load, so then i suppose every postback they are recreated, but the think is, the linkbutton i have noticed doenst really do a postback.

  Might it also be the problem that does linkbuttons are created in UpdatePanel? And the textbox is outside of that updatepanel?

Yes, that is probably the reason it is happening.

Controls inside an updatepanel don't do a full postback, instead they only do an ajax call to update the info inside the updatepanel.

Kippie

Yes, that is probably the reason it is happening.

Controls inside an updatepanel don't do a full postback, instead they only do an ajax call to update the info inside the updatepanel.

Hmm ok, so how could i solve the problem, should i put the textbox in another updatepanel and trigger it when the linkbutton is clicked? but the problem then is that i cant make a trigger thingie cuz i dont know the ID of those linkbuttons.

Is there any ohter suggestions, how to solve this problem? 

mudassarkhan

Refer here

http://www.aspsnippets.com/Articles/Creating-Dynamic-Button-LinkButton-and-ImageButton-in-ASP.Net.aspx

Thank you altho it wasnt really of much help, cuz i know how to dynamicaly create linkbuttons and such + someone here already even gave the example.

And secondly i solved the problem, i just added that textbox into the updatepanel and in the event ive added updatepanel.update();


Anyway, thank you everyone for your suggestions and help. 

One more small question, so the updatepanel, if it updates no matter what, it does hit everytime page_load, or normally it shouldnt? i just putted a break point at the page load, and yeah, every 5 sec i hit that break point, (5 sec is my timer setted on and is binded as trigger for the updatepanel) 

Hi,

shkipper

if it updates no matter what, it does hit everytime page_load, or normally it shouldnt

The updatepanel do a partial postback to server, and it will always trigger the page_load event. But it does refresh the control state inside the updatepanel commonly.

Here's a good article for updatepanel. You can check it:

http://msdn.microsoft.com/en-us/magazine/cc163413.aspx

Hope this can help you.

[RESOLVED] Retrieving a controls ID on the posted page

Friends,
 I have a linkbutton inside a repeater's item templete and i want to access the  link buttons text on the next page.I set the postbackurl to the next page.But when i use the page.PrevoiusPage.Findcontrol("lnkReport") on the destination page's code behind , I get a null value .These are the markups .Can anyone please help?

 

<asp:Content ID="Content2" ContentPlaceHolderID="cpmain" runat="Server">
  <fieldset id="fsTrialAct">

</fieldset>
    <asp:Repeater ID="rptRepeater" runat="server">
       <asp:LinkButton ID="lnkReport" PostBackUrl="~/features/Reports/AdHocReportDetail.aspx"
                                runat="server"><%#Eval("AdhocBurstingReportName")%></asp:LinkButton></p>

    </asp:Repeater>

</asp:Content>

Pass ReportName as Querystring and access it in the other page

<asp:Repeater ID="rptRepeater" runat="server">
    <ItemTemplate>
        <asp:LinkButton 
        ID="lnkReport" 
        PostBackUrl='<%# String.Format("~/features/Reports/AdHocReportDetail.aspx?Name={0}",Eval("AdhocBurstingReportName")) %>'
        runat="server">
        <%#Eval("AdhocBurstingReportName")%>
        </asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>

In ReportDetailPage, you can get the value in page load or some other event as

string name = Convert.ToString(Request.QueryString["name"]);