I have a usercontrol containing a few textboxes and a submit button which runs inside a larger page, I have a few panels in the usercontrol so that when the user clicks submit the panels I toggle the visibility of the form panel with a panel saying "you have submitted". The problem I am having is that when the form is submitted it refreshes the whole page. I am wondering if there is any quick modification I can make to the usercontrol to have it only refresh itself without rewiring the form in ajax?
protected void Button1_Click(object sender, EventArgs e)
{
DatabaseConnection connection = new DatabaseConnection();
//Verify if entry exists
if (CheckValid())
{
//Register data
pnlSuccess.Visible = true;
}
else
{
pnlDejaRepondu.Visible = true;
}
pnlForm.Visible = false;
}
You can enclose content of the user control into an UpdatePanel.
UpdatePanel will cause partial postback of the user control content only, leaving the rest of the page intact.
Related
I want to control the visibility of panel(true/false) in ASP.NET webforms based on a button click. If both the controls were in single page, I could have directly accessed it as panel1.visible = true in the button click event but button and panels are in different pages. On button click, I should redirect to a certain page and a certain panel should be made visible. How to achieve this.?
Here's an example using QueryString
Redirection code to be behind your button
bool show = true; // Change accordingly
Response.Redirect("~/NextPage.aspx?show=" + show.ToString());
Retrieval code to be behind your destination page
protected void NextPage_Load(object sender, EventArgs e)
{
string qString = Request.QueryString["show"];
if (qString != null)
{
MyPanel.Visible = Convert.ToBoolean(qString);
}
}
I have a user control inside an aspx page, after the aspx page loads, when I click some button inside that user control, I want the focus to come back to user control after that button click action is complete and the aspx page loads again.
You need to have an event in your user control that will allow the .aspx page to subscribe to that event so that it can set focus to the element in the user control after the form posts back, like this:
public class UserControlClass
{
// Define event that will be raised by user control to anyone interested in handling the event
public event UC_Button1ClickEventHandler UC_Button1Click;
public delegate void UC_Button1ClickEventHandler();
// Mechanism to allow event to be raised by user control
private void Button1_Click(System.Object sender, System.EventArgs e)
{
if (UC_Button1Click != null)
{
UC_Button1Click();
}
}
}
Now in your .aspx page, you need to subscribe to the event in the user control and say what method will actually handle the event, like this:
userControl1.UC_Button1Click += Button1_Click;
Finally, the click event handler needs to exist, like this:
public void Button1_Click(object sender, EventArgs args)
{
// Set focus here to user control element, text box for example
((TextBox)userControl.FindControl("TextBox1")).Focus();
}
I am fairly new to the asp.net and experimenting with it to learn the page life cycle. Here is a problem that I have been unable to resolve for past few days.
I have a hosting page (.aspx). Then I have two user controls (.ascx). The page has a place holder control in which it loads the user controls one at a time based on the application flow. First user control is loaded on application start up. It has a "continue" button. Continue button click loads the Second user control that has two buttons - "Back" and "Submit". Obviously the "Back" button should load the first user control again and Submit button should submit the form data. Pretty simple.
The problem is that the command button event handler that I have on the second user control is not firing the first time. (I have one event handler for both buttons). The load event of the user control fires but then it ignores the button click. If I click it again, then it fires. I re-load the controls on the page in every page_load. Here is some relevent code:
AddPlayer.aspx:
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Clear();
// Load the ctlInputPlayer control
Control ctlToAdd = LoadControl("ctlInputPlayer.ascx", null);
if (ctlToAdd != null)
{
_ctlInputPlayer = (ctlInputPlayer)ctlToAdd;
_ctlInputPlayer.SendPlayerData += new EventHandler(ctlInputPlayer_SendPlayerData);
PlaceHolder1.Controls.Add(_ctlInputPlayer);
}
// see if there is player data available in the view State
PlayerData player = (PlayerData)ViewState["Player"];
if (player != null)
{
ctlToAdd = LoadControl("ctlPlayerInfo.ascx", player);
if (ctlToAdd != null)
{
_ctlPlayerInfo = (ctlPlayerInfo)ctlToAdd;
_ctlPlayerInfo.SubmitPlayerData += new EventHandler(ctlPlayerInfo_SubmitPlayerData);
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(_ctlPlayerInfo);
}
}
}
ctlPlayerInfo.ascx (second user control):
ctlPlayerInfo.ascx.cs
protected void CommandBtn_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Submit":
//submitPlayerData will fire here
break;
case "Back":
// editplayer data will fire here
break;
}
}
protected void Page_Load(object sender, EventArgs e)
{
// load control data here....
}
The page_Load fires every time but "CommandBtn_Click" doesn't fire after the first click. I have to do it click it again. It doesn't matter which order I click the buttons.
I appreciate the help. Let me know if more details are needed. Thanks!
You should load your user controls every time in Page_Init and set the ID property.
I have a user control which contains a grid and three buttons for add,edit and delete.
I have placed this user control on an asp.net page.
I have OnClick events for these buttons.
When i click on add and delete buttons it's working fine but when i click on edit button,the onclick event of edit button is fired but the row in the grid doesn't appear in the edit mode, i have to click two times.
I don't know where is the problem.The onclick event handler for edit button is as follows:
protected void btnEditBankAccount_Click(object sender, EventArgs e)
{
grdBankAccounts.EditIndex = grdBankAccounts.SelectedIndex;
grdBankAccounts.RowSelectingEnabled = false;
}
Anyone please help.
my user control has a method which binds the grid to the data source, it's as follows
public void SetSupplierData(SupplierType Supplier)
{
if (Supplier != null)
{
ViewState["SupplierID"] = Supplier.SupplierId;
grdBankAccounts.DataSource = Supplier.BankAccounts;
grdBankAccounts.DataBind();
Session["BankAccounts"] = Supplier.BankAccounts;
}
}
the SetSupplierData method is called from the page where i have my user control.
In order to get this "in-place editing" in grids to work, I typically have to data-bind twice:
once in the OnInit or OnLoad method so that the button click event handlers have the data available to work on
in the OnPreRender method again to show the new values / new state (editing or not)
Marc
I have a page with textboxes and buttons. When the page loads, often times, focus is in one of the textboxes and I don't want this to happen. However, I don't want to use something like setfocus in the page load event because then when buttons are clicked the page will jump. How do prevent the textboxes or any controls for that matter of getting focus on page load?
strange. by default, the page will not focus on any form input unless you set focus on it.
I think you have to cancel all focus by Setfocus on page.
protected void Page_Load(object sender, EventArgs e)
{
// make sure its not a postback
if (!IsPostBack)
{
// your code
InitForm();
InitBlaBlaBla();
SetFocusOnThis();
SetFocusOnThat();
// cancel all focus
Focus();
}
else
{
// this is a postback,
// set focus on control which make post back
Control control = null;
if (!string.IsNullOrEmpty(Page.Request.Params[postEventSourceID]))
{
control = Page.FindControl(Page.Request.Params[postEventSourceID]);
}
else
{
// if postEventSourceID is null, its a button
// find it by iterating all controls
foreach (string ctl in Page.Request.Form)
{
Control c = Page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
// finally
control.Focus();
}
}
basically what I want to do is when
the page loads as the result of
redirecting from another page, don't
set the focus in any textboxes, just
load the page normally, but if the
page loads as a result of clicking a
control on the page, keep the focus
on/near that control.
To accomplish this you have to test for post back. If you detect the page has been posted back, you can set focus to the control, as you intend to. Otherwise, the page is not beeing posted back (GET), so you can avoid the focus on the control.
As far as I know, by default, ASP.NET Webforms don't set the focus on any of textboxes. Are you sure you don't have some code-behind method setting the focus on the textbox?
It turns out that freetextbox controls exhibit this behavior on IE7 and IE6. It is a bug they are investigating.