ASP.NET Button to update properties and show jQuery Modal - asp.net

I have the following ASP.NET code:
<div id="panelIssue" runat="server" style="width: 450px; height: 320px;">
<gsl:IssueUC ID="ucIssue" runat="server"
OnItemSaved="ucIssue_ItemSaved"
OnItemCancelled="ucIssue_ItemCancelled" />
</div>
and then have an asp:Button on the page that simply call some methods and set some properties of the custom user control like
protected void btnNewIssue_Click(object sender, EventArgs e) {
ucIssue.ChangePanelMode(PanelModeEnum.Add);
ucIssue.FirmID = Convert.ToInt32(Page.Session["FirmID"]);
ucIssue.loadObject();
}
I know that I can use the div to show a jquery modal dialog but the question is how to set the usercontrol properties and call their methods?
I can evaluate also a different approach (e.g. changing the usercontrol).
Thanks in advance
Lorenzo

What I can suggest is that in the click event, after setting the control properties, you can emit a piece of javascript that will create and display a modal when the page finishes loading upon reaching the client.
Try something along the lines of the following snippet (not tested, may need small tweaking to work, but I hope you get the big idea):
protected void btnNewIssue_Click(object sender, EventArgs e) {
ucIssue.ChangePanelMode(PanelModeEnum.Add);
ucIssue.FirmID = Convert.ToInt32(Page.Session["FirmID"]);
ucIssue.loadObject();
//emit client script that will create and show the modal dialog
ClientScriptManager clientScriptManager = Page.ClientScript;
string scriptText = "$(document).ready(\n" +
"function() {\n" +
"$('#panelIssue').dialog({\n" +
"autoOpen: false,\n" +
"modal: true,\n" +
"width: 450,\n" +
"height: 320,\n" +
"title: \"Some title\",\n" +
"resizable: false,\n" +
"draggable: false });\n" +
"$('#panelIssue').dialog('open');\n" +
"});";
clientScriptManager.RegisterClientScriptBlock(this.GetType(), "MyScript", scriptText, true);
}
You should also look at the capabilities of the dialog creation in jQuery to see what other settings will fit your scenario better. I just used a few of the params that I have in an example of mine.

Related

Response.Redirect in DNN using CommandArguments

I have an imagebutton in ASCX file which code is
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandArgument='<%#Eval("ItemID") %>' ImageUrl="~/images/edit.gif" OnClick="ImageButtonEdit_Click" />`
and the behind code is
protected void ImageButtonEdit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton editing = (ImageButton)sender;
Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "AddCollectionItem", "mid=" + this.ModuleId) + "?ID=" + Convert.ToInt32(editing.CommandArgument));
}
Problem is it doesn't Redirect to the page or any other thing?
The Response.Redirect when I try to use it in general it doesnt work in this part only and in the other site it is working well
You're trying to pass a URL with two ?s in it, that might be causing part of the problem. Your second argument, ?ID= should be "&ID="
protected void ImageButtonEdit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton editing = (ImageButton)sender;
Response.Redirect(Globals.NavigateURL(TabID, "AddCollectionItem", "mid=" + this.ModuleId + "&ID=" + Convert.ToInt32(editing.CommandArgument)));
}
You also could pass that inside of NavigateURL instead of outside of NavigateURL as you are doing.
If your module is properly inheriting from PortalModuleBase you don't need PortalSettings.ActiveTab.TabID, just call TabID directly.
Also, are you doing any of this inside of an Update Panel? Perhaps that is causing redirect issues?

how to add onclick or mouseover event to imagebutton in runtime

ive imagebuttons which created in runtime from database and i need to add them onclick or mouseover event but i failed
imagebutton.click += new ImageClickEventHandler(imageButton_Click);
im adding this when i created imagebuttons, what i need else
also i tried javascript but its not firing too;
<script type="text/javascript">
function SetProductImage(imgID) {
document.getElementById('imgProduct').src=document.getElementById(imgID).src;
}
</script>
imgProductImage.Attributes["onclick"] = "javascript:SetProductImage('" + imgProductImage.ID + "')";
thank you...
From your comment I am assuming you are doing something like:
void Page_Load(object s, EventArgs e)
{
ImageButton img = new ImageButton();
img.click += new ImageClickEventHandler(imageButton_Click);
Controls.Add(img);
}
This won't work because the PostBack mechanism sends the Id of the control back in the EventTarget field and the target isn't specified so it doesn't know where to handle it.
To get this to work properly I usually wrap the display in a custom user control and override the CreateChildControls() method. This ensures that the control is created at the correct time and is available when the postback handling is done.
Are you trying to fire up a code behind event? or a javascript event? If you are trying to add a javascript event to your element, you should Add the attribute. It's not currently there when you use: Attributes["onclick"]. So It'd be like:
imgProductImage.Attributes.Add("onclick","SetProductImage('" + imgProductImage.ID + "')");
Ok. I think I found out what you are trying to achieve. You have to explicitly set the ID of the control in Page_Load:
ImageButton img = new ImageButton();
img.ID = "image1";
img.ImageUrl = "YOUR_IMAGE_URL";
img.OnClientClick = "SetProductImage('" + img.ID + "');return false;";
Controls.Add(img);
Just make sure if you have to define multiple image buttones, Set a unique ID for each of them.

How to add persistent dynamic controls based on user input (not on initial page load)

I am familiar with creating and persisting dynamic controls on the first load of a page and on subsequent postbacks but I am having trouble with the following user initiated scenario...
In my demo I have a placeholder, two buttons and a literal
<div>
<asp:PlaceHolder ID="phResponses" runat="server" />
</div>
<div>
<asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="Add"/>
<asp:Button ID="btnInspect" Text="Inspect" runat="server" OnClick="Inspect"/>
</div>
<div>
<asp:Literal ID="litResult" runat="server"/>
</div>
I want the user to be able to click the add button to provide a response so I have...
protected void Page_Init(object sender, EventArgs e)
{
BuildControls();
}
protected void Add(object sender, EventArgs e)
{
BuildControls();
}
protected void BuildControls()
{
phResponses.Controls.Add(new LiteralControl { ID = "response_" + _Count.ToString() });
_Count++;
}
_Count is a static member variable to enable me to have unique ids for the new controls. I realise I need to rebuild the dynamic controls on Page_Init but the problem is that I end up with 2 new Literal controls on every postback. Also if any Text property is put into the new controls it is lost when the controls are rebuilt.
So how do I avoid adding multiple controls and how do I persist newly added properties when rebuilding these controls?
I use the following to inspect the responses
protected void Inspect(object sender, EventArgs e)
{
foreach (var control in phResponses.Controls)
{
if (control is LiteralControl)
{
litResults.Text += "<p>" + control.Text + " : " + control.ID + "</p>";
}
}
}
Which itself adds another unwanted control because of the rebuilding on Page_Init
I'd not sure that I quite understand what you're asking, but it looks like you just want to ensure that BuildControls is only called once per lifecycle. You could do that by making the following changes:
Add a new private bool _isControlsBuilt = false;.
Change Page_Init to check _isControlsBuilt before calling BuildControls.
Set _isControlsBuilt to true within BuildControls.
Make sure that BuildControls occurs earlier in the page lifecycle than Page_Init.
As for losing the values of controls on postback, it'll be that they're never hitting the viewstate. I'm not sure if it'd work, but my first guess would be to add a line to the end of BuildControls to call Page.RegisterRequiresControlState:
protected void BuildControls()
{
LiteralControl newLiteral = new LiteralControl { ID = "response_" + _Count };
this.RegisterRequiresControlState(newLiteral);
phResponses.Controls.Add(newLiteral);
_Count++;
_isControlsBuilt = true;
}
If that doesn't work (which might imply that it's the _view_state, not the _control_state that matters to you here), you may need to look at rolling your own viewstate. I wrote about how to do that in my answer to #3854193, which you might find useful.

jQuery not working on Popup Window

I am using ASP.Net and jQuery + jQuery UI. Everything works fine with the jQuery on any other page, however when I create a popup window with window.open(...) jQuery seems to no longer function.
I have all of the script files included on the Popup's Master page, so am not sure why it won't fire.
Any Thoughts?
I am not sure if you use window.open you are creating a page class that inherits from the master page.
So JQuery is probabaly not included in that pop up. In addition popups aren't nice. people download all kind of things so they don't open. but it can be done like this:
protected void Button1_Click(object sender, EventArgs e)
{
string queryString =
"http://localhost:39208/TreeView.aspx?param1="
+ TextBox1.Text.Trim();
string newWin =
"window.open('" + queryString + "');";
ClientScript.RegisterStartupScript
(this.GetType(), "pop", newWin, true);
}
ClientScript.RegisterStartupScript will bring your jquery in the pop up

ASP.NET WebForms + Postback then open popup

I have a LinkButton that has to postback to perform some logic.
Once it is finished, instead of loading the page back up in the browser, I want to leave it alone and pop open a new window.
So far, the best idea I've had is to put the LinkButton in an UpdatePanel, and have it render some JavaScript out when it reloads, yet I think that is totally hacky. Also, if I recall right, JavaScript within a update panel won't run anyways.
Any other ideas?
Use LinkButton.PostBackUrl to set a different page to POST to, and some client script to get a new window (and the old target restored so that future postbacks work normally). The 2nd page can use PreviousPage to get access to any needed state from the original page.
<script runat="server">
void lnk_Click(object sender, EventArgs e) {
// Do work
}
</script>
<script type="text/javascript">
var oldTarget, oldAction;
function newWindowClick(target) {
var form = document.forms[0];
oldTarget = form.target;
oldAction = form.action;
form.target = target;
window.setTimeout(
"document.forms[0].target=oldTarget;"
+ "document.forms[0].action=oldAction;",
200
);
}
</script>
<asp:LinkButton runat="server" PostBackUrl="Details.aspx" Text="Click Me"
OnClick="lnk_Click"
OnClientClick="newWindowClick('details');" />
Here is the code:
protected void Button1_Click(object sender, EventArgs e)
{
// Do some server side work
string script = "window.open('http://www.yahoo.com','Yahoo')";
if (!ClientScript.IsClientScriptBlockRegistered("NewWindow"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),"NewWindow",script, true);
}
}
One thing you could try is to have your LinkButton OnClick event do its processing, then register a Page.ClientScript.RegisterStartupScript with the popup code, which will put some Javascript into the tag to fire off after the page loads. This should launch your new window after the processing completes.
EDIT: Reading your comment, I believe you can still use this approach, have your results stored in a session variable, and then have the popup page pull the results from there.

Resources