Using a PostBack on a LinkButton in ASP.NET - c#

I have an .ascx control in my searchresults.aspx page:
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt" PostBackUrl="~/searchresults.aspx?type=topics"
runat="server"/>
But when I click on it, it does the postback but the type=topics doesn't appear to be sent. Any suggestions?

Try out HyperLink to navigate to an other page:
<asp:HyperLink NavigateUrl="~/searchresults.aspx?type=topics" />
From MSDN:
HyperLink
A control that displays a link to another Web page.
On LinkButton class page:
If you want to link to another Web page when the control is clicked,
consider using the HyperLink control.
EDIT: Answer to comments
Remove PostBackUrl from LinkButton
Add <asp:LinkButton OnClick="OnTopicsTypesEnabled" ... />
In code behind (searchresults.aspx.cs)
protected void OnTopicsTypesEnabled(object sender, EventArgs args)
{
// handle this particular case
}

I believe your code will perform an POST, whereas you need a GET to transfer your variables through QueryString.

Related

modalpopupextender asp.net usercontrol page

I have a usercontrol as a header on a page. The usercontrol has sign up button which opens a modalpopupextender. The popup open code on sign up link is below:
protected void lnkSignUp_Click(object sender, EventArgs e)
{
mp1.Show();
}
Now I am able to successfully use this usercontrol in my page. The problem is in my page there are other links apart from header which will provide sign up facility. Now I want to use the same signup popup of usercontrol to be opened by clicking on page sign up links. What should I do on page link click. I have tried the below code from page but myMpeModal returns null:
ucHeaderJobseeker uc1 = new ucHeaderJobseeker();
var myMpeModal = (AjaxControlToolkit.ModalPopupExtender)uc1.FindControl("mpeModal");
myMpeModal.Show();
Try doing something like this:
Your Front End (.aspx Page):
<uc1:WebUserControl runat="server" ID="myUserControl" />
Your UserControl Should look like this:
<ajax:ModalPopupExtender ID="mpeModal" runat="server" BackgroundCssClass="modalBackground"
PopupControlID="pnlShowMe" TargetControlID="btnClickMe" CancelControlID="btnClose">
</ajax:ModalPopupExtender>
<asp:Button ID="btnClickMe" runat="server" Text="Click Me" OnClick="btnClickMe_Click" />
<asp:Panel ID="pnlShowMe" runat="server" CssClass="ModalPanel">
</asp:Panel>
Your Code Behind (.aspx.cs):
var myMpeModal = (AjaxControlToolkit.ModalPopupExtender)myUserControl.FindControl("mpeModal");
myMpeModal.Show();
Here in the above code, "myUserControl" is your user control, "mpeModal" is your ModalPopupExtender id.
Use the above code where ever you want in your page to show the popup.

Session Variable in ASP.NET returns previous value in ModalBox?

I have a question regarding passing Session Variables to a text-box in an Update Panel (which is displayed in a Modal PopUp).
This is the code I have so far:
ASPX CODE:
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Link" runat="server" OnClick="LinkButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="panel_Load">
<ContentTemplate>
<asp:Button ID="OKButton" runat="server" Text="Close" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="ClientButton" PopupControlID="UpdatePanel1" OkControlID="OKButton">
</asp:ModalPopupExtender>
<asp:Button ID="ClientButton" runat="server" Text="Launch Modal Popup (Client)" style="display:none;" />
CODE BEHIND (C#):
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
Label lbl_nme = (Label)clickedRow.FindControl("lbl_name");
String string_nme = lbl_nme.Text.ToString();
Session["Name"] = string_nme;
mpe.Show();
}
protected void panel_Load(object sender, EventArgs e)
{
Label1.Text = (string)(Session["Name"]);
}
So basically I have a GridView with name, address etc… When the user clicks on a link in a row, then the value for the name field of that row is saved as a session variable. Then a Modal PopUp is displayed. The Modal PopUp should then show the Name which was saved as a Session variable.
The code sort of works. What I’m experiencing is that when I click a row, the Label1.Text in the Modal PopUp is empty. So if I close the PopUp then click another link in another row, the PopUp then displays the Name of the row that was clicked previously.
In other words.. If row 1 has Name “Kevin” and row 2 has Name “Nathaniel”, and I click a link to open the Modal PopUp of row 1, I would expect the PopUp to display “Kevin”. But it doesn’t. The first time I click a link after rebuilding the application, nothing is displayed. But say I click row 2 after clicking row1, then the Modal PopUp displays the value of the row I clicked before, i.e. “Kevin” when I expect it to be “Nathaniel”.
I hope I didn’t confuse anyone. I’m a newbie and I’m just getting into this stuff, so I’d appreciate it if someone could help me out, preferably with examples of code etc.
Thank you. Much appreciated.
The "Load" event (panel_Load) occurs before the "Click" event (LinkButton1_Click) so it only sees the previous value.
The quick fix is to set the label in the "Click" event as well. Unless ViewState is enabled for the label (ick!) the label may have to be [re]set in the "Load" as well, depending upon when/how updates occur.
See ASP.NET Page Life Cycle Overview and ASP.NET Application and Page Life Cycle: Page Events.
Happy coding.

On click event of image button

I have a problem that when I click on the image button instead of redirect me to the corresponding page, it will just redirect back to the same page.
This is the code in asp;
<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" />
And this is in my page load in the code behind;
header1.Attributes.Add("onclick", "~/ChildSelection.aspx");
Any ideas why this is happening?
Your Image button should have an onclick event.
<asp:ImageButton ID="header1" ImageUrl="Resources/Icons/Header1.jpg" runat="server" OnClick="header1_Click" />
protected void header1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/ChildSelection.aspx");
}
The onclick event executes javascript if I'm not mistaking. OnClick refers to a server-side event. Try setting the window's location to redirect to the corresponding page.
header1.Attributes.Add("onclick",
string.Format("window.location = '{0}'", ResolveClientUrl("~/ChildSelection.aspx")));
I guess you can achieve it by replacing it by
header1.Attributes.Add("PostBackUrl", "~/ChildSelection.aspx");
Onclick is an event. You might also consider using a hyperlink with ImageSrc property instead of an ImageButton.
[Edit]
If you are just trying to redirect a better approach could be
<asp:HyperLink ID="header1" runat="server" ImageUrl="Resources/Icons/Header1.jpg">Click Here</asp:HyperLink>
And in code behind
header1.NavigateUrl = "~/ChildSelection.aspx";
Use the following code:
<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" PostBackURL="~/ChildSelection.aspx"/>

Click Event of Hyperlink

How to find Whether a hyperlink is clicked or not in ASP.net C# in runtime?
I want to write code on like that
Response.Redirect("Default.aspx");
If you want to execute server code upon a click in a link, then you should use the ASP.NET control <asp:LinkButton>
This is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.
You would attach either the event in the code behind, or in the ASPX / ASCX of your link in question like so:
<asp:LinkButton ID="linkGoSomewhere" runat="server" Click="linkGoSomewhere_Click" />
OR
linkGoSomewhere.Click += (linkGoSomewhere_Click);
With an event handler looking like so in your code:
public void linkGoSomewhere_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
HOWEVER
In this situation, you don't need a server side control to just send the user somewhere else. You just need a simple hyperlink:
Go somewhere else
if this HyperLink you can do it using javascript but if it is LinkButton you can do it inside onclick event
<asp:LinkButton ID="MyLnkButton" runat="server" onClick="MyLnkButton_Click" Text="Click Me!">
protected void MyLnkButton_Click(Object sender,EventArgs e)
{
Response.Redirect("Default.aspx");
}
The onclick server side handler can be added to achieve this.
<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>
You can determine this with the Click event of the LinkButton

Setting focus on top of the page on click of asp.net link button

I've a asp.net datagrid which shows customer order details.
Pagination at the bottom of the grid is done using datalist and asp.net Linkbutton controls.
Here is the code:
<asp:DataList ID="DataList2" runat="server" CellPadding="1" CellSpacing="1"
OnItemCommand="DataList2_ItemCommand"
OnItemDataBound="DataList2_ItemDataBound" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnPaging" runat="server"
CommandArgument='<%# Eval("PageIndex") %>'
CommandName="lnkbtnPaging"
Text='<%# Eval("PageText") %>' />
<asp:Label runat="server" ID="lblPageSeparator" Text=" | " name=></asp:Label>
</ItemTemplate>
</asp:DataList>
When the user clicks on any page number(ie.Link button), I need to set focus on top of the page.
How do i do this?
Thanks!
I think the default behaviour would be for the page scroll position to be set back to the top of the page. Is there anything else in your page that might be overriding this behaviour?
For example:
Is your DataList inside an UpdatePanel? In that case the current scroll position will be maintained over a (partial) post-back. You would therefore need to reset the scroll position to the top of the page yourself. One way to do this would be to implement a handler for the PageRequestManager's EndRequest client-side event which would set the scroll position - this thread explains how
Is the Page MaintainScrollPositionOnPostBack property set to true?
You could try setting a named anchor at the top of the page. Here is an article that explains it http://www.w3schools.com/HTML/html_links.asp
After an AJAX partial postback you may need to return to the top of your ASPX page to display an error message, etc. Here is one way that I have done it. You can add the JavaScript function below to your ASPX page and then call the method when needed in your code-behind by using the ScriptManager.RegisterClientScriptBlock method.
ASP.NET C# Code-behind:
ScriptManager.RegisterClientScriptBlock(this, Page.GetType(),
"ToTheTop", "ToTopOfPage();", true);
JavaScript:
<script type="text/javascript">
function ToTopOfPage(sender, args) {
setTimeout("window.scrollTo(0, 0)", 0);
}
You can also just JavaScript to scroll to the top of the page by using the OnClientClick property of your button. But this will cause this behavior to occur every time the button is clicked and not just when you want it to happen. For example:
<asp:Button id="bntTest" runat="server"
Text="Test" OnClick="btn_Test" OnClientClick="javascript:window.scrollTo(0,0);" />
<asp:LinkButton ID="lbGonder" runat="server" CssClass="IK-get" ValidationGroup="ik" OnClick="lbGonder_Click" OnClientClick="ddd();" title="Gönder">Gönder</asp:LinkButton>`
<script type="text/javascript">
var ddd = (function () {
$('body,html').animate({
scrollTop: 300
}, 1453);
return false;
});

Categories