ASP.NET: Textbox lost Enabled value on postback - asp.net

I have a textbox, that programatically i set it 'Enabled = false'. When the page, after this change, get a postback, this textbox return the enabled value to true. why?
I have the textbox into a updatepanel.

If you're setting it in code-behind in the Page_Load event, you need to remember that Page_Load occurs even on postbacks. You have two options.
Place it with in a block that checks to see if the page is a postback.
if(!Page.IsPostBack)
{
TextBox1.Enabled = false;
}
or set it in Page_Init instead.
If you're not already familiar with it, be sure you understand the Page Lifecycle. This is must-know information for ASP.NET developers. Read about it at http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

In the page_load, do the following:
If (IsPostBack) Then
Textbox1.Enabled = False
End If

Related

ASP.NET page maintain State after postback with EnableViewState = false

In ASP.NET webpage, I have set the EnableViewState to false to the page and all the controls of the page. Still I am getting the PostBack data in the Controls.
Is there any way to remove this?
How do we remove the page life cycle event like restore PostBackData?
Actually _ViewState is maintained for all controls. I am not clarified from MSDN as well.
Postback data has nothing to do with ViewState.
If you don't want Postback data, why are you using Post? What are you trying to achieve?
Because you re-bind your datas after each PostBack
You can use !IsPostBack in your Bind
if(!IsPostBack)
{
//Bind your datas just for load, and disable your ViewState, that will erase your selected datas
}

When is DataBind called automatically on an ASP.NET page?

I have a GridView on a page with a search button. The GridView is not visible to start with so that the user must click search before results are retrieved. The DataSourceID is set to the ID of an ObjectDataSource. When click is called, the following method is called from the click handler:
private void PopulateGrid()
{
gv.Visible = true;
gv.DataBind();
}
A problem occurs when the same method is called from the Page_Load handler. We store a user's search terms in their session and retrieve them the first time the page is accessed, something like this:
if(!PostBack && Session["search"] != null)
{
SetSearchFromSession();
PopulateGrid();
}
The problem in this case is that the ObjectDataSource's Selecting event is fired twice. Once when the GridView is made Visible, and again when DataBind() is called. I fixed this by substituting gv.Visible = true; for PopulateGrid(); in Page_Load.
But I'd like to understand what is going on. Why does setting GridView visible from page load result in DataBinding when a call in a button click event doesn't?
If you declaratively set the datasourceid then it is going to get called after PreRender and if you call DataBind it will be called again. (twice)
DataBinding
Raised after the control's PreRender event, which occurs after the
page's PreRender event. (This applies to controls whose DataSourceID
property is set declaratively. Otherwise the event happens when you
call the control's DataBind method.)
This event marks the beginning of the process that binds the control
to the data. Use this event to manually open database connections, if
required, and to set parameter values dynamically before a query is
run.
source

Rebind to asp:Listbox on postback

When a postback occurs on my page, changes are made in my database and I want those changes to be reflected in my ListBox. It has a datasource that is set up in the .aspx side of things (DataSource = "myDataSource"). Right now, on a postback, these changes are not visible. How can I "Rebind" my ListBox?
try this :
In your event handler that is causing the postback try adding this at the end (say Button1_Click if it a postback caused by a button)
ListBox1.DataBind();
it should cause the datasource to refresh.

Conditionally trigger a full page postback from a link button inside an update panel

How do I conditionally trigger a full page postback from a link button inside of an update panel?
I have a custom control that contains its own updatepanel with a link button nested inside of it. When the link button is pressed I want its event handler to have the option of either letting the control update as normal or doing a full postback on the page.
Here is the control hierarchy:
Page
Custom Control
UpdatePanel
LinkButton
Event handler Pseudo code:
LinkButton Click Handler Begin
If is a partial post back AND a full postback is needed
Page.DoFullPostback
End If
End Handler
Note: I aways need the partial postback to happen. I was considering injecting a __DoPostback in the controls markup but this seems hacky to me.
Thanks for your help!
Sorry, I'm not familiar with the VB, so my example source will be written in C#:
protected void btnLink_Click(object sender, EventArgs e)
{
bool isAsync = ScriptManager.GetCurrent(Page).IsInAsyncPostBack;
bool postBackIsNeeded = true;
if (isAsync && postBackIsNeeded)
{
ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnClick);
string postback = Page.ClientScript.GetPostBackEventReference(
btnClick,
string.Empty
);
ScriptManager.RegisterStartupScript(
btnClick,
btnClick.GetType(),
"postback",
postback,
true
);
}
}
The main idea is to change the type of postback of your LinkButton control. If neccessary it should be changed to full postback instead of partial during the async postback event. Right after this, another postback script should be generated and it should be executed as soon as the page will be returned to client.
And the last thing - use loop detection condition (if (isAsync && postBackIsNeeded) in my case) otherwise postback will be infinite.
The easiest approach is to create a hidden button somewhere on your page, outside of any UpdatePanels. When you need to do a full postback, use JavaScript to either click the button or issue __doPostback() to it. You can achieve a partial postpack programmatically in JavaScript by calling __doPostback() on an UpdatePanel itself, or to a button inside one.

DropDownList_OnSelectedIndexChanged event, In a UserControl is not firing on postback

I forgot to mention this asp.net 2.0.
The user control has a unique id and it is loaded in the PageLoad event.
The user control is loaded into a panel and the panel is inside of a webpart.
The dropdown has autopostback set to true.
EnableViewState = true on the dropdown.
The ListItems are created in the dropdowns pre-render event method.
This is why I don't understand why it is not firing, the dropdown is the only thing that causes postback on this user control.
The event methods for the dropdown should occur since the user control is loaded in the page load method on postback again right?
Make sure there is no OnLoad or PageLoad event that is rebinding the datasource of the dropdown list. Rebinding the data with a new set of data may cause the clickhandler to not ever get executed.
make sure you have if (!Page.IsPostBack) around dropdownlist.datasource = and dropdownlist.databind()
I am not sure if this is your problem, but it is the most common.
Try with EnableViewState set to
true for the DropDownList
If the ViewState is set to false, on post back the selected Index gets back to default which is normally the first Item. First item, if selected, does not cause SelectedIndexChange event to fire

Resources