In my application there is a grid which gets loaded once the user click on row which has some data, but now my requirement is like I want to load the grid at the time when page gets loaded, so for that I just want to call the Click event to happen automatically at the Page_Prerender itself.
Can anyone please help me out with the solution.
Say you have a grid view like this.
<GridView ID="gvChildGrid" runat="server" />
You can easily data bind it in Page_PreRender event with
protected override void OnPreRender(EventArgs args)
{
gvChildGrid.DataSource = yourdatasource;
gvChildGrid.DataBind();
}
Related
I have a DropDownList in my page:
<asp:DropDownList ID="ddlPra" ClientIDMode="Static" CssClass="chosen-select" runat="server" OnSelectedIndexChanged="Practice_SelectedIndexChanged"></asp:DropDownList>
Code Behind:
protected void Practice_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(ddlCli.SelectedItem.Text);
}
I also have an UpdatePanel which has a GridView that displays some data with links. The issue I am having is when the page loads and I change the option from the select drop down, I don't get an alert but when I click on anything inside theUpdatePanel` the alert is displayed.
How do I fix it so that the DropDownList works independent from the UpdatePanel
Why DropDownList event fires after another event?
Because you have not set AutoPostBack="true", it is false by default. That means that it will not post back immediately after the user selected another item. But the event will be triggered on the next postback anyway, independent of the control that caused it.
I have a user control with a DropDownList with AutoPostBack = true, also I have an aspx page to display this control.
I re-create user control in OnInit method of the page
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var list = (List<Control>)Session[Controls];
if (list != null)
{
foreach (var control in list)
{
var uc = (Control)LoadControl(ControlPath);
uc.SetDropDownState(control.state);
PlaceHolderQuestion.Controls.Add(uc);
}
}
}
So while re-creating I am restoring DropDown selection, it works for the first time, but when I change selection again, OnSelectedIndexChanged event does not fire and it is obvious because I first restore DropDown selection in OnInit and so no OnSelectedIndexChanged event, cause nothing was changed, can you suggest some workaround?
UPDATE
var uc = (Control)LoadControl(ControlPath);
is required to be keep user controls events
After a deep research, I figured out that the issue was in the SelectedIndexChanged event logic.
I am using "Indirect Subscription" approach to handle it within my Page logic,
for more info please take a look
Indirect Subscription Approach
Originally I was reloading controls inside the handler but it was not required since I am doing it in the Page_Load method as well, so all I need to do is to update control inside my PlaceHolder controls list.
I hope it is clear, if not, please ask, and I will provide more description.
I am experiencing the almost same issue specified in this question. Can anybody please post the answer for this? The question is not answered in a clear way.
Issue with dynamically loading a user control on button click
In this aforementioned question, he is loading the control to a placeholder present in the first user control. My scenario is slightly different. My scenario is i have a single aspx page, UserControl1 and UserControl2. At the very beginning, I will load UserControl1 to Page. Then I need to unload userControl1 and load UserControl2 to Page when user clicks a button from UserControl1.
Events must be registered on page load not later. Your control is created during event handling and its event is not registered.
Take a look:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
You need to create a custom event handler for UserControl1 and bubble the event up to the page when the button is clicked.
Create a custom event handler for UserControl1:
public event EventHandler UpdateButtonClick;
public void OnUpdateButtonClick(EventArgs e)
{
if (UpdateButtonClick!= null)
UpdateButtonClick(this, e);
}
Assign the event handler for the UserControl1:
<uc:UserControl1 ID="UserControl1" runat="server"
OnUpdateButtonClick="UserControl1_UpdateButtonClick" ... />
Handle the event in the code-behind:
protected void UserControl1_UpdateButtonClick(object sender, EventArgs e)
{
UserControl1.Visible = false;
UserControl2.Visible = true;
}
I am developing an asp web page in which I have a drop down combobox and a place holder below that. When the user selects an item from the drop down combobox, a postback is done to the server side and server loads an asp user control to the place holder in this parent page. Everything upto now is working fine.
In the user control I have a button and the user control code behind is implemented to handle the button click event. The problem is, when I click this button, I can see that the postback is send to the server side (i.e. parent page Page_Load() is invoked in debug mode), but both the user control's Page_Load() or button click event handler is not invoked.
Please help..
Some additional information,
My parent page is not an asp master page. Just a simple asp page.
I am using VS2008 and .Net 3.5 SP1 and C#.
You need to ensure that you UserControl exists so the button click event is triggered when viewstate is rebuilt.
Loading your UserControl in the Page_Load will work the first time. When you click the button and the post_back occurs, Page_Load has not occurred yet. This means the UserControl will not exist, which mean the button does not exist for the event to be wired back up. So the UserControl with the button in it cannot be connected to the click event and the click event wont fire.
Recommend that your user control is loaded in this event.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//-- Create your controls here
}
Try a sandbox test. In page_load, dynamically create a button with a click event in the Page_Load. You will see that the click event does not fire. Now move the button to the OnLoad event. The click event will fire. Also note, the click event will occur before the Page_Load event. Further proof that the button does not exist at the right time.
Another idea...
You are reloading the usercontrol on the page before the button event occurs. Ensure your LoadControl method is inside the If block
if (!IsPostBack)
{
//load usercontrol
}
Default.aspx
<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
var ctl = LoadControl("Controls/UserControl.ascx");
ph1.Controls.Add(ctl);
}
UserControl.ascx
<h3>User control</h3>
<asp:Button ID="btn1" runat="server" OnClick="btn1_Click" Text ="Click me" />
UserControl.ascx.cs
protected void btn1_Click(object s, EventArgs e)
{
Response.Write("You clicked me, yay");
}
All works like a charm. I see the "You clicked me, yay" written when I click the button
Point of attention. If you try to load the controls dynamically in your example in the handler for SelectedItemChanged event of the dropdown control, it will fail, because of the way that lifecycle works for ASP.Net page.
Instead you should handle such control creation in the PageLoad event of the page, like this example below
Default.aspx
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true">
<asp:ListItem Value="0" Text="--select a value--" />
<asp:ListItem Value="1" Text="User control 1" />
<asp:ListItem Value="2" Text="User control 2" />
</asp:DropDownList>
<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
switch (ddl1.SelectedValue)
{
case "1":
var ctl = LoadControl("Controls/UserControl.ascx");
ph1.Controls.Add(ctl);
break;
case "2":
ctl = LoadControl("Controls/UserControl2.ascx");
ph1.Controls.Add(ctl);
break;
}
}
}
In my particular case, I found that the problem was the UserControl ID (or rather the lack of).
When the UserControl was first instantiated, my button ID was ctl00$ctl02$btnContinue, but after the postback it had changed to ctl00$ctl03$btnContinue, therefore the button event handler didn't fire.
I instead added my UserControl with a fixed ID and the button now always loads with the ID ctl00$myUserControl$btnContinue.
I am using Telerik grid control and LinqDataSource control on a page.
code :
<asp:LinqDataSource ID="ldsFillGridData" runat="server" OnSelecting="ldsFillGridData_Selecting" />
all things working fine now i want to call its
protected void ldsFillGridData_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = from tc in db.city select tc;
}
this selecting event calling from some button click event. How???
I am trying like
ldsFillGridData_Selecting(null, null);
but it throws an error.
How to regenerate grid data on various button click event of a page while we are using telerik grid with LinqDataSource?
Just use Rebind() function of this telerik grid control, it will automatically calls ldsFillGridData_Selecting event and it shows updated records in Grid.
RadGrid.rebind();
RadGrid1.Rebind();
Will rebind the grid, calling the select method of your datasource (as long as the linqdatasource is indicated as datasource for the grid).