asp.net server side event is not working - asp.net

I have an button in my Jquery mobile page, which is using asp.net webform.
<asp:Button ID="btnSeacrh" runat="server" Text="Search" OnClick="btnSeacrh_Click" />
Which is enabling asp:panel via this simple code on backend
protected void btnSeacrh_Click(object sender, EventArgs e)
{
pnlSearch.Visible = true;
}
Now, when I am clicking this button nothing happened but the URL is extended with the # keyword.
Ex. Previously it was
http://localhost:4989/MobileApp/CreateOrderByText.aspx
After clicking
http://localhost:4989/MobileApp/CreateOrderByText.aspx#/MobileApp/CreateOrderByText.aspx
But it is not showing the panel.
Any help ?

The code you posted looks fine, so I would assume the problem is elsewhere: are there any javascript errors on the page?

Related

code behind and postback in master page doesnt work

i have a very annoying problem...
i had a textbox and a link in my master page. i wanted to use the link button to pass the text of textbox as a query string for some filtring stuff.
but i understood that the cod behind of link bottun doesnt work at all and it just refresh the page.
i tried to do it by jquery by window.location.href but it only worked at the first page and other pages couldnt get the postback.
i changed everything and tried to use radsearchbox.
this control works fine but it only works in every page except the main page.
let me be more clear:
it works great onhttp://kalashabakeh.ir/product.aspx?groupID=1&subgroupID=0
but it doesnt work at www.kalashabakeh.ir
i really dont know what can couse so many problems. maybe my script manager or a js file or something?
plz help me!
here is my current code with radsearchbox:
in masterpage.master:
<telerik:RadSearchBox runat="server" ID="RadSearchBox2"
CssClass="searchBox" Skin="Silk"
Width="200" DropDownSettings-Height="300"
DataSourceID="SqlDataSource_search"
DataTextField="product_name"
DataValueField="product_key"
EmptyMessage="جستجو..."
Filter="Contains"
MaxResultCount="20"
OnSearch="RadSearchBox2_Search">
</telerik:RadSearchBox>
in master page codebehind:
protected void RadSearchBox2_Search(object sender, SearchBoxEventArgs e)
{
Response.Redirect("product.aspx?searchID="+ e.Text.ToString(),false);
}
Is it a typo or something like that. Your Rad searchbox control ID is RadSearchBox2 and it's calling a event handler named RadSearchBox2_Search whereas you are having a handler method named RadSearchBox1_Search. See below pointed
<telerik:RadSearchBox runat="server" ID="RadSearchBox2"
.....
OnSearch="RadSearchBox2_Search"> <--Here
</telerik:RadSearchBox
protected void RadSearchBox1_Search(object sender
^-------- Here
i finally found the answer...
as people discussed here
its a kind of a bug in iis7...
this code solved my problem thank to Eric
just add this code in master page.
public void Page_PreRender(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.Page.Form.Action) && Request.Url.AbsolutePath.ToLower().EndsWith("/default.aspx"))
this.Page.Form.Action = "Default.aspx";
}
Now i can light up my cigurate ...

I get correct redirection after clicking on ASP.NET button but not in a new window as specified

Clicking on ASP.NET button redirects to correct website but on the same tab, not in a new tab what i need to do. What's wrong with the code OnClientClick="aspnetForm.target ='_blank';" below? Why it is not enough alone and what else need to be done?
The following ASP.NET code for the button control is:
<asp:Button ID="btnGenerateReport" runat="server" Text="Generate Report"
OnClick="btnGenerate_Click" OnClientClick="aspnetForm.target ='_blank';" />
I know two methods for redirecting the page to new tab in asp
1) The first method which you are already using and it works also. Make an onclientclick event of Button and on code behind of Button
Click write the following code:-
button.OnClientClick = "aspnetForm.target='_blank'"; Response.Redirect("yourpage.aspx");
2)You can also use javascript
button.Attributes.Add("onclick", "window.open('yourpage.aspx');return false;");
Both the method will redirect your page to new tab on clicking the button.
The error with your code is OnClientClick = "aspnetForm.target='_blank;'" remove the semicolon after '_blank' and it will work
If you are looking out for server side code to open a new window on Button Click, then here's how to do so.
Add the following script to the section of your page
<script language="javascript" type="text/javascript">
function openNewWin(url) {
var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
x.focus();
}
</script>
Then add a Button Control in the following manner
<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"
onclick="btnOpenPop_Click" />
Finally add some code in the code behind file
protected void btnOpenPop_Click(object sender, EventArgs e)
{
string url = "http://www.dotnetcurry.com";
ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
}
You can't use target attribute on button.
You have to use javascript function window.open()
Check this:
Window open() Method
I don't post links lightly, but I found this site explains all and it has solved my problem too.
http://dotnetspidor.blogspot.co.uk/2009/01/open-new-window-in-aspnet-web-page_28.html

How can I stop refreshing the page in the else condition

My code:
protected void btnOk_Click(object sender, EventArgs e)
{
if (txtReportFavorite.Text != string.Empty)
{
//..
}
else
{
Response.Write("<script>alert('Enter Favorite name.')</script>");
// I need to prevent page refresh here.
}
}
How can I stop refreshing the page in the else condition. Thanks.
You can't.
The new page has already been requested when that code runs. If you don't do a postback, that code will never run.
If you want to do the validation without doing the postback, you should do it using client code instead.
The fact that you got to the server side means that your page has done a full cycle to the server and refreshed itself.
Unless you are calling this code with an Ajax call.
You can also achieve this by placing an AjaxUpdatePanel around your button that will simulate an Ajax call when your clients will submit your form.
in your code behind on page load put this
btnOk.Attributes.Add("onclick","return validate();");
in your aspx file have this script
function validate()
{
if(document.getElementById("txtReportFavorite").value == "";
{
alert("Enter Favorite name");
return false;
}
}
Your page is already go to the server side and it is in already postback is progressing.
you have to use client side code for preventing postback.
why not to use RequiredFieldValidator if only empty textbox need to validate?
you can do it on client side.
<asp:TextBox runat="server" id="txtReportFavorite" />
<asp:RequiredFieldValidator runat="server" id="txtReportFavorite" controltovalidate="txtName" errormessage="Enter Favorite name!" />
<br />
<asp:Button runat="server" id="btnSubmit" text="Ok" onclick="btn_Click" />
protected void btnSubmitForm_Click(object sender, EventArgs e)
{
if(Page.IsValid) //for secure validation
{
//do something
}
}
Try using RegisterScriptBlock.
ClientScript.RegisterStartupScript(this.GetType),"","$(document).ready(function(){alert('Enter Favorite name.')});",true);
If you want to perform from server-side do it like above.. Otherwise many answers already posted.
The kind of functionality you are showing can be easily achieved by using a Validator so the page won't post back.
As once it reaches the server, its really not possible to stop the refresh. Well, at least as far as I know.
-Milind

how to prevent refresh on asp.net c# web page button?

how to prevent refresh on asp.net c# web page button?
protected void Button1_Click(object sender, EventArgs e)
{
}
Regards
you must use this way :
<asp:button ID="btn" runat="server" Text="Button" onClientclick="btn_Click(event);"> </asp:button>
<script>
function btn_Click (e)
{
if(//Check somthing)
e.preventDefault();
}
</script>
Short answer, you can't.
However, using methods such as ajax, you can hide the appearance of the postback.
you can do it by creating a cookie inside your Button1_Click method. The first instruction within this method should check if that cookie exists or has not expired. In case itdoesn't exist or it expired, the follow line will create or update the cookie. In case the cookie exists,
return;
Sorry i cant give code cause im using my mobile.

Repeater databound loses data & event on postback - is there a best practice solution?

Currently struggling with a problem that I've encountered variations on in the past. At the moment a worthwhile solution escapes me, but it seems such an obvious issue that I can't help wondering whether or not there's a "best practice" approach I should adopt.
Without code, here's the issues in a nutshell:
page has databound control (a repeater) which isn't populated until user inputs data and clicks a button.
Repeater item template contains a button
User clicks button, page posts back. On load, the repeater is actually empty so event is never handled because the originating control no longer exists
Go back to the beginning of wretched cycle
I've confirmed that this is the problem because if you provide the repeater with some static data on page load, everything works fine. But of course that's no use because it has to be populated dynamically.
Is there a commonly approved way round this headache? I can store the data in session and re-use it on page load, but it seems terribly clumsy.
Cheers,
Matt
If the event is being fired by a button within a repeater then this would bubble up to the repeaters ItemCommand event. Using a buttons CommandName and CommandArgument parameters you can then identify which button was clicked and act accordingly. Below is some basic markup and code behind to demonstrate the approach:
HTML:
<asp:Repeater ID="rptTest" runat="server" onitemcommand="rptTest_ItemCommand"
onitemdatabound="rptTest_ItemDataBound">
<ItemTemplate>
<p>
<asp:Button ID="btnTest" runat="server" />
</p>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnLoad" runat="server" Text="Load" onclick="btnLoad_Click" />
Code behind events:
protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button button = (Button)e.Item.FindControl("btnTest");
button.Text = string.Format("Button {0}", e.Item.DataItem.ToString());
button.CommandName = e.Item.ItemIndex.ToString();
}
protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Response.Write(string.Format("Postback from button {0}", e.CommandName));
}
protected void btnLoad_Click(object sender, EventArgs e)
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
rptTest.DataSource = list;
rptTest.DataBind();
}
Hopefully i've understood the problem and this helps.
If any of your controls are created dynamically, then they have to be recreated during post back in order for the events etc to get hooked back up.
If this is the case, take a look at a control built by a guy named Denis Bauer. We use this with just some slight modifications and it's perfect.

Resources