The following is the code I am using to display popup window of Telerik(Radwindow) in aspx page. It successfully displays the window with the below current code.
How can I display popup window from ASP.NET Usercontrol?
RadWindowManager windowManager = new RadWindowManager();
RadWindow window1 = new RadWindow();
window1.NavigateUrl = "Window1.aspx";
window1.ID = "RadWindow1";
window1.VisibleOnPageLoad = true; // Set this property to True for showing window from code
windowManager.Windows.Add(window1);
this.form1.Controls.Add(window1);
You can, of course, but there are two issues with this approach:
the user control will have to know about the window manager and window on the master page, traverse the controls hierarchy and find them
if you add the entire snippet to the user control you will end up with several window manager instance and this can play a few tricks on you (see here).
So, think about the following ideas:
add a RadWindow instance to the user control (not a RadWindowManager) and use that alone. Read this article to register a script from the server in order to open it and this article on making the JS functions you may need unique per user control.
open the RadWindow purely from the client-side as shown here. You can register a JS function from the server that will pass the parameters you need (URL, modality, whatever)
Here is a sample implementation of one of the ideas (that I would go with) based on your comment:
Master page
<telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>
<script>
function openDialog(url, modal, width, height) {
if (radopen) { //if not, there is no RadWindowManager on the page, add an else{} block to use window.open() or other logic
var wnd = radopen(url, null);
wnd.set_destroyOnClose(true);
//add checks here in case parameters have not been passed
wnd.setSize(width, height);
wnd.center();
wnd.set_modal(modal);
}
}
</script>
User control markup
<asp:Button ID="Button1" Text="open RW" OnClick="Button1_Click" runat="server" />
Use control code-behind
protected void Button1_Click(object sender, EventArgs e)
{
bool flag = true;
if(flag)
{
string script = string.Format("function f(){{openDialog('{0}', {1}, {2}, {3});Sys.Application.remove_load(f);}}Sys.Application.add_load(f);",
"the-page.aspx",
"true",
600,
400);
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someKey", script, true);
}
}
Related
I have a page that have the following code for a custom control:
<SiteControls:Announcements runat="server" id="UserAnnouncements" />
Let's also say I have a GridView control, just so I can cover multiple scenarios. I need to check if the user has permission to view this control by checking the Boolean:
PermissionsManagement.DoesUserHavePermission(userId, permissionId)
Which is defined as:
public static class PermissionsManagement
{
public static bool DoesUserHavePermission(int userAccountId, int permissionId)
{
// Code Goes Here
}
}
If the user doesn't have permission, DoesUserHavePermission will return false. I have the ASP.NET WebForms page laid out as if the user has full control (meaning I have all the controls on the page and want to remove them if they don't have permission vs adding every single control to the page).
I can set the control's visibility to false on Page_Load function if the user doesn't have permission, but that doesn't stop my control from loading or in the case of a GridView from loading its data. How do I stop a control (User control or standard control) from loading any data if the user doesn't have permission to use (view) the control? I have tried the following inline code which doesn't work:
<% if(PermissionsManagement.DoesUserHavePermission(1, 1))
{ %>
<SiteControls:Announcements runat="server" id="UserAnnouncements" />
<% } %>
But that doesn't work as the control Page_Load still fires for the control and I assume any other control will load data if it is data-bound or acts similar to my control.
Without knowing much of your code, it is a little difficult to figure out the exact answer. However, as much as I understood your question, here's my answer.
Loading data for Announcements or GridView should still be in your control. I would expose a method in Announcements control that actually loads data for it. For the GridView you should simply defer the binding of DataSource until the permission check is performed. Of course these things need to be done in addition to hiding (setting visibility) of these controls.
See the code below, not complete, but enough to express an idea:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Check permissions here
if (allowed)
{
// For custom/user control
UserAnnouncements.GetAnnouncements();
// For grid view
GridView1.DataSource = GetGridviewData(); // GetGridviewData would return DataSet or anything valid.
GridView1.DataBind();
}
else
{
// Hide the controls
}
}
}
We have an asp.net (2.5) application where we use one master page and a few hundred forms that utilize that master. We're creating a new set of forms and we want to submit the form on a button so that we can use all of the form values on the next page.
However, the form tag is in our master page and asp.net seems to only allow 1 form tag per page. Is there a way for me to change the target of the form post of the master from the child page or is there a way to add a second post?
You can change the target. In Masterpage add an id if it's not there already:
<form runat="server" id="form1" >
Now in the content page access the form and change it's target:
protected void Page_Load(object sender, EventArgs e)
{
HtmlForm myForm = Page.Master.FindControl("form1") as HtmlForm;
if (myForm != null)
{
myForm.Target = "_blank"; // _parent, _self or _top
}
}
I don't know how much it will help.
You might look here, I found it very interesting: Form Elements in ASP.NET Master Pages and Content Pages.
Do you need to access this second form from the code-behind on your new forms? If not there is a little hack you can do just add an end form tag and start a new tag that has the new target details
</form>
<form method="POST" action="blah.svc" >
//non-asp.net form
However to change the actual postback url you would change the form action via the PostBackUrl property on your buttons.
There is also Server.Transfer, kinda depends on what you are doing on your new forms.
I have some javascript code on my main page that opens a popup when a link is clicked on:
<script language="javascript">
function OpenResidentialAddressWin(subscriberContactRelationGid, routeId, btn) {
window.showModalDialog("SubscriberResidentialAddress.aspx" + BuildQueryStringValuesForSubscriber(subscriberContactRelationGid, routeId, returntxtReceiptDate().value), this, strWindowFeatures + ";scroll:no;dialogWidth:442px;dialogHeight:350px");
location.href = location.href;
}
</script>
So the above code opens up the page SubscriberResidentialAddress.aspx in a modal window (as well as passing a number of variables into this page)
Now this page is arranged in such a way that it either displays or edits information. (These are in separate divs that are displayed or hidden dependign on what buttons have just been pressed in the window).
Currently after a user makes a save to the information on the page it switches to display mode. Instead I want to close the modal window completely and reload the main page.
A colleague suggsested that I use a literal control on the aspx page of the modal window and write javascript to it after a successful save has been carried out. So I placed the following in the head section of the SubscriberResidentialAddress.aspx page:
<asp:Literal runat="server" ID="litCloseWind"></asp:Literal>
And then in the code behind in the function that is called when a save is carried out
protected void btnAddSave_Click(object sender, EventArgs e)
{
////// code related to saving
if (status.IsSuccessful)
{
litCloseWind.Text = "<script>this.close()</script>";
Response.Redirect(Request.Url.AbsoluteUri, false);
}
}
I have tried the above and also litCloseWind.Text = "window.close()"; but neither were successful in closing the modal window after a save is carried out.
Is this plan logically sound and have I just made an error somewhere or was this a bad way of doing it to begin with?
Why don't you register a script here, like this:
ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
No need of putting any Literal Control here.
So your code will look something like this:
protected void btnAddSave_Click(object sender, EventArgs e)
{
if (status.IsSuccessful)
{
ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
}
}
I am developing a web application where I am using a GridView control to load data from the database.
I have a row that is loaded in editable mode. I have a save button which the user clicks after making the changes, and I want to check the IsDirty flag to stop the user from saving and notify them via an alert box. I'm using a Web User Control (ASCX) to load the GridView. How can I check dirty rows in the grid and stop the user from saving when user clicks the save button or logout button??
P.S. I am using a LoginView for the logout button.
You need to handle the OnRowUpdating event in your grid, for example:
<asp:GridView id="a" runat="server" OnRowUpdating="a_rowUpdating" ... />
In code behind:
protected void a_rowUpdating (object sender, GridViewUpdateEventArgs e)
{
//Add logic appropriately to know whether you should allow the update or not.
//If you shouldn't, just set e.Cancel=true as below:
e.Cancel=true;//will stop from updating.
}
You have access to the Old and New values in the GridViewUpdateEventArgs object. For more details and sample code, check here.
I use a simple JS method to check for a variety of changes in ASP.net pages. I added it to a JS filer called CheckDirty.js and added a link to it on the page, in the file is this code:
var checkDirty = true;
var form_clean;
//this should be called when the save button is clicked, but prior to the page post
function onSave() {
checkDirty = false;
}
// serialize clean form
$(function () {
form_clean = $("form").serialize();
});
window.onbeforeunload = function (e) {
if (checkDirty) {
var form_dirty = $("form").serialize();
if (form_clean != form_dirty) {
return 'Your changes will be lost';
}
}
};
This will also work for changes to a gridview and will alert the user. I find this method best because it works with everything and I don't need to keep writing different methods for different controls.
i have a page where on click of a button, a javascript function runs. It then aggregates some data and places the data on a hidden field in this page. It then opens a new window. This new window picks up this aggregated data like so :-
$('#accepted').val(window.opener.$('#accepted').val());
where accepted is the hidden field in both parent and child window (no runat="server" was used). The issue now is that i require this data to databind two grids. Currently I've done a doPostback on both grids, but what i really want to do is doPostback for the form once and handle the databinding the PageLoad event. So two questions :-
1) How do i doPostback the form?
2) How do i do this while still being able to differentiate from the actual form submission?
To post the form you should just be able to add a call to __doPostback in your javascript, after the accepted field is set. You can use the EventTarget and EventArgument parameters of the __doPostback to control the binding in your grid.
So, you could put this in your js:
__doPostback('rebindGrid', '');
and then this in your page load event:
if (Request.Form["__EVENTTARGET"] == "rebindGrid")
{
//....Do so stuff
}
In order to tie it in more directly with the postback model I wrap mine with some C#
C# Extension Method
public static string GetPostBackLink (this Control c, string argument = "") {
return c.Page.ClientScript.GetPostBackEventReference(ctl, argument, true) + ";";
}
ASPX
<asp:LinkButton id="lnkDoThis" runat="server" onclick="lnkDoThis_Click"
style="display: none;"></asp:LinkButton>
<asp:HiddenField id="hdnParamHolder" runat="server" />
JS
function DoSomething(param) {
$("[id$='hdnDealTemp']").val(param);
<%= lnkDoThis.GetPostBackLink() %>
}
CodeBehind
protected void lnkDoThis_Click (object sender, EventArgs e) {
var myParam = hdnParamHolder.Value;
// Do server actions here
}
As for the opening in a second window ... I am not sure I follow when you want this to happen? If it is after the postback you will need to read from the hdnParamHolder control when the page reloads.