I have a Modal Dialog:
function ShowPopup()
{
window.showModalDialog('dialog.aspx', null, 'status:no;dialogWidth:950px;dialogHeight:150 px');
}
Then its called in the code behind
Page.ClientScript.RegisterStartupScript(this.GetType(), "popUpScript", "ShowPopup();", true);
The dialog.aspx has two buttons:
<asp:Button id="btn1" runat="server" Text="Button 1" OnClick="btn1_Click"></asp:Button>
<asp:Button id="btn2" runat="server" Text="Button 2" OnClick="btn2_Click"></asp:Button>
However, the Click events in the code behind are never getting fired.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, System.EventArgs e)
{
Response.Redirect(url)
}
protected void btn2_Click(object sender, System.EventArgs e)
{
Response.Redirect(url);
}
}
I recall seeing this problem before and I think it is related to caching. Try adding this to your dialog.aspx page_load method when !IsPostBack
Response.AddHeader("Pragma", "no-cache")
To stop the browser caching the page and reusing it
Are you receiving any hidden JavaScript errors preventing the POST?
Related
I have an button in my aspx web page, which should be 'clicked' in my code behind c# code. The OnClientClick method then call a JS confirm dialog box and so the btn_Click function only get executed when user click 'OK'...
This is my code so far:
Variante 1
aspx:
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
// set OnClientClick
btnConfirm.OnClientClick = "confirm('Save in DB?');";
// invoke method
Type t = typeof(System.Web.UI.WebControls.Button);
object[] p = new object[1];
p[0] = EventArgs.Empty;
MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
m.Invoke(btnConfirm, p);
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//save something in database
}
Got code samples by: http://forums.asp.net/t/1046550.aspx?How+do+you+raise+a+button+click+event+from+outside+the+button+
I want to call the OnClientClick method, but when I replace 'OnClick' with 'OnClientClick' this error appear:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Edit:
Variante 2:
I tried to rewrite my program like this:
aspx:
<script language="javascript">
function invokeButtonClick() {
document.getElementById("btnConfirm").click();
}
function Validate() {
return confirm('Save in db?');
}
</script>
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />
aspx:cs:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "InvokeButton", "invokeButtonClick();", true);
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
// save something in database
}
But then the page postback before my btnConfirm_Click function get called...
Thanks,
Michael
If OnClientClick has a return value of true then the codebehind OnClick event will fire. If OnClientClick is returned false then it won't.
If OnClientClick doesn't return anything the codebehind event will fire regardless. So you want..
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
<script language="javascript">
function Validate() {
return confirm('Save in db?');
}
</script>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
// set OnClientClick
btnConfirm.OnClientClick = "return Validate();";
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//save something in database
}
UPDATE:
This post just came to my attention, don't know why I didn't include it in my original post but you can use 'OnClientClick' in the aspx tag:
<asp:Button runat="server" ID="btnConfirm" OnClientClick="return Validate();" Text="Confirm" OnClick="btnConfirm_Click" />
<script language="javascript">
function Validate() {
return confirm('Save in db?');
}
</script>
You need to Add both the events on the button click. OnClientClick handles the Client side script code and OnClick handles the server side event calling.
So add both events and there handlers. The NullReferenceException you are getting because of that.
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />
and in your javascript code can be
function Validate(){
if (valied())
return true;
return false;
}
100% code behind confirmation! Sometiems its so annyoing to Code behind or Server side confirmation in asp.net when you want to Delete or Update something, I got its solution after a long search as below:
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Are you sure to BLOCK this customer ?')" OnClick="btnSave_Click" />
Now whenever you will click this button it 'll ask to confirm, if you 'll select "NO" page will not post and if you select "Yes" then page will post and your button click event will be fire... like below
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
// Before coming here,,,there will be confirmation
protected void btnSave_Click(object sender, EventArgs e)
{
GetCustomerInfo();
}
In my .aspx page I have a 'input' html tag, also an asp button.
<input id="Name" type="text" runat="server" clientidmode="Static" />
<asp:Button Width="100" type="submit" ID="sendOrder" runat="server" OnClick="SubmitForm" Text="Submit" />
On page load, I am filling the value in input tag from code behind, like this:
Name.Value= "X";
But now if I change value of this text box from browser, lets say "Y", and click on Submit button, then I get the old value, but not the new one.
protected void SubmitForm(object sender, EventArgs e)
{
var test= Name.Value; // here I get old value
}
How can I get the altered value?
Make sure you are only setting the value to "X" when its not a postback:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load() event will change the value from "Y" back to "X".
You need to use !IsPostBack on Page_Load shown as below:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input> control in asp.net but best practice is to use <asp:TextBox></asp:TextBox> control instead.
Here is the sample example:
HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}
Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!
You don't need all the else portion, just do this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}
This is the html ,
<asp:LinkButton ID="hlnkLogoffF" runat="server" Text="Logoff" OnClick="hlnkLogoffF_Click" ></asp:LinkButton>
This is the code behind,
protected void hlnkLogoffF_Click(Object sender, EventArgs e)
{
//do something here
}
When i run this, i get the following error,
"The resource cannot be found. "
You have to use
if (!Page.IsPostBack)
{
// Your code here
}
in code behind of master page.
Also
What happens if you turn off validating with CausesValidation setting to false?
i tried to run your code
.aspx
<asp:LinkButton ID="hlnkLogoffF" runat="server" Text="Logoff" OnClick="hlnkLogoffF_Click" ></asp:LinkButton>
.Cs
protected void hlnkLogoffF_Click(object sender, EventArgs e)
{
}
i Haven't get any error.Could you please post the code inside the click event.?
I have save button on a dynamic usercontrol that I load onto aspx page, but I want to move the button onto .aspx page instead. How can I fire the onclick event from aspx to ascx.
Any help would be great.
Cheers.
Code Example:
ascx:
protected void BT_Save_Click(object sender, EventArgs e)
{//Save details currently on ascx page }
aspx:
protected void BT_aspx_Click(object sender, EventArgs e)
{
//when this button is clicked I need it to fire BT_Save_Click on ascx page to save the data
}
In user control
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
In User control .cs page
public event EventHandler ButtonClickDemo;
protected void Button1_Click(object sender, EventArgs e)
{
ButtonClickDemo(sender, e);
}
In Page aspx page
<uc1:WebUserControl runat="server" id="WebUserControl" />
In Page.cs
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);
}
protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
{
Response.Write("It's working");
}
Write a public / internal sub on the ASCX, and then call it from the onClick on the ASPX?
You'll need to create a custom event on your ascx user control and subscribe it within your aspx page.
have a look at this question
define Custom Event for WebControl in asp.net
My code:
*.aspx:
<asp:DropDownList ID="CountryList" CssClass="CountryList" runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged" />
*.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
CountryList.SelectedIndexChanged +=
new EventHandler(CountryList_SelectedIndexChanged);
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}
But this doesn't work.
Try setting AutoPostBack="true" on this dropdown list:
<asp:DropDownList
ID="CountryList"
CssClass="CountryList"
runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged"
AutoPostBack="true"
/>
Also you don't need to manually wire-up the event handler in the Page_Load method. It will be automatically done by ASP.NET when it compiles the webform:
protected void Page_Load(object sender, EventArgs e)
{
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}
I think you missed AutoPostBack="true" Property in aspx file
Add AutoPostBack="true" in ur aspx code and everything will work as you thought.