ModalPopupExtender Hault Execution Until Ok is Clicked? - c#

Perhaps I misunderstood the control, or very possibly am not implementing it correctly, but I've used a ModalPopupExtender much like I'd like to use a MessageBox in desktop development. The problem I'm running into is that once I call the Show() method of the ModalPopupExtender it continues to execute the server side code despite the fact that the user has not yet clicked the button set as the OkControlID. Is this the normal behavior, and or is there a way to hault code execution until the OkControlID has been clicked. To specify, I don't want to create another event in the button click handler as this popup is inline. Here is my code - any advice is appreciated.
My Control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ModalMessage.ascx.cs" Inherits="LB.ModalMessage" %>
<asp:Button ID="btnPopup" runat="server" style="display: none;"/>
<ajaxToolkit:ModalPopupExtender ID="ModalMessageExtender" runat="server"
OkControlID="btnOkay" PopupControlID="Panel1"
TargetControlID="btnPopup" BackgroundCssClass="modalPopupBG">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel CssClass="whitebubble" ID="Panel1" style="display: none; max-width:400px;" runat="server">
<div style="padding:5px 5px 35px 5px;">
<asp:Label ID="lblMessage" Font-Size="Medium" runat="server" ForeColor="Black"/>
<br />
<asp:Button runat="server" Text="Ok" Width="75px" Height="30px" ID="btnOkay" CssClass="modalButton gray modalButton"/>
</div>
</asp:Panel>
The control code behind:
public void ShowMessage(string message)
{
this.lblMessage.Text = message;
ModalMessageExtender.Show();
}
My content page:
<%# Register Src="~/ModalMessage.ascx" TagName="ModalMessage" TagPrefix="mm" %>
<mm:ModalMessage runat="server" ID="mpeMessage"/>
My content code behind:
mpeMessage.ShowMessage("Please enter a username before attempting to reset your password.");
UPDATE:
Sorry for the lack of clarity - let me make my question more clear. If I do the following:
mpeMessage.ShowMessage("Please enter a username before attempting to reset your password.");
Response.Redirect("Register.aspx");
The redirect occurs and the ModalPopupExtender never gets shown. I'm somewhat new to web development so please forgive me if I'm using incorrect terminology. But essentially, I want the execution of code in the code behinds to wait for the user to click "Ok". I'm trying to replace something like this:
ScriptManager.RegisterStartupScript(this, typeof(string), "Message", "alert('Your new account has been created!'); window.location='" + continueUrl + "';", true);
With something a little nicer looking, and since I'm already doing a postback anyway, I thought calling the ModalPopupExtender programmatically would be fine. Hopefully this clears up my question. Thank you all for your responses so far.

You can use validation controls , to check whether the fields are properly filled or not
for more details check the following links:
http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp
http://msdn.microsoft.com/en-us/library/aa479013.aspx

If i am understanding your query correctly then you want that until user does not click on "ok" button on modelpopup, user should not be able to click on page.
if you want this then add PopupDragHandleControlID="Panel1" in your modelpopup control.
<ajaxToolkit:ModalPopupExtender ID="ModalMessageExtender" runat="server"
OkControlID="btnOkay" PopupControlID="Panel1" PopupDragHandleControlID="Panel1"
TargetControlID="btnPopup" BackgroundCssClass="modalPopupBG">
</ajaxToolkit:ModalPopupExtender>
UPDATE
just replace Response.Redirect("Register.aspx"); to btnOkay click event.
on aspx page -
<asp:Button runat="server" Text="Ok" Width="75px" Height="30px" ID="btnOkay" OnClick="btnOkay_Click" CssClass="modalButton gray modalButton"/>
on aspx.cs page -
protected void btnOkay_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
then until user will not click on "Ok" button. he/she can't redirect to Register Page.

Related

ModalPopupExtender not showing anything

Here is the relevant part of the aspx file:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div id="containerForTopButtonsCenter">
<telerik:RadButton ID="bt_addNewSnpAtTop" runat="server" Text="Add New SNP" AutoPostBack="false" UseSubmitBehavior="false" OnClientClicked="clickedToCreateNewSNP" OnClick="bt_addNewSnpAtTop_Click" />
</div>
<atk:ModalPopupExtender ID="NewSnpsModalExtender" runat="server"
TargetControlID="bt_addNewSnpAtTop"
PopupControlID="NewSnpsPopupWindow"
BackgroundCssClass="NewSnpsBackground"
OkControlID="NewSnpsOk"
CancelControlID="NewSnpsCancel"
DropShadow="true">
</atk:ModalPopupExtender>
<asp:Panel ID="NewSnpsPopupWindow" runat="server">
Hello! This is the Modal.
</asp:Panel>
When I click on the bt_addNewSnpAtTop button, no modal pops up. Also, the panel control NewSnpsPopupWindow shows up on the parent page (I was hoping that it would house the content displayed in the modal)...
All help is appreciated, thanks in advance.
I'm guessing the reason is related to your button also having onclick and onclientclick events.
In your OnClick event try:
bt_addNewSnpAtTop_Click(object sender, eventargs e)
{
//Other Code
NewSnpsModalExtender.Show();
}

How to implement a click event on textbox in ASP.NET?

In my web application I need a functionality so that when users click on textbox to input values, it should make the button and the other fields visible?
I am using the code provided below but, could not get it working.
C#:
protected void TextBox1_Click(object sender, EventArgs e)
{
ButtonSearch.Visible = true;
}
ASP.NET:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" Visible="False" />
How to accomplish this?
Set AutoPostback="True". This way the event will be fired server-side, and the button will become visible.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click" AutoPostBack="true"></asp:TextBox>
However, if you only want to toogle visility of a button, you really should considerate javascript. This will save a trip back to the server.
<asp:TextBox onclick="txtBox1_ClientClicked()" ID="TextBox1" runat="server" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" style="display:none;" />
<script type="text/javascript">
function txtBox1_ClientClicked(){
var theButton = document.getElementById('<%=ButtonSearch.ClientID%>');
theButton.style.display = 'block';
}
</script>
You do not need to post back to the server to accomplish your job. You can use client side onFocus event and javascript/jquery, for example.
I know I used an input of type text, and you are using an ASP Control which posts on server, but here is a JSFiddle to get you on the right track: http://jsfiddle.net/Mmjtz/1/
$("<%= ButtonSearch.ClientID %>").click(function(){
$("#TextBox1").show():
});
In this code you need to pass fields ID which you want to visible on the click of button.
Put the textbox inside a div and use the div's onClick event from codebehind. It's not what you asked but it works for me without any errors. Here is a javascript function to implement requested event:
function toggleVisibility()
{
document.getElementById('TextBox1').disabled = true;
/*
...some other code...
*/
}
And of course, you have to define your onclick event at the div definition after implementing this JS function.
<div id="TBdiv" onClick="toggleVisibility()">
<asp:TextBox ID="TextBox1"..../>
</div>
IMPORTANT: Since you now disabled your TextBox from codebehind, you have to enable it in somewhere before you want to use it again. Otherwise you will not see it while the page is running.
jQuery is the perfect solution for your problem. The code would be something like this:
$("#TextBox1").on("click",function(){$("#ButtonSearch").css("visibility", "visible");})
You include the script by adding <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the page and then you can add the piece of code above to within <script></script> tags.

My button opens my modal popup window, but the c# codebehind connected to the button does not fire

I have a button that opens a modal popup, but before I open the modal popup, I want my "OnClick" event for the "btnSaveAndScheduleTask" button to fire. I am using ASP.NET 4.5 / Visual Studio 2012 / HTML5 / CSS3
My aspx (snipet):
How do I get the codebehind OnClick event for my "btnSaveAndScheduleTask" button to fire? If the entire code would help figure it out, let me know, but I'm probably missing something simple (bear in mind that I want to be able to view all my asp controls from the C# codebehind):
<asp:Button ID="btnSaveAndScheduleTask" runat="server" CausesValidation="true"
OnClientClick="javascript:return validatePage();" OnClick="btnSaveAndScheduleTask_Click"
Font-Bold="true" Text="Schedule Task" />
<ajaxToolkit:ModalPopupExtender ID="mpeScheduleTask" runat="server" ValidateRequestMode="Enabled"
BackgroundCssClass="modalBackground" CancelControlID="btnCancSchedule"
PopupControlID="pnlScheduleTask" TargetControlID="btnSaveAndScheduleTask" DropShadow="true" >
</ajaxToolkit:ModalPopupExtender>
<div id="divScheduleTask" runat="server">
<asp:Panel ID="pnlScheduleTask" Height="310" Width="690" BackColor="#ece4e1" ForeColor="Black" runat="server" >
<asp:UpdatePanel runat="server" ID="udpScheduleTask" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTaskSch" Visible="false" Font-Bold="true" Text="Task Scheduling: " runat="server" />
<asp:Button ID="btnSaveSchedule" runat="server" OnClick="btnSaveSchedule_Click" Text="Save Schedule" />
</div><asp:Button ID="btnCancSchedule" runat="server" Text="Canc" />
</ContentTemplate></asp:UpdatePanel></asp:Panel></div>
I've left out most of the panel as it's huge... here is my validatePage() Javascript:
<script type="text/javascript">
function validatePage() {
//Executes all the validation controls associated with group1 validaiton Group1.
var flag = window.Page_ClientValidate('vTask');
if (flag)
//Executes all the validation controls which are not associated with any validation group.
flag = window.Page_ClientValidate();
if (!Page_IsValid) {
$find('mpeScheduleTask').hide();
}
return flag;
}
</script>
My aspx.cs code behind:
protected void btnSaveAndScheduleTask_Click(object sender, EventArgs e)
{
//do stuff
}
Remove TargetControlID="btnSaveAndScheduleTask" , give other dummy control's ID
So when button clicks, click will take user to server , in code behind you need to manually open popup.
This is given here...!!!!
http://www.codeproject.com/Tips/215040/ModalPopupExtender-from-Server-Side-Code
I would put the javascript to be called in the behind code in your button click..
protected void btnSaveAndScheduleTask_Click(object sender, EventArgs e)
{
// do stuff.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "validatePage();", true);
}
this is how I do most of mine. Hope this helps.
And your ASP should look like..
<asp:Button ID="btnSaveAndScheduleTask" runat="server" CausesValidation="true" OnClick="btnSaveAndScheduleTask_Click" Font-Bold="true" Text="Schedule Task" />

ASP.NET: Manually updating an UpdatePanel using jQuery

I'm having trouble with updating an ASP:UpdatePanel using javascript (jQuery). Here're what I have.
I'm using the hidden button trick as I seem not the be able to get the ClientID of the update panel for a __doPostBack trick).
<asp:UpdatePanel runat="server" ID="pnlUpdate">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpdate" />
</Triggers>
<ContentTemplate>
<asp:UpdateProgress runat="server" AssociatedUpdatePanelID="pnlUpdate" DynamicLayout="false" DisplayAfter="100">
<ProgressTemplate>
<img alt="Laddar..." src="img/loader.gif" width="16" height="11"/>
</ProgressTemplate>
</asp:UpdateProgress>
<div style="display:none;">
<asp:Button runat="server" ID="btnUpdate" CommandName="Refresh" CommandArgument='<%# Eval("Id") %>'/>
</div>
<asp:Repeater runat="server" Id="rptrEnquiry">
...
</asp:Repeater>
<%= DateTime.Now.ToString() %>
Fire!
</ContentTemplate>
</asp:UpdatePanel>
In the codebehind that handles the btnUpdate (in a GridView RowCommand) the rptrEnquiry is rebound when btnUpdate is pressed.
If I press the button directly (while not hidden) everything works perfectly (updateprogess is shown and date updated and repeater updated.
But if I click the fire link and trigger the button through javascript only the date is updated but the updateprogress isn't shown and the repeater isn't rebound. While debugging I can see that the rebound code is executed but it's effect isn't in the update.
Ok, so I mangaged to solve my problems by totally rebuilding the whole thing. A few lessons learned that might help someone else:
I'm having the updatepanel in a gridview, when I sepparated the updatepanel part into a control of it's own most of my problems was solved, such as not beeing able to reference pnlUpdate.
http://encosia.com/2007/10/24/are-you-making-these-3-common-aspnet-ajax-mistakes/ was very helpful.
Updates in the update panel is controlled in it's PreRender. By using __EVENTTARGET only the panel we're interested in, is updated.
protected void pnlUpdate_PreRender(object sender, EventArgs args)
{
if (Request["__EVENTTARGET"] == pnlUpdate.ClientID)
{
PreBind();
switch(Request["__EVENTARGUMENT"])
{
case "toggle":
Toggle();
break;
case "purchase":
Purchase();
break;
case "update":
/* nop */
break;
}
Bind();
}
}
To get the __EVENTTARGET to have the proper clientId (it's empty string if using a button) I needed to fire of the panel update using javascript:
<a href="javascript:__doPostBack('<%= pnlUpdate.ClientID %>','toggle');">
<img runat="server" ID="imgToggle" src="~/img/grid_plus.gif" title="Expandera" alt="" width="14" height="14"/>
</a>
Have you tried something like this? (Taken from Easily refresh an UpdatePanel, using JavaScript).
there’s an easy method for triggering
a postback targeted at the
UpdatePanel: __doPostBack().
As long as the event target of a
__doPostBack() call is an async trigger of an UpdatePanel, the ASP.NET
AJAX framework will intercept the
postback and fire a partial postback
instead.
<a href="#" onclick="__doPostBack('<%= pnlUpdate.ClientID %>', '');"/>

How can i access a control within a ListView once a button has been clicked?

I need to access a label control in a listview when I've clicked a button (that is on the same row)...
Does anyone know how to do this please? :(
See below for more of an insight...
ASPX Page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
Code Behind:
protected void voteOnThis(object sender, EventArgs e)
{
Button myButton = (Button)sender;
Voting.vote(int.Parse(myButton.CommandArgument));
// Here i would like to access the 'label' lblDone and make this Visible
}
In this simple case, I should consider using Javascript (JQuery)
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" style="visibility:hidden">Your vote has been counted</asp:Label>
<asp:Button OnClientClick="showLblDone()" ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
now, define inside a script tag the showLblDone function:
<script>
function showLblDone (){
$(this).siblings('span').show();}
</script>
You can also call this function with a parameter if you want to show/hide on every click, or you can use .toggle() instead of .show().
In this case you must add a div (or a Panel) inside the ItemTemplate.
You need to hook into the listview row bind and add the information you want to have when clicked. Using this, you can add an attribute to the button that you read back on click, for example...
If you posted some actual code, I could probably help some more.

Categories