code behind and postback in master page doesnt work - asp.net

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

Related

asp.net - RadioButton "CheckedChanged" event does not work when it is unchecked

I have two RadioButtons on my Web Form and here is the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToLongTimeString());
}
When I check RadioButton2, time appears on the page but when I check RadioButton1(RadioButton2 is unchecked now) it does not update the shown time and removes it.
the event function must be called when it checks and unchecks.I don't understand what is wrong here and why the time disappears
(As it is obvious, I know about GroupName property and AutoPostBack. My problem is something else)
You can check property of each control and check AutoPostBack=True
and also if checked="true"
I tried it on different ways and opened my pages with different browsers and I figured out a few things. the most important of them which is related to my problem:
When a control Calls back, the page refreshes and that is why the time disappears. It sends the status of all controls using post method to itself and they would be the same way as they were. But the time is not written on a control and it would not be saved.
As I said if it was for example on a Label, the text would be kept:
Label1.Text = DateTime.Now.ToLongTimeString();
It answers the first question but I still think that the event function must be called when the RadioButton is being unchecked.

asp.net dynamic user control button click issue

I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.
First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.

asp.net server side event is not working

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?

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.

ASP.NET navigate to anchor in code behind

I have a simple Web Form with code like this:
//...
//tons of text
//...
<a name="message" />
//...
//tons of text
//...
<asp:Button ID="ButtonSend"
runat="server"
text="Send"
onclick="ButtonSend_Click" />
After POST I want to navigate user to my anchor "message". I have following code for this:
protected void ButtonSend_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(),
"navigate",
"window.location.hash='#message';",
true);
}
This simple JavaScript is not working in Firefox 3.5.2 - url is changing in browser but page is not navigated to anchor. In IE 8 it works perfectly.
Why this JavaScript code is not working in Firefox? Am I missing something?
I've solved my problem. JavaScript code was called before my anchor even existed. That's why Firefox wasn't scroll page down.
My code looks now like tihs:
this.ClientScript.RegisterStartupScript(this.GetType(),
"navigate",
"window.onload = function() {window.location.hash='#message';}",
true);
After page load I'm calling my simple JavaScript function.
The key to found solution was Cleiton answer. Firebug reports that getElementById was returning null reference. Then I looked at generated HTML like andrewWinn suggested - JavaScript was called before anchor existed. Made little googling and found solution.
Thanks for your answers!
Mendoza, you can use native scrollIntoView function.
To do what you want, just write:
this.ClientScript.RegisterStartupScript(this.GetType(),
"navigate",
"document.getElementById('id_of_the_element').scrollIntoView();",
true);
I ran into this once. Have you taken a look at the actual HTML? If I remember, FireFox was being case sensative on the anchors. I don't know if that changed recently or not.
You could try using the jQuery LocalScroll plugin. Using this, you could scroll using:
$(function() {
$.scrollTo("#message");
});
Or with your ASP.NET code:
protected void ButtonSend_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(),
"navigate",
"$(function() { $.scrollTo('#message'); });",
true);
}
It's not an ideal solution, especially if you're not using jQuery anywhere in your project already, but it might solve your problem.

Resources