I have a multiview with two views. view at index 0 is "Readonly" and view at index 1 is edit view. by default is is set to show reaonly view with this :
<asp:MultiView ID="mv" runat="server" ActiveViewIndex="0">
I click a button I enter some information in view 1 and click save and it goes to view 0 with this code:
protected void SaveLinkButton_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
bool success = Save();
if (success)
{
LoadViewMode();
mv.ActiveViewIndex = 0;
}
}
}
Now after clicking save button, when I am in reaonly view. If I refresh the button, It calls save function again. I don't know why ?
Please suggest solution to it.
When you refresh the page (not the button), the last request that returned the page being viewed is resubmitted to the server. Therefore, the server executes the same code again.
Related
Is there a way to make a button click event run from the page load?
Yes, you can do that.
if your button has runat="server" you can access it from codebehind.
you could insert a click action on the button in the page_load event. but it's better to create a function with all actions to call both from button click and page_load.
<asp:Button id="btn1">Click here</asp:Button>
void Page_Load(object source, EventArgs e)
{
doThis();
}
void btn1_click()
{
doThis();
}
void doThis()
{
//click actions
}
Another way to do it is by javascript. find the button when document is ready and fire the click on the button.
You could wrap the logic behind the button click event in a function and call that function from the page load.
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 have an asp.net button in my page, after I click the button, it is clicked again and again whenever I refresh the same page.
I tried;
if (!IsPostBack)
{
.....
}
for the click function of the button, but in this case button is not working.
<asp:Button ID="btnCommentSubmit" runat="server" Text="Submit" CssClass="leaveAComment" onclick="btnCommentSubmit_Click" />
above is the button.
protected void btnCommentSubmit_Click(object sender, EventArgs e)
{
string cityID = BusinessClass.Default.findCityIDByCountryAndCityName(Request.QueryString["city"].ToString(), "url").ToString();
BusinessClass.Default.makeComment(txtComment.Text, Session["userID"].ToString(), cityID);
txtComment.Text = null;
}
and this is the function.
If don't get you wrong your problem is when you hit F5 after clicking the button. Trying to resubmit the form is the normal behavior and is not related to asp.net but to browsers in general.
F5 causes to re-execute last request to the server, in your case, a POST request.
A way to avoid this issue is implementing the web pattern Post/Redirect/Get
Easy fix with a bit of JavaScript.
document.onkeydown = function() {
if(event.keyCode==116) {
event.keyCode=0;
event.returnValue = false;
}
}
This would stop the duplicate resend when you hit F5.
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