How to show modal popup on page index changed event? - c#

I have one user control having gridview in it.In my aspx page i have modal popup to show this user control.So,when i click on page index modal popup get disappears.
So,how can i avoid disappearance of modal popup on page index change in asp.net.
Thanks.

C# code:
public bool showModelPopup = false;
void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
showModelPopup = true;
//Your code
}
.apsx: (jQuery)
$(function(){
if ("<%=showModelPopup%>" == "true")
$("#ModelPopUpID").show();
});

Every postback will cause the Popup to disappear. If just certain postsback can handle during your popup you could put a MyModalPopupExtender.Show() into the code that handles that postbacks.

Related

C# dynamically created user control button click not working

I have a page (company.aspx) that when you click a button on the page the button click event (btnShowDeptsUserControl_Click) dynamically creates a user control and adds to a placeholder. The page reloads with the user control displayed. That works fine.
The issue I am having is that when I click the button on the user control itself, the btnEmailDepts_Click event is not fired, but the main page (company.aspx) is reloaded.
I have read that one way to resolve this is to create the dynamic user control in Page_Init, but I am creating the user control from the main page button click. Also, I am not declaring the user control in the code behind of the main page as you would normally, but do have a placehoder within which the user control is added.
I have also read that you can add a delegate to the main page for the user control button click, but the example seemed to have so much necessary code for such a simple thing that I figured there must be a better way.
Here are the relevant code snippets:
company.aspx:
<asp:Button id="btnShowDeptsUserControl" runat="server" OnClick="btnShowDeptsUserControl_Click">Show Depts</asp:Button>
<asp:PlaceHolder ID="phUserControls" runat="server"></asp:PlaceHolder>
company.aspx.cs:
protected void btnShowDeptsUserControl_Click(object sender, EventArgs e)
{
CreateDeptsUserControl();
}
private void CreateDeptsUserControl()
{
phUserControls.Controls.Clear();
var uc = (UserControl)LoadControl("~/controls/ucDepartments.ascx");
phUserControls.Controls.Add(uc);
}
ucDepartments.ascx:
<asp:Button ID="btnEmailDepts" runat="server" Text="Send Email" OnClick="btnEmailDepts_Click" />
ucDepartments.ascx.cs:
protected void btnEmailDepts_Click(object sender, EventArgs e)
{
EmailDepts(); // breakpoint here is never hit
}
private void EmailDepts()
{
// do something
}
If you read the MSDN, you will notice:
However, the added control does not catch up with postback data processing. For an added control to participate in postback data processing, including validation, the control must be added in the Init event rather than in the Load event.
You are adding your control in the click event which happens after both init and load events, so the postback will not work.
You can call your CreateDeptsUserControl function in the ini event, but there you will have to detect if the btnShowDeptsUserControl was clicked by yourself. It's not hard, you will need to check the submitted values collection and see if there is an item for btnShowDeptsUserControl.
Just wanted to post what I did to make this work. Racil Hilan's answer helped me to arrive at this solution. I did away with the dynamic user control, and went with the more common declared user control in the aspx, but set it to Visible="False" by default.
Notice that the main page button event is empty. Also notice that the user control Page_Load event is empty. All the checking is done in the user control OnInit event, which is executed with each main page load.
In the user control OnInit event I have a couple guard conditions that check to see what EVENTTARGET, if any, caused the page / user control to load. If the EVENTTARGET (control ID) is that of the main page button, then execution will continue and will call LoadSomeData() as well as set the user control Visible = true. Otherwise, if either guard condition evaluates as false, we exit and the user control does not get loaded, so no wasted db / service calls.
company.aspx:
<asp:Button id="btnShowDeptsUserControl" runat="server" OnClick="btnShowDeptsUserControl_Click">Show Depts</asp:Button>
<uc1:ucDepartments ID="ucDepartments" runat="server" Visible="False" />
company.aspx.cs:
protected void btnShowDeptsUserControl_Click(object sender, EventArgs e)
{
// empty, just need the event for inspection later in user control OnInit event.
}
ucDepartments.ascx:
<asp:Button ID="btnEmailDepts" runat="server" Text="Send Email" OnClick="btnEmailDepts_Click" />
ucDepartments.ascx.cs:
protected override void OnInit(EventArgs e)
{
if (string.IsNullOrWhiteSpace(Request.Params["__EVENTTARGET"]))
return;
var controlName = Request.Params["__EVENTTARGET"];
if (controlName != "btnShowDeptsUserControl")
return;
LoadSomeData(); // call method to load the user control
this.Visible = true;
}
protected void Page_Load(object sender, EventArgs e)
{
// empty
}
private void LoadSomeData()
{
// get data from database
// load table / gridview / etc
// make service call
}
protected void btnEmailDepts_Click(object sender, EventArgs e)
{
EmailDepts(); // this event is now executed when the button is clicked
}
private void EmailDepts()
{
// do something
}
Variation that includes jquery to scroll to user control after postback:
In the main page button click event you can also do something like set a hidden var to a value that can be inspected on main page doc ready to do some jquery stuff, such as scrolling the user control into view if it is far down the page (which I am actually doing in my current task).
Not only will this scroll the now loaded user control into view after clicking the main page button, but notice the code in setupDepts(). I hide the asp button that does a postback to load the user control, and show a regular html button. They both look the same (both say Show Depts), but the regular html button, when clicked, will fire jquery to toggle the div that contains the user control to close, click again, it will open, click again it will close, etc.
This is so that you only load the user control once (make db / service calls once) when the main page button is clicked, and then can toggle show or hide it with subsequent clicks of the alternate button. This approach can be used with multiple buttons or links so long as they all have the same class ids. For example, you may have a Show Depts button / link at top of page and another at the bottom of the page, which is the case in my current task.
company.aspx:
<asp:Button id="btnShowDeptsUserControl" runat="server" class="btnShowDepts" OnClick="btnShowDeptsUserControl_Click">Show Depts</asp:Button>
<button class="btnToggleDepts" style="display: none;">Show Depts</button>
<div id="divShowDepts" style="display: none;">
<asp:HiddenField runat="server" id="hdnShowDepts"/>
<uc1:ucDepartments ID="ucDepartments" runat="server" Visible="False" />
</div>
<script language="javascript" type="text/javascript">
$(function () {
toggleDeptsUserControl();
if ($('#<%=hdnShowDepts.ClientID%>').val() === "show")
setupDepts();
});
function setupDepts() {
$('.btnShowDeptsUserControl').hide();
$('.btnToggleDeptsUserControl').show();
scrollToDepts();
}
function scrollToDepts() {
$('#divShowDepts').toggle(700, function () {
if ($(this).is(":visible")) {
$('html, body').animate({ scrollTop: ($(this).offset().top) }, 'slow');
}
});
}
function toggleDeptsUserControl() {
$('.btnToggleDeptsUserControl').on('click', function (event) {
event.preventDefault();
scrollToDepts();
});
}
</script>
company.aspx.cs:
protected void btnShowDeptsUserControl_Click(object sender, EventArgs e)
{
hdnShowDepts.Value = "show";
}

Show aspx form as modal on button click

is there a way where I can show an aspx form without using javascript?
Is there like this code in a asp button?
protected void btnLookUpPayment_Click(object sender, ImageClickEventArgs e)
{
webLookUpPayment wblp = new webLookUpPayment();
wblp.ShowModalDialog();
}
Because using a javascript popup modal is actually not my option, I have a gridview in my modal and upon clicking a row in that gridview, i will populate another gridview in another form, I can't explain it thoroughly because it is confusing, just want to know if there is a way where I can show a modal without using any javascript?
Thanks in advance for your help guys!
It will perfectly work. For pop up new .aspx from code behind
Response.Write("<script>window.open('About.aspx', 'hello', 'width=700,height=400,scrollbars=yes');</script>");
And for new window without pop up
ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", String.Format("<script>window.open('{0}');</script>", "About.aspx"));
mark it as answer if it works. Thanks

onClick event is not working for user control controls

I'm loading an user control in one of the class file. This is not code behind page. This is common class for grid view which is applied to all the gridviews in all pages.
I added onclick method to the controls in the user control, but they are not firing in the code behind of that user control.
I kept break points. However, it is not reaching those break points. In addition, the user control is getting closed after I click on any control which has onclick method. After clicking the entire page is reloading in which the grid view is loaded and because of it i guess user control is disappearing. I used update panel in user control still it is not working. Onclicking the the user control is going inside on_pageload of that user control.
This user control is displayed when an image on each column of grid header is clicked. My code is as follows
User control ASCX:
<asp:LinkButton ID="lnkSortDescending" runat="server"
onclick="lnkSortDescending_Click">
Sort Descending
</asp:LinkButton>
Code Behind:
protected void lnkSortDescending_Click(object sender, EventArgs e)
{
//some code
}
File in which user control is loaded:
protected void btnFilterButton_Click(object sender, ImageClickEventArgs e)
{
int i = 0;
Panel p = new Panel();
Control uc = (Control)Page.LoadControl("~/UserControls/FilterPanel.ascx");
uc.ID = "Filter"+(i++);
p.Controls.Add(uc);
}
Please help me to solve this problem.

How to load the grid (in Main page) after some data added in popup page

Consider I have a page with Add button and Grid.
Grid Contains some data.
If I click Add Button ,the new popup window will open.
Popup window contains Text-boxes and Button for adding the new values to Grid.
My ques is ,how to automatically load the grid view after adding some values in popup window?
code for popup window:
protected void btnAdd_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}','','width=550,height=300,left=500');</script>", "PopupADD.aspx"));
}
Follow these steps..
1.Design a window to add data to gridview inside a div.
2.Show the div as popup using Javascript.
3.Bind the gridview when you click some button(like ok) in that popup window.

How to hide modal pop up on browser back button

I have an application where I will show Modal Popup on successful insert, update and delete. But after performing this when I move to next page and coming back to previous page on hitting browser back button the Modal Popup is getting displayed, I don't want to display this pop up on hitting back button. How can I solve this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage==null) {mpeModalPopup.Show(); }
if (Session["Tasks"] == null)
{
Server.Transfer("login.aspx");
}
else
{
string strTasks = Session["Tasks"].ToString();
if (strTasks.Contains("205"))
{
if (!IsPostBack)
{
mpeModalPopUp.Hide();
funPageLoadData();
CheckPopup();
Session["url"] = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
}
}
else
{
ReturnBack();
}
}
}
You dont seem to have handled Page.IsPostBack boolean property on your page_load event.
if (Page.PreviousPage==null) {mpeModalPopup.Show(); }
For opening the popup (after postback) asp.net changes the html or inserts a javascript function to show the modal popup.
The only solution I know of is triggering an ajax postback (with an UpdatePanel) instead of a full postback when you click the button(s). This way the popup is loaded by an ajax call and won't display when you press the backbutton later on.

Categories

Resources