textbox's value not correct when displayed in browser - asp.net

I have a simple textbox on my page with an embedded code block where I set its value. But in the browser it still displays "oldvalue". Can't figure out why..
<asp:TextBox id="textBox" runat="server" Text="oldvalue">
</asp:TextBox>
<%
var box = FindControl("textBox") as TextBox;
box.Text = "newvalue";
%>

This will not work as the inline expressions <% %> are executed after prerender event in the asp.net page life cycle.
And the last changes you can make to the contents of the page or its controls is upto PreRender event, So that any changes in the view state of the server control can be saved during this event. MSDN reference here.
So, rather than using inline expressions, use any event upto PreRender events of Page life cycle in your code to change the TextBox.Text property.

Related

trigger postback between two aspx pages with change in asp:dropdown value

I am having an aspx page with div tag:
<div id="report"><div>
I am calling other page with the help of javascript function because the "id" decides what drop-downs should be able on page.
function loadreport(id){
$("#report").load('/lists.aspx?id=' + id + ' #reportdropdowns', function () {
});}
In that lists.aspx i am having couple of dropdowns.
<asp:DropDownList ID="Account" AutoPostBack="true" AppendDataBoundItems="true" runat="server">
<asp:DropDownList ID="Company" AppendDataBoundItems="true" runat="server">
When i change a value from account drop-down, it is triggering postback event, but that event is coming back to report.aspx instead of lists.aspx.
I thought of catching that postback in page_load of report.aspx and call loadreport() using
Page.RegisterStartUpScript();
Then it has thrown error of "id" not defined. Keeping that aside, whole report.aspx is getting refreshed.
Is there any way, i can do postback to lists.aspx by passing selected value of first drop-down and only refresh second drop-down instead of whole page.
If you load aspx page through ajax request and assign html to div then all your postback control event will not work with your page that is loaded via ajax request.
To achieve this you have to use ajax request then it will work
Another way to make this work is to do a full page load in an iframe

ASP.NET webform Fileupload inside an ascx which is inside an updatepanel in the aspx

here is some code in my page :
<asp:UpdatePanel ID="UpdatePanelEQSelector" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc12:EQSelector ID="custEQSelector" OnEqChange="custEQSelector_OnEqChange" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Inside my user Control, I'm asked to add a fileuploader.
After coding it simply, I tested and my fileuploader is always empty.
I've searched a while and discovered that it was a normal behavior.
The solution to make it work is to add a PostBackTrigger for the updatePanel.
When I tested it in my aspx page, I achieved to do it and my fileUploader had the file.
Then I tried to add it dynamically (to finally do it in my control), it worked with that :
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = this.btnTest.ID;
this._UpdatePanelEQSelector.Triggers.Add(trigger);
But I can't manage to make this code work in my control (I passed my updatePanel as a parameter to my control set in Load, the fileUpload is always empty)
Do you see a solution ?
Thanks
Does it postback, but leaves the control empty? or is it not posting back at all. If it's not posting back at all I'd add this
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnTest);
to the page_load of the user control.
If it is posting back, but the control is empty, then I'd wager there is probably some dodgy html somewhere on the page and the values are getting lost.

How to enable html select post back apsx page when selection changed?

With asp.NET control dropdownlist, there is a property AutoPostBack, if it is set "True", the whole page will be posted back.
If the aspx page include a html element "select" like:
<select id="list" name="list" runat="server"
DataTextField="Name" DataValueField="ID" ></select>
and it data is filled by code-behind.
Question is: how to allow this Select have AutoPostBack function too?
The DropDownList approach adds a __doPostBack('selectelementname', 'commandname'); call to the onchange event. When you change the value, this then proceeds to post back to the server, and then the ASP.NET control processes the postback data in LoadPostData method.
HTH.
You can't apply Auto post back property for html select control.To invoke function writen inside c# code page(serverside),you need to use webservice. You can call javascript function(client side) on 'onchange' event of html select control.

asp:ImageButton not firing onclick event

I have a page that uses a master page, several RequiredFieldValidators, and the Web Toolkit autocomplete extender. The following code only shows the bare minimum of the page:
<%# Page Language="C#"
AutoEventWireup="true"
CodeFile="Login.aspx.cs"
MasterPageFile="~/master.master"
Inherits="Login" %>
<asp:Content id="Content1"
contentplaceholderid="ContentPlaceHolder1"
runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<div>
<asp:ImageButton class="submitButton"
imageurl="images/button_submit.gif"
id="btnSubmit"
runat="server"
onclick="btnSubmit_ServerClick"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Code-behind:
protected void btnSubmit_ServerClick
(object sender, ImageClickEventArgs e)
{
//breakpoint here does not get hit
}
The <form runat="server"> tag is in the master page. The code above does not fire the onclick event. If I get rid of the master page and add a form tag to the page, it works. Is the form tag in the master page not supported, or is this supposed to work somehow?
alt text http://digitalcopy.warnerbros.com/images/mainmenu.gif?provider=00079&disc=03403AAA-1D20-47F2-91FA-5EE632832659
You can also check if your ImageButton does not trigger validation. If it does set its CausesValidation property to false (of course if it makes sense).
I had a similar issue (different scenario). I used Page.RegisterRequiresRaiseEvent(ImageButton) and my onclick event started to fire. Why I needed to do that? I don't know.
My solution was to set the ImageButton's CausesValidation to false.
I have a similar issue with the image button and found the root cause. You are using
"ib.ID = i + ":" + j;"
as the ID of the ImageButton, the ":" is illegal name to use, as you are creating it programmatically, ASP.NET allows it to be created.
At runtime, if you look at the HTML source of the page, you will see the special characters are either ignored or replaced with "_". So the page is unable to find the correct control, thus the event won't fire. Try changing the name with plain text, the event will fire.
ib.ID = i + ":" + j;
should be changed to
ib.ID = i.toString()+":"+j.toString();
If it still doesn't work try making use of the StringBuilder to buildup the ID and assign it later to ib.ID property
This is solution that worked for me
If you are binding through data bound controls then use OnCommand attr instead of OnClick attr
You have to have the control in a form runat=server somewhere, it can be in the Master page or the .aspx file. Double check that the master page form tag is runat=server
AutoEventWireup is the property that allows the syntax you are using. Double check the setting in the Master Page, WebForm and it can also be set in the web.config.
if that doesnt work, you can always explicilty code it (which I prefer)
<script runat=server>
protected override void OnInit(EventArgs e)
{
btnSubmit.Click += delegate(object sender, EventArgs e1)
{
};
base.OnInit(e);
}
</script>
UpdatePanel can mess with server side events being raised also, so try it without the UpdatePanel. And I am sure you have a ScriptManager in the Master Page
From the code you supplied, you seem to be missing the <asp:scriptmanager> from your page. You must do one of the following:
Have the <asp:scriptmanagerproxy> on the page and the <asp:scriptmanager> on the master page.
Have <asp:scriptmanager> on your page and no <asp:scriptmanager> on the master page.
Personally, I recommend having the <form> tag on the master page, but that's personal preference.
You can always try taking out the UpdatePanel and seeing if it works. I usually start without the UpdatePanel, get everything working the way I want and then add in the UpdatePanel and debug anything that causes.
The form in the MasterPage works for me so the ScriptManager/ScriptManagerProxy mentioned by #Keltex might be an issue, though I forget them sometimes and usually get away with it.
With the UpdatePanel the button's click event will be handled via Javascript, so you might grab FireBug or equivalent (depending on browser) and follow through what actually is happening. Is it tripping on the validation and you don't see it? Is there a JS error somewhere (the Control Toolkit isn't perfect always)? Is the page actually posting back at all and just not hitting the event handler?
On my webpage i am creating imagebuttons dynamically inside a table that is contained by an updatepanel. The buttons are created by this code:
for (int i = 0; i < 15; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 20; j++)
{
TableCell tc = new TableCell();
ImageButton ib = new ImageButton();
ib.Click += new ImageClickEventHandler(ImageButton1_Click);
ib.ImageUrl = "../img/defaultCell.jpg";
ib.ID = i + ":" + j;
tc.Controls.Add(ib);
tc.Width = 25;
tc.Height = 25;
tr.Cells.Add(tc);
}
GameTable.Rows.Add(tr);
}
}
The image buttons will not trigger click events. HOWEVER, if the line 'ib.ID = ...' is commented out, they do! That single alternation seems to fix all the issues.
I have no idea why.
If anyone can explain this, and also tell me how to trigger events keeping the ability to set button id's, i'd be much thankful
I think it may have something to do with the fact your re-assigning the id's after creating & assigning the event handler?
Do you even need to assign the id's? - Surely this is done for you anyway? - True removing the 'ib.ID = i + ":" + j;'
Make sure you use OnClick rather than onClick
Your update panel could be messing with the postback. Try it without the UpdatePanel and see if that is the culprit.
I had the same problem that OnClick event of ImageButton was not firing. But the actual problem was, at form level, I had onSubmit="return false;"
I was facing the same issue, where I was dynamically creating an ImageButton and click of that event was not triggering. hence, i created the Image button in if (IsPostBack) . Now it is working fine. And even if the page gets refresh, the ImageButton will be retained.
I've just solved a similar issue where OutputCache was enabled. When changing from asp:ImageButton to asp:Button, the event is correctly fired. Probably asp:ImageButton has some bug with OutputCache.
After none of the above suggestions worked for me, I did one more try by calling the button creation in OnInit(). This fixed my issue and now the OnClick event is firing.

How to populate UpdatePanel in Repeater in ASPX (not code-behind)?

I have a Repeater which displays a list of items (in a grid-like/table view). When a user clicks an item, I display an UpdatePanel under this item with additional information relevant to the item (similar to Accordion control). I know how to populate the elements of the UpdatePanel in code-behind (I pass the ID of the selected element in the Repeater control as CommandArgument, get additional info for this ID, and set up the text fields of the active UpdatePanel controls). But I'm wondering if I could set up binding directly in the ASPX (instead of code-behind). When I used the <%= %> syntax to assign text fields of the UpdatePanel control the values of the page properties, e.g. <%= Comment %>, it sort of worked, but it changed the fields of all UpdatePanels in the repeater. Is there any way to bind the active UpdatePanel to the current values and leave already bound UpdatePanels unchanged?
Are you looking to display a container that displays additional information? Is there other activity in the "box" that requires it be an updatepanel?
<asp:repeater>
<itemtemplate>
<%# Eval("Name") %> <%# Eval("LastName") %><br />
<span onclick="$get('<%# Eval("Id") %>')">View Age</span>
<div id="<%# Eval("Id")%>" style="display:none;">
Age: <%# Eval("Age") %>
</div>
<itemtemplate>
</asp:repeater>
Ithink that's right, some syntax may be off a bit (typing without intellisense). Would that work?
I used ID as a unique identifier for the div id and the onclick command. You could also use jquery, asp:controls or whatever else you wanted.
Easiest way is to nest a FormView inside the update panel. Then the only thing you need to do in the code behind is get the additional info, assign it to the FormView.DataSource, and call FormView.DataBind(). Everything in the FormView will use the <%# Eval("SomeColumn") %> syntax. You'll probably need to use a FindControl() to get a reference to the FormView. I'd type up the code for you but I'll save you some headaches down the road and say DON'T DO THIS.
The update panel is about the most inefficient way to do any ajax stuff. The only way to get it all to wire up correctly with this repeater and server side code is to either have a gigantic viewstate or to rebind the repeater in your page load. You are turning a request that could be 300ms into something that will take over a second...or longer! Get familiar with a good ajax framework and don't be afraid to write real html. At the very least, use a webservice that loads a usercontrol with your markup.
I know the update panel is easy, and it's built in. It might even be adequate for what you are doing, but you must resist. You'll be glad you did.

Resources