Link Button disappears when Post Back occurs - c#

I create a link button and put an image on it.
Here is my code:
<asp:LinkButton ID="LinkButton1" runat="server" Text="">
<asp:Image ID="Image1" ImageUrl="" runat="server" />
</asp:LinkButton>
And here is my subsequent C# code:
if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
I provide the ImageUrl from the c# code behind for the Image and add an OnClientClick event from c# code for the Link Button. When the page first load then the button showed properly.
My browser rendered HTML before post back
<a onclick="ClientClick();" id="MainContent_LinkButton1"
href="javascript:__doPostBack('ctl00$MainContent$LinkButton1','')">
<img id="MainContent_Image1" src="Images/embed.png">
</a>
When I press any button in this page and PostBack occurs, then the button disappears but I don't do anything in my code behind for that button for PostBack.
My browser rendered HTML after post back
<a onclick="ClientClick();" id="MainContent_LinkButton1"
href="javascript:__doPostBack('ctl00$MainContent$LinkButton1','')">
</a>
If I do not add OnClientClick or Text for the Link Button then the image does not disappear.Or if I set ViewState false for link button then image does not disappear. So why this button disappear when page is PostBack?

With EnableViewState="true" this code works correct even with post back and the image is shown
<asp:LinkButton ID="LinkButton1" runat="server" Text="">
<asp:Image ID="Image1" ImageUrl="" runat="server" />
</asp:LinkButton>
if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
With EnableViewState="false" You must remove the IsPostBack because is not saved anywhere and you need to add it again
// remove that check if EnableViewState is false
//if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}

This occurs because Postback clears all the control of your asp.net page.
To retain controls from previous load, you need to make sure that whatever code is in Page_Load() is re-executed on "Button_click" which causes postback.
void button_click()
{
// do stuff
page_load(Dummy Object);
}

Related

asp.net search button just refreshing page

Im trying to create a search button on my website on my homepage which is an aspx page but when I click search all it does is refresh the page instead of preforming the query, any help would be appreciated
heres the code for my index.aspx and the code for my index.aspx.cs page
<asp:TextBox ID="searchtitle" runat="server"></asp:TextBox>
<asp:Button ID="searchitems" runat="server" Text="Search" />
protected void searchitems_Click(object sender, EventArgs e)
{
String stext = searchtitle.Text;
Response.Redirect("search.aspx?searchquery=" + stext);
}
The button is not working because you didnt call the event on the button
<asp:Button ID="searchitems" runat="server" OnClick="searchitems_Click" Text="Search" />
add OnClick attribute with name of the event you want to call and in the given code the event you want to call is searchitems_Click.
definitely when you will click the searchitems page will reload because function for that click is defined on server side.. what you have to do is on the pageload look for the QueryString and get the value of ["searchquery"] then manipulate it as you want..
if you want to not refresh the page use Ajax then...

Event in repeater not fired until page postback by another control in asp.net c#!

Why My Event as link button event or item command not fired until i post back page with another control in page.
for example in this code:
<div id="pagination">
<span class="all" runat="server" id="CurrentPage">Total Pages</span>
<asp:Repeater ID="RPTPaging" runat="server" Visible="false" OnItemDataBound="RPTPaging_ItemDataBound" OnItemCommand="RPTPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="BtnPage" CssClass="inactive" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" Text="<%# Container.DataItem %>"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
related to this question
Why Repeater ItemCommand Doesn't fire, even if i don't rebind by post back?
i found when i click on link button in this repeater nothing happened,but after i click on empty input button on page, after post back a page changed corectlly.
Dear Expert After 2 day finally i figure out why my page doesn't post back.
after i tried to post back my button manually with java script and i failed. i tried to trace the script and i found this Error with firebug:
TypeError: theForm.submit is not a function
theForm.submit();
when i google it i found i have a button in my MasterPage with id="Submit" and as
"submit is not a function" means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.
When you name the button submit, you override the submit() function on the form.
So This Problem Resolved. Thanks to all.

Window opened using window.open reopens when browser back button is clicked

I created an asp.net web application which has a linkbutton and hyperlink in the default.aspx. Hyperlink is set navigationurl -"www.google.com". Linkbutton opens the same url in a new tab using javasript's window.open.
Default.aspx
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Data</asp:LinkButton><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>
Default.aspx.cs
protected void LinkButton1_Click(object sender, EventArgs e)
{
string url = "http://www.google.com";
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + url + "');", true);
}
Steps to reproduce my query:
1. Clicks LinkButton. This opens google in new window/tab.
2. Click Hyperlink. This navigates to google.
3. Click Browser Back button.
This time the browser navigates back to default.aspx, simultaneously google opens in new window/tab.
T want this not to happen.
Use Hyperlink Button instead of link button and use target="_blank" and url ="www.google.com" this will open url in new window
No need to use java script to open a new Window .... Also It will Resolve Your Problem
<asp:HyperLink ID="HyperLink2" Target="_blank" NavigateUrl="http://www.google.co.in" runat="server" >Data</asp:HyperLink><br />
<asp:HyperLink ID="HyperLink1" Target="_self" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>
here's the code: add the code to the page which you don't want the user to return using back.
if(history.length>0)
history.go(+1);
call it on body load
basically i increment the browser history.
Add this codefunction in your head tag
<script type="text/javascript">
function myfun() {
window.open("http://www.google.co.in");
return false;
}
</script>
and in your body
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return myfun();">Data</asp:LinkButton><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>
Note: Avoid any request to server until and unless its not required,you can also do the same with the help of <a> tag by keeping target="_blank"

Link OnClick code behind does not execute when set as ModalPopupExtender TargetControlID

I did a lot of searching and cannot figure this out.
I have a ModalPopupExtender pop-up that I want to display when the user clicks a link DoSomething. The pop-up has a dropdown control that I then want to populate on the fly when the user asks to open the dialogue. This needs to happen server side via the code behind. Currently I am trying to do it via an OnClick event on the link but as soon as the link is tied to the ModalPopupExtender the link OnClick code is not executed.
Code snippet:
<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server" BackgroundCssClass="modalBackground"
DropShadow="true" PopupControlID="lnkDoSomething"
PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"
TargetControlID="lnklnkDoSomething"></asp:ModalPopupExtender>
The problem is as soon as I set the ModalPopupExtender to the link the OnClick code does not execute. This obviously is by design but it doesn't make sense to me (naive) as if the user clicks the link the OnClick code should be executed.
Any ideas why this is not supported and what the correct solution is?
Attach the ModalPopupExtender to a dummy button and show the modal on the LinkButton's OnClick even from the code-behind:
Markup:
<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:Button id="dummyButton" runat="server" style="display:none;" />
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server"
BackgroundCssClass="modalBackground" DropShadow="true" PopupControlID="controlToPopUpId"
PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"
TargetControlID="dummyButton"></asp:ModalPopupExtender>
Code-behind:
protected void lnkDoSomething_Click(Object sender, EventArgs e)
{
//do work
mpelnklnkDoSomething.Show();
}

Calling a functon in code behind by using an imagebutton in a gridview

I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
<!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink.
Sure it won't be fired because it is nested in a Hyperlink. So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. ImageButton can cause the desired action only if it stands out of the Hyperlink. Try this:
<asp:GridView ....
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');"
PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
ImageButton can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. You can omit the HyperLink.
Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
Edit: Make sure event name and arguments are correct. This is the event I used to test it. y the way don't forget to place the ImageButton in a TemplateField, refer the code above
protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
TextBox5.Text = "Fired ";
//Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one
}

Categories

Resources