I am using ASPxGridLookup control and I set AutoPostBack="false" for that control, but when I changed the value the normal page life cycle is getting executed what is the solution for this.
<dx:ASPxGridLookup ID="ASPxGridLookup1" runat="server" KeyFieldName="ID" AutoPostBack="false">
</dx:ASPxGridLookup>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dtLookup = new DataTable();
dtLookup.Columns.Add("ID");
dtLookup.Columns.Add("Name");
dtLookup.Rows.Add("1", "Dorababu");
dtLookup.Rows.Add("2", "Vivekh");
ASPxGridLookup1.DataSource = dtLookup;
ASPxGridLookup1.DataBind();
}
}
If you set AutoPostBack to false, changing ASPxGridLookup value initiates callback, not postback. Callback is a special kind of postback that skips some of lifecycle events and doesn't update ViewState.
DevEx: Discussion about Page.IsPostBack and Page.IsCallback values
DevEx: The Concept of Callbacks
Difference between a Postback and a Callback
Related
I have a custom control which inherits from TextBox.
This allows me to use the TextBox's AutoPostBack property. This property makes the Page_Load method on the parent page fire when I change the value and click out of the text box.
I am setting the value of the rendered text box in JS as follows
var outputData = document.getElementById("CameraScannerTextbox1");
outputData.value = barcode.Value;
When this code runs I am expecting the Page_Load method to run again.
I have tried things like
outputData.focus();
outputData.value = barcode.Value;
outputData.blur();
The code in the Page_Load is
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = CameraScannerTextbox1.Text;
}
}
So basically I am hoping to have whatever is in barcode.Value set on Label1.Text on the server.
All you need is to trigger onchange event for input since ASP.NET adds postback code to onchange attribute. The simplest way is calling onchange manually
var outputData = document.getElementById("CameraScannerTextbox1");
outputData.value = barcode.Value;
outputData.onchange();
For more advanced techniques of simulating onchange event see this and this answers.
You can simply trigger the PostBack yourself with __doPostBack.
<asp:TextBox ID="CameraScannerTextbox1" runat="server" ClientIDMode="Static" AutoPostBack="true"></asp:TextBox>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<script>
var outputData = document.getElementById("CameraScannerTextbox1");
outputData.value = '123456';
__doPostBack('CameraScannerTextbox1', '');
</script>
</asp:PlaceHolder>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = CameraScannerTextbox1.Text;
PlaceHolder1.Visible = false;
}
}
Not that I placed the javascript in a PlaceHolder that is hiddedn on PostBack, otherwise it will create a PostBack loop. But there are other ways to prevent that also.
I have DropDownList inside my ASP page.
When selection changed postback accured and the Page_Load method fired.
I need to get selected item(selectedValue and selectedIndex) in Page_Load method.
I know that I can use selectedIndexChanged event handler, but in my case it is not
suitable solution because of incorrect architecture.
Any idea how to get selected item in DropDownList control in Page_Load method.
With incorrect architecture is it useful to write
var selectedValue = Request.Params[drpDownList.UniqueID];
You should be OK as long as you check for postback -
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var index = ddlDropDown.SelectedIndex;
// do stuff
}
}
I'm assuming that the control isn't dynamically created.
I have a feeling I'm missing something really obvious, I'm not able to capture the selected value of my DropDownList; the value renaubs the first item on the list. I have set the DropListList autopostback property to true. I have a SelectedIndexChangedEvent which is pasted below. This is NOT on the master page.
protected void ddlRestCity_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
r_city = ddlRestCity.SelectedValue.ToString();
}
}
Here is the DropDownList control:
<asp:DropDownList ID="ddlRestCity" runat="server"
Width="100px" AutoPostBack="True"
onselectedindexchanged="ddlRestCity_SelectedIndexChanged">
</asp:DropDownList>
Thanx in advance for your help!
My off the cuff guess is you are maybe re-populating the list on a post back and that is causing the selected index to get reset.
Where is your DataBind() call? Are you checking !IsPostBack before the call? For example:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
ddlRestCity.DataSource = ...;
ddlRestCity.DataBind();
}
}
Explanation: If you don't check for !IsPostBack before DataBind(), the list will re-populate before SelectedIndexChanged is fired (because Page.Load fires before child events such as SelectedIndexChanged). When SelectedIndexChanged is then fired, the "selected item" is now the first item in the newly-populated list.
What is r_city?
If it's a textbox, then you need to do something like r_city.text = ...
Also -- you might consider removing your postback check. Usually, that's most useful in the page.onload event, and usually, you're checking for if NOT ispostback...
For a "dashboard" module I need to dynamically load user controls based on criteria as the user enters the page (role, etc). The problem is that the events are not being fired at all from the controls
As I understand it I need to load the controls in the OnPreInit method of the dashboard page, however I cannot get a reference to the Placeholder control at this point of initialization (i.e. I get a NullReferenceException); trying to load the Placeholder dynamically via Page.FindControl gives me, ironically, a StackOverflowException.
I've tried loading the controls in PreRender and OnInit as well but the events in the controls are not wired up properly and will not fire.
The code is basically this:
// this does not work; if I try to access the placeholder control itself
// ("phDashboardControls") I get a NullReferenceException, if I try
// Page.FindControl("phDashboardControls") I get a StackOverflowException
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
Control ph = Page.FindControl("phDashBoardControls"); // Placeholder
if (ph != null)
{
// GetControlsToLoad just instantiates the controls and returns
// an IList<Control>. Eventually it will have logic to
// determine which control needs to be loaded based on user role etc.
foreach (Control control in GetControlsToLoad())
{
ph.Controls.Add(control);
}
}
}
// IModularControl is a custom interface defining a single
// Initialize method to set up a control...
private void Page_Load(object sender, EventArgs e)
{
foreach (Control control in this.phDashboardControls.Controls)
{
if (control is IModularControl)
((IModularControl)control).Initialize(this.CompanyID);
}
}
I've successfully loaded controls dynamically in Page_Load before. The only thing I found I had to be careful of was to ensure that if I did a postback, the same controls were loaded in subsequent page_load to ensure that the view state didn't get corrupted... all events etc worked as expected. In my case the controls flow ended up something like this:
page_load - load control a
(do something which causes postback and event x to fire)
page_load - make sure you load control a
event_x - clear control a, load control b
(do something which causes postback)
page_load - make sure you load control b
...
it meant loading controls you fully intented discarding, but was the only way I could find to not corrupt the viewstate...
If you have a page with PlaceHolder1 and Label1 in it, then the following code causes the button click event to fire just fine:
protected void Page_Load(object sender, EventArgs e)
{
var dynamicButton = new Button() { Text = "Click me" };
dynamicButton.Click +=new EventHandler(dynamicButton_Click);
PlaceHolder1.Controls.Add(dynamicButton);
}
void dynamicButton_Click(object sender, EventArgs e)
{
Label1.Text = "Clicked button";
}
Behaves the same with a user control:
WebUserControl ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Click Me" onclick="Button1_Click" />
WebUserControl code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Clicked Button";
}
parent control that loads the child control:
protected void Page_Load(object sender, EventArgs e)
{
var dynamicControl = Page.LoadControl("~/WebUserControl.ascx");
PlaceHolder1.Controls.Add(dynamicControl);
}
Just FYI the issue had to do with validation; the events weren't firing properly because some of the validation controls (there were a ton) weren't configured to only apply to that control.
Although, questions somehow similar to this have been asked for a number of times, but the question is still unsolved. Here is the question:
I have a GridView which is contained in a tab container AJAX control which itself is inside an UpdatePanel. Gridview works excellent and its corresponding methods are fired accurately, but when I enable paging(e.g.) after I click on page 2, the GridView hides itself. here is my PageIndexChanging() method:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
UpdatePanel2.Update();
}
Why paging causes GridView to stop working correctly? What can I do?
The solution is that you should refill the dataset which is used to populate the gridview, each time your page index is changed. By this way, you could ensure that in each seperate postback which has been triggered by the gridview page number, results will be populated.
I just tried that above code. I had same issue and now it is working just fine.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
// UpdatePanel2.Update(); <-- Remove this line from your code.
}
I have GridView inside update panel. Did you write your event PageIndexChanging in your .aspx file also?
Hope this helps.
Further research:
http://msdn.microsoft.com/en-us/library/cc295545.aspx
Controls that are not compatible with UpdatePanel controls
The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:
GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.
I had the same issue , changing the updatepanel property UpdateMode="Conditional" to UpdateMode="Always" and setting the property ChildrenAsTriggers="true" solved the problem for me.
To do it, you have to re set the datasource in the page index change event. The performance will be lower but that's the way you can make it works.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.DataSource = ;//Set again the datasource
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
UpdatePanel2.Update();
}