asp.net search button just refreshing page - c#

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...

Related

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.

how to prevent web page refreshing

I have a "CaptchaControl" in a webusercontrol, when i click refresh link for the captcha code, the page refresh too. how can i prevent it?
<cc:CaptchaControl ID="captcha" CssClass="toppading" runat="server" CharCount="5" ImageUrl="~/images/CapchaImage.jpg" />
<asp:LinkButton CssClass="leftpading" id="btnReset" runat="server" Text="Refresh" resourcekey="lblrefresh" OnClick="btnReset_Click" ></asp:LinkButton>
protected void btnReset_Click(object sender, System.EventArgs e)
{
captcha.Refresh();
}
i've heard something about ajax, but i'm green in ajax.
Ajax is the best solution for this problem.ajax will send data to your server with out making a post back in your browser.
Just add an ajax update-panel in your form(you can find this in your Toolbox under the tab Ajax extensions) and put your user-control inside it.And don't forget to add a script-manager in the beginning of your form.

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();
}

Why IE does not refresh the page after a JS call to a click button?

On a ASP.Net page, I am in front of a problem only with IE. Here is the description.
On a page, there is two buttons and one label. The first button is visible and calls a JS function on the click event. This JS function calls the click function of the second button. The second button has an C€ event handler on the click event. The C# event handler edit the label.
In Firefox : the label is correctly edited after the clicks.
In IE (8) : the label is not edited, despite the C€ event handler has been correctly hit.
Also, I observed, in IE, that the Page_Load event is called two times after the JS button click :
Page_Load
button2_OnClick => change of the Label Text
Page_Load => The Label Text is reset :(
In Firefox, the Page_Load is called only once.
My question is : how to make IE refresh correctly the page as Firefox does after a JS button click ?
Below is the sample test code :
1) Page ASPX
<head runat="server">
<title></title>
<script type="text/javascript">
function button1Click(sender, args) {
var button2 = document.getElementById("button2");
button2.click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="button1" Text="Click-me!" OnClientClick="button1Click();" />
<asp:Button runat="server" ID="button2" Text="Second" OnClick="button2_OnClick" style="display:none" />
<p />
<asp:Label runat="server" ID="label1" Text="Init" />
</div>
</form>
</body>
</html>
2) C# code-behind :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2_OnClick(object sender, EventArgs e)
{
label1.Text = "Changed";
}
}
The ID of your button will not be button1 or button2 when it's rendered. It will probably be something like ctl001_button1. Therefore your javascript will not work. In ASP.NET 4 you can override this behaviour by using an assigned ClientID.
<asp:Button runat="server" ID="button1" Text="Click-me!"
OnClientClick="button1Click();" ClientIDMode="Static" />
<asp:Button runat="server" ID="button2" Text="Second"
OnClick="button2_OnClick" style="display:none" ClientIDMode="Static" />
As an aside, this alludes to the main problem with ASP.NET Winforms - it tricks developers into thinking that the web is a connected environment.
What actually happens when you click an <asp:Button /> element by default is that a postback is invoked. I.e. Your browser sends a request to the server for a new page. It sends up something called ViewState which is how the server knows what you've done and what to render. There is no "event" handled as such.
I think the problem is with the way you are trying to get the hidden button
var button2 = document.getElementById("button2");
maybe change this to
var button2 = document.getElementById("<%= button2.ClientID %>");
After the buttons are rendered in the browser, the ID is changed by the ASP.Net engine, and not the same as your source.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
Hope this helps.

Button need to be click twice to fire an event

I have a simple ASP.NET Button inside of iFrame, for strange reason i have to click it twice in order to fire an event.
<asp:Button ID="btnSaveComment" runat="server" Text="Add Comment"
CssClass="button"
OnClick="btnSaveComment_Click"
ValidationGroup="add" />
protected void btnSaveComment_Click(object sender, EventArgs e)
{
AddComment();
}
I suspect that when you initially click an area above the button you're giving focus to the iframe (that is to say, the page within it), and on the second click the mouse can interact with the button.
On the other hand, if the page seems to be posting back but not actually doing anything until the second click, then it might well be related to the page lifecycle, as suggested in an answer comments by #AndroidHustle.
Edit:
To test the theory of whether or not the frame is focused, try giving it focus via script. Something like the following might help:
<script type="text/javascript">
document.getElementById("iFrameId").focus();
</script>
I had the same problem. Here is how I solved it:
$("#MainContent_Button1").hover(function (event) {
$("#MainContent_Button1").click()
});
Now it only takes 1 click, and the button functions like it's supposed to.
<asp:Button ID="Button1" runat="server" Text="Save Payment Plan" OnClick="Button1_Click" />

Categories

Resources