Showing posts with label modalpopup. Show all posts
Showing posts with label modalpopup. Show all posts

Wednesday, March 28, 2012

Programatically set ImageUrl in ModalPopup

Hi I have a DataList that populates from an SQL datasource. One control in the DataList is an image whose ImageUrl i am currently setting as follows:

protected void dlItems_ItemDataBound(object sender, DataListItemEventArgs e)
{
Label lblItemID = (Label)e.Item.FindControl("lblItemID");
Image imgItem = (Image)e.Item.FindControl("imgItem");

imgItem.ImageUrl ="~/Images/Items/" + lblItemID.Text +".jpg";
}


I want to be able to click the image and get a modal popup which displays a large view of the image. I have wrapped the image (imgItem) in a LinkButton. Heres the code:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public void SetLargeImagePath(int contextKey)
{
imgLargeImage.ImageUrl = "~/Images/Items/" + contextKey.ToString() + ".jpg";
}
</script>
<div>
<asp:DataList ID="dlItems" runat="server" DataKeyField="pkiItemID" Width="100%" OnItemDataBound="dlItems_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("pkiItemID")%>' Visible="False" CssClass="ItemText"></asp:Label>
<asp:Label ID="bSpecialOfferLabel" runat="server" Text='<%# Eval("bSale")%>' Visible="False" CssClass="ItemText"></asp:Label>
<table width="100%" class="Table">
<tr>
<td rowspan="6" valign="top">
<asp:LinkButton ID="lnkImage" runat="server">
<asp:Image ID="imgItem" runat="server" Width="150px" AlternateText="Click for a larger view" />
</asp:LinkButton>
<cc1:ModalPopupExtender ID="ModalPopupExtender" runat="server" TargetControlID="lnkImage" PopupControlID="pnlLargeImage"
BackgroundCssClass="modalBackground"
OkControlID="OkButton" DynamicControlID="Label1" DynamicContextKey='<%# Eval("pkiItemID")%>' DynamicServiceMethod="SetLargeImagePath" >
</cc1:ModalPopupExtender>
</td>
<td style="width: 104px; padding-left: 5px;"><h2>Title:</h2></td>
<td><asp:Label ID="szTitleLabel" runat="server" Text='<%# Eval("szTitle")%>' CssClass="ItemText"></asp:Label></td>
<td style="width: 112px" rowspan="6" valign="middle">
</td>
</tr>
<tr>
<td style="width: 104px; padding-left: 5px;"><h2>Type:</h2></td>
<td><asp:Label ID="szItemTypeLabel" runat="server" Text='<%# Eval("szItemType")%>' CssClass="ItemText"></asp:Label></td>
</tr>
<tr>
<td style="width: 104px; padding-left: 5px;"><h2>Status:</h2></td>
<td><asp:Label ID="szItemStatusLabel" runat="server" Text='<%# Eval("szItemStatus")%>' CssClass="ItemText"></asp:Label></td>
</tr>
<tr>
<td style="width: 104px; padding-left: 5px;"><h2>Description:</h2></td>
<td><asp:Label ID="szDescriptionLabel" runat="server" Text='<%# Eval("szDescription")%>' CssClass="ItemText"></asp:Label></td>
</tr>
<tr>
<td style="width: 104px; padding-left: 5px;"><h2>Price:</h2></td>
<td><asp:Label ID="mPriceLabel" runat="server" Text='<%# String.Format("{0:c}", Eval("mPrice"))%>' CssClass="ItemText"></asp:Label></td>
</tr>
<tr>
<td style="width: 104px; padding-left: 5px;"><h2><asp:Label ID="mOfferPriceLabelStatic" runat="server" Text="Offer Price:"></asp:Label></h2></td>
<td><asp:Label ID="mOfferPriceLabel" runat="server" Text='<%# String.Format("{0:c}", Eval("mSalePrice"))%>' CssClass="ItemText"></asp:Label></td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:Button ID="btnAddToBasket" runat="server" CssClass="Button" Text="Add To Basket" />
<asp:Button ID="btnBuyNow" runat="server" CssClass="Button" Text="Buy Now" /></td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="dlItemsDataSource" runat="server" CancelSelectOnNullParameter="False" ConnectionString="<%$ ConnectionStrings:ConnectionString%>" SelectCommand="spREAD_tblItems" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="pkiItemID" Type="Int32" />
<asp:Parameter DefaultValue="BRACELET" Name="fkiItemTypeShortText" Type="String" />
<asp:Parameter DefaultValue="FORSALE" Name="fkiItemStatusShortText" Type="String" />
<asp:Parameter DefaultValue="True" Name="bPublic" Type="Boolean" />
<asp:Parameter Name="bSale" Type="Boolean" />
<asp:Parameter Name="mPrice" Type="Decimal" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:Panel ID="pnlLargeImage" runat="server" Width="50%">
<asp:Panel ID="pnlHeader" runat="server" Style="cursor: move;background-color:#DDDDDD;border:solid 1px Gray;color:Black" HorizontalAlign="Center" Width="100%">
<div>
<p>Larger view of item</p>
</div>
</asp:Panel>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Image ID="imgLargeImage" runat="server" Width="200px" />
<p style="text-align: center;">
<asp:Button ID="OkButton" runat="server" Text="OK" />
</p>
</div>
</asp:Panel>
</asp:Content>

This currently will load the ModalPopup and give Label1 the value of the pkiItemID, so i am successfully getting the item ID and passing it to the ModalPopup.

The problem is that the ImageURL is not being set. Oddly the SetLargeImagePath() method does not appear to run (breakpoints are ignored), although the Label1 value is being set??

Does anyone have any ideas how to get the image to appear in the ModalPopup?

Many thanks

Hi Assimalyst,

Assimalyst:

<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public void SetLargeImagePath(int contextKey)
{
imgLargeImage.ImageUrl = "~/Images/Items/" + contextKey.ToString() + ".jpg";
}
</script>

Please contextKey should be a string type. For example, public void SetLargeImagePath(string contextKey){};

We suggest that you should use a debugging tool such as Web Development Helper or Fildder etc.

Assimalyst:

I want to be able to click the image and get a modal popup which displays a large view of the image.

Here is a sample which implement on the client side.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Change Picture</title> <style> .modalBackground { background-color:Gray; filter:alpha(opacity=70); opacity:0.7; } .modalPopup { background-color:#FFD9D5; border-width:3px; border-style:solid; border-color:Gray; padding:3px; width:250px; } </style></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager><asp:Image ID="Image1" runat="server" ImageUrl="~/pic/upg_Business.jpg" imgUrl="../pic/upg_HomePremium.jpg" onclick="changePicture(event.srcElement.imgUrl)"/><asp:Image ID="Image2" runat="server" ImageUrl="~/pic/upg_HomeBasic.jpg" imgUrl="../pic/upg_Ultimate.jpg" onclick="changePicture(event.srcElement.imgUrl)"/> <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" Height="200px" Width="300px" style="display:none"> <asp:Image ID="Image3" runat="server" ImageUrl="~/pic/AJAX.gif"/> <asp:Button ID="Button2" runat="server" Text="Cancel" /> </asp:Panel> <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Button1" PopupControlID="Panel1" CancelControlID="Button2"> </ajaxToolkit:ModalPopupExtender> <div style="display:none"> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> <script type="text/javascript" language="javascript"> function changePicture(imgUrl){ $get("<%=Image3.ClientID%>").src = imgUrl; $get("<%=Button1.ClientID%>").click(); } </script> </div> </form></body></html>

I hope this help.

Best regards,

Jonathan

Monday, March 26, 2012

Programmically create ModalPopup

I have load data and show it in DataGridView control.
What can I do to archieve the following criteria:

When user click on a specific cell - it will bring up detail records in modalPopup extender control?

See forum posthttp://forums.asp.net/thread/1613190.aspx. It talks about PopupControl but you could use the same principle here.

Wednesday, March 21, 2012

Question About using a modal popup for each row in a gridview

Hello, I'm trying to have a modalpopup control fire in a gridview, but I dont know how to approach the problem. Basically I have a linkbutton in each row of my gridview. The code for this is:

<asp:ButtonField HeaderText="Primary Subject" SortExpression="subject" ButtonType="Link" commandname="Subject" DataTextField="subject" />

which does not have an ID attribute, but of course modal popup control needs the attribute TargetControlID="something"

How do I make the buttonfield have an id? or be able to use it inside a modal popup?

Thanks

Use a TemplateField with a LinkButton instead.


Question About using a modal popup for each row in a gridview

Do you really need the trigger (the button is inside the panel, so it should trigger without it)?

Does it work if you remove the Popup extender?

Question: DynamicServicePath

In trying to troubleshoot my issues with the migration I noted that for the sample ModalPopUp example with the toolkit automatically populates the DynamicServicePath

From the toolkit example (Beta release )

Sys.Application.add_init(function() {
$create(AjaxControlToolkit.ModalPopupBehavior, {"BackgroundCssClass":"modalBackground","CancelControlID":"ctl00_ContentPlaceHolder1_CancelButton","DropShadow":true,"DynamicServicePath":"/test/ModalPopup/ModalPopup.aspx","id":"ctl00_ContentPlaceHolder1_ModalPopupExtender","OkControlID":"ctl00_ContentPlaceHolder1_OkButton","OnOkScript":"onOk()","PopupControlID":"ctl00_ContentPlaceHolder1_Panel1"}, null, null, $get('ctl00_ContentPlaceHolder1_LinkButton1'));
});

In my application - all of my urls are dynamically served... and so for instance for this page url: /localhost/SitesEasyTest/admin/EditNamedPages/default.aspx

The following gets generated:

Sys.Application.add_init(function() {

$create(AjaxControlToolkit.ModalPopupBehavior, {"BackgroundCssClass":"modalBackground","DropShadow":true,"DynamicServicePath":"/SitesEasyTest/default.aspx","id":"PopUp","PopupControlID":"ctl00_ctl00_ctl00_PanelModal"}, null, null, $get('ctl00_ctl00_ctl00_Target'));

It is not grabbing the actual url that it should. In the ModalPopUp example and in my own code the DynamicServicePath is not declared it is added by the toolkit at runtime...

I attempted to correct the value via code behind:

MyPopUp = (ModalPopupExtender)GetOptionalControl(skin,"MyPopUp");

MyPopUp.DynamicServicePath = (CommunityGlobals.CalculatePath("Default.aspx")); // properly generates relative path url to the dynamic page

However, the following error is displayed when compiled and tested in a browser:

DynamicControlID must be set

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.ArgumentException: DynamicControlID must be set

[ArgumentException: DynamicControlID must be set] AjaxControlToolkit.DynamicPopulateExtenderControlBase.EnsureValid() +372 AjaxControlToolkit.ExtenderControlBase.GetScriptDescriptors(Control targetControl) +98 Microsoft.Web.UI.ExtenderControl.Microsoft.Web.UI.IExtenderControl.GetScriptDescriptors(Control targetControl) +7 Microsoft.Web.UI.ScriptControlManager.RegisterScriptsForExtenderControls() +187 Microsoft.Web.UI.ScriptManager.OnPreRender(EventArgs e) +402 System.Web.UI.Control.PreRenderRecursiveInternal() +77 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

In either case - the script generated does not generate a DynamicControlID by defaultand so it is puzzling why trying to 'correct' what is incorrect by ovveriding the value - would cause the error?

Does theDynamicServicePath have any purpose if you are not using the dynamic population feature? Otherwise can chalk this up as being a potential issue down the road if I start to use the dynamicservices features...

Thanks!

Relatively easy code fix for the Dynamic Service Path... For sites like mine where I do url rewriting as all content is database driven the following change needs to be applied:

in the ServicePathConverter.cs

namespace AjaxControlToolkit {

publicclassServicePathConverter :StringConverter {

publicoverrideobject ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,object value,Type destinationType) {

if (destinationType ==typeof(string)) {

string strValue = (string)value;

if (String.IsNullOrEmpty(strValue)) {

HttpContext currentContext =HttpContext.Current;

if (currentContext !=null) {

//OLD

// return currentContext.Request.FilePath;

//Changed by Jody to allow sites that rewrite urls not to

//have paths = /<root>/default.aspx

return currentContext.Request.RawUrl;

}

}

}

returnbase.ConvertTo(context, culture, value, destinationType);

}

}

}

Otherwise, on sites that manage dynamic content and generate urls - the path will always be the root...The filepath works if say you have all 'static' pages in folders etc... but not in dynamically generated scenarios. The code fix will allow for both styles of content to be served properly.

Questions - ModalPopup sample code

Hello, I'm trying to replicate the functionality presented here specific to the ModalPopup control:

http://ajax.asp.net/ajaxtoolkit/ModalPopup/ModalPopup.aspx

I've created a C# Ajax-enabled website project, have added the ScriptManager, ModalPopup, Panel and various Button controls, but although I've bound the ModalPopup to the button I want to have it use to activate my modal Panel, and though the project builds successfully, it doesn't react in the same way as the sample.

Here's the code from my skin file:

1<%@dotnet.itags.org. Page Language="C#" trace="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
23<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>45<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
6<html xmlns="http://www.w3.org/1999/xhtml">
7<head>
8 <title>Untitled Page</title>
9</head>
10<body>
1112 <form runat="server">
1314 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
15
16 <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px" Visible="False" >
17
18 <asp:Button ID="OkButton" runat="server" Text="OK"></asp:Button>
19 <asp:Button ID="CancelButton" runat="server" Text="Cancel"></asp:Button>
2021 </asp:Panel>
22
23 <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"24 TargetControlID="btnAddServer"25 PopupControlID="Panel1"26 BackgroundCssClass="modalBackground"27 DropShadow="true"28 OkControlID="OkButton"29 OnOkScript="onOk()"30 CancelControlID="CancelButton" />
31
32 <br />
33 <center>
3435
36 <asp:Panel ID="PanelMain" runat="server" Height="25px" Width="125px" HorizontalAlign="Center">
37
38 <asp:Button ID="btnAddServer" runat="server" Text="Add" Width="33px" /></td>
39
40 </asp:Panel>
41
42 </form>
43
44</body>
45</html>
46

At this point, I'm guessing that the missing piece of the puzzle lies with the CSS stuff and/or the master page, as that I haven't brought over from the sample project utilizing ModalPopup. Can someone please help me pinpoint what I'm missing that is preventing this from working?

Thanks in advance for any help. Smile

I found a fix which appears to work, but I'm still too much of a newbie to understand why. If someone could please explain I'd appreciate it.

After adding this to the "httpHandlers" section of my site project's Web.config file, my ModalPopup works:

<addverb="GET"path="ScriptResource.axd"type="Microsoft.Web.Handlers.ScriptResourceHandler"validate="false"/>

Thanks to this blogger: http://www.thecodinghumanist.com. The error I was encountering in the Javascript console (Firefox) was "Sys not defined", and the above appears to have addressed that, enabling the popup to work... somehow. Smile