Update an UpdatePanel when ModalDialog closes or on button event - c#

I have two pages. The first one has one button and an UpdatePanel that contains an image.
The button uses the following code to show a ModalDialog:
window.showModalDialog('AjustarImagem.aspx',
null,
'status:no;
LOCATION: NO;
TOOLBAR=NO ;
DIRECTORIES: NO;
dialogWidth:250px;
dialogHeight:300px;
dialogHide:true;
help:no;
scroll:yes');
return false;");
What I need to do is to update the UpdatePanel when the ModalDialog closes, or when the click event of the ModalDialog button fires.

Use the __doPostBack() at the onclick javascript event of your button:
<script type="text/javascript" >
function ReloadPanel() {
//debugger;
// Realiza un postback parcial al panel de ajax.
__doPostBack('<%=UpdatePanel1.ClientID %>', parametro);
}
</script>
And add the load event of the update panel for process the request if you want:
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
// Get the parameter
string arg = Request.Form["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(arg)) return;
}
View this post: How to use __doPostBack()
Hope this helps.

Related

asp.net C# update textbox value without refresh

I have a website where there are two pages, page1 contain textbox and button1,
if I cliked button1 it will open page2 which contain button2,
if I cliked button2 it will assign value from page2 to the textbox in page1
but the problem is the value will display in the textbox after I refresh page1,
and my question is how I can update textbox value directly without refresh in page1 after I click button2 in page2 like this:
here is my code:
page1 aspx.cs:
<script type="text/javascript">
function openPopup() {
window.open("page2.aspx", "_blank", "WIDTH=1080,HEIGHT=790,scrollbars=no, menubar=no,resizable=yes,directories=no,location=no");
}
<asp:button text="clik" id="button1" runat="server" onclientclick="return openPopup()" xmlns:asp="#unknown" style="margin-right:30%" />
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userID"] != null)
{
txtbox.Text = HttpContext.Current.Session["userID"].ToString();
}
}
page2:
protected void button2(object sender, EventArgs e)
{
Session["userID"] = row.Cells[0].Text;
}
we can do this in following way:
create a javascript function on page 2, that refresh textbox1 on
page 1.
on button2 click from page 2, using scriptmanager, call
the function created in step 1 with require param.
So, basically you code be like:
// place this in page2.aspx
// javascript function to update text box in page 1;
function UpdateParentText(value){
if(typeof(value) != undefined){
window.opener.document.getElementById("TextBox1").value=value;
}
}
//2. code behind call to the javascript function from page2.cs
protected void button2(object sender, EventArgs e)
{
// code behind call to the javascript function.
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyScript", "UpdateParentText('" + row.Cells[0].Text + "'", true);
}
If you are not using AJAX, then use this:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "UpdateParentText('" + row.Cells[0].Text + "'", true);
You may use:
window.postMessage() — to send the message
window.addEventListener(“message”,callback) — to receive and process the message
See sample here.
thanks everyone I solve it by using session with javascript

asp.net server side buttons clicks programatically

i have 2 server side asp.net buttons , i need to automate the buttons clicks.
i.e. After page_load, i need to click button1 and after its results are shown on the page, wait for 10 seconds and click button2 .
i tried the following sample code
protected void Page_Load(object sender, EventArgs e)
{
Button1_Click(Button1, null);
Thread.Sleep(10000);
Button2_Click(Button2, null);
}
protected void Button1_Click(object sender, EventArgs e)
{
changeLabel.Text = "Button1";
}
protected void Button2_Click(object sender, EventArgs e)
{
changeLabel.Text = "Button2";
}
}
i had 2 obeservations (maybe useful):
always Button2_Click(Button2, null); event is the latest when the page is fully loaded (which is obvious).
Page_Load(object sender, EventArgs e) doesnot
hit atall when programatically clicked the button.
Any idea how to achieve the solution.
What you are doing in your example is to call the handlers of button1 and button2's click events, that is not the same as having the user click the buttons, and for the form to postback to the server.
If you want the buttons to click them selves and having the post back to the server you need to add javascript that clicks the buttons for you.
If you want a javascript which hits a button for you after X seconds, i would do something like this:
in your aspx page:
<asp:button ID="Button1" runat="server" ClientIDMode="Static" OnClick="Button1_Click">
</asp:button>
in your javascript files or some block on your page:
<script type="text/javascript">
setTimeout(function(){
document.getElementById("Button1").click();
}, 10*1000); // 10 seconds
</script>
Important to note here is that you have to either set ClientIDMode="Static" on your button, otherwise it might have a very obscure name if you are using master pages, or you can do:
<script type="text/javascript">
setTimeout(function(){
document.getElementById("<%= Button1.ClientID %>").click();
}, 10*1000); // 10 seconds
</script>
if you have the javascript in your .aspx file rather then its own .js file.
ps: if you do Thread.Sleep(X) in an aspx page, you will only make the users browser wait for the X milliseconds more for the page to load, code run before the sleep will not be submitet to the clients browser in the way i think you want it to do.
Button_Click is server event, when invoked from client, browser post the relavent data to server and request of new page content, at that time Page_load is invoked.
if you want to do some thing, encapsulate action to some method and call that method in pre-render. Or other wise, use JavaScript.
protected void Page_PreRender(object sender, EventArgs e)
{
UpdateButton1()
Thread.Sleep(10000); // no need to put sleep
UpdateButton2();
}
protected void Button1_Click(object sender, EventArgs e)
{
UpdateButton1();
}
protected void UpdateButton1()
{
changeLabel.Text = "Button1";
}

Registering Javascript function that manipulates radwindow from ajaxified radgrid

I'm trying to open a radwindow client side with some registered script.
Question 1: Is there a reason radwindow wouldn't be found if executed on startup, if so why and how do I fix it?
Question 2: Whenever I ajaxify the radgrid the event no longer fires. This makes sense, as there is not a postback occurring, so the page never starts up. How do I get registered scripts to execute in an ajax enviornment?
---- Relevant Code Behind ----
protected void RadgridProjects_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Member")
{
Session["ProjectId"] = (e.Item as GridDataItem).GetDataKeyValue("ProjectId").ToString();
radGridProjectMembers.Rebind(); //Not the same grid!
ClientScriptManager cs = Page.ClientScript;
string js = "<script type='text/javascript'>ShowWindow()</script>";
cs.RegisterStartupScript(this.GetType(), "showwindow", js);
}
}
---- Javascript Function ----
function ShowWindow()
{
alert("code fired");
var radWin = $find("<%= RadWindow1.ClientID %>");
radWin.show();
radWin.moveTo(650, 450);
radWin.set_width(500);
radWin.set_height(400);
}
The window never opens, but my testing alert does fire. It should be noted that when I use a client event for the script the window DOES open.

Disabling a button doesn't fire click event on server side

I have an imagebutton in my aspx page like:
<asp:ImageButton ID="btnProcessPayment" ImageUrl="~/Images/process-payment.png" OnClientClick="return disableButton(this);"
runat="server" OnClick="btnProcessPayment_Click" />
This is my javascript function:
function disableButton(button) {
button.disabled = true;
return true;
}
As you can see in my javascript event handler I have disabled the button to prevent the user from click the button twice. However, even my server side event handler doesn't get fired due to this. What am I doing wrong?
Note: If I comment out this line button.disabled = true; all works out pretty well.
Disabling the button disables the form submit as well. You need to do the submit yourself:
function disableButton(button) {
button.disabled = true;
__doPostBack(<%= btnProcessPayment.ClientID %>,''); // need to manually submit
return true;
}
Updated as per Waqas suggestion!
You need to fire out serverside event of the button some thing like this
onclick="this.disabled=true;__doPostBack('buttonclientid','');"
or
function disableButton(button) {
button.disabled = true;
__doPostBack('buttonclientid','');
return true;
}
Use this and it worked for me
__doPostBack('<%= btnSubmit.ClientID %>', '');
where btnSubmit is the ID of my button and I wanted to trigger the server side click event for the button. It works.

How can i catch onclick event of Dynamically loaded control?

I have a dropdownlist (on Page) which has OnSelectedIndexChange event thats Loads different Control (ascx) dynamically each time ( with LoadControl Command) - into the page.
Each Control Has a Button(runat=server) and TextBox(runat=server).
When i click on the button - i cant get into the Onclick function .
How can i get into the OnClick Function of the Ascx ?
I know that each SelectedIndexChange its makes postback - so i know i have to save something in the viewstate. but i dont know how to save it and later get the values eneterd on the TexstBox. ( of Each ascx)
You need to add an event handler to the user control, like this:
public event EventHandler ButtonClick;
And in the click event of the button:
protected void Button1_Click(object sender, EventArgs e)
{
if (this.ButtonClick != null)
this.ButtonClick(this, e);
}
Then, from the page, you can get the click event like this:
<UC:MyUserControl ID="UserControl1" runat="server" OnButtonClick="UserControl1_ButtonClick" ... />
protected void UserControl1_ButtonClick(object sender, EventArgs e)
{
//Handle the click event here
}
If you're loading the controls dynamically, then you'll need to make sure the controls are rehydrated after postback, and emulate the code above by assinging the event handler through code:
MyUserControl ctrl = (MyUserControl)this.LoadControl("...");
ctrl.ButtonClick += new EventHandler(UserControl1_ButtonClick);

Categories

Resources