Showing posts with label aspx. Show all posts
Showing posts with label aspx. Show all posts

Wednesday, March 28, 2012

Extenders inside template column of a gridview

[THIS IS FOLLOWED FROM POSThttp://forums.asp.net/thread/1498604.aspx in ControlToolKit forum, where I have been suggested to post in this forum]

I have a gridview that contains a template column, with 2imageButtons inside it (Basically Edit Item and Delete Item) and aconfirmButtonExtender linked on the delete image button. Everything wasworking well in Beta 2, but in RC1, in design time, it does not renderthe gridview, saying that a scriptmanger is required and must existbefore any control on the page... It works great at runtime, but it isquite annoying at design time...

Will this be fixed in a future release?

I have posted an example of page that reproduce the issue in the other post (see link at top of message)

Thanks!This is a known issue with Ajax RC 1.0. Wish this issue will be resolved in ASP.NET AJAX RTM.

extender control inside databound controls

I will show you a piece of code that worked with the last CTP:

<%@dotnet.itags.org.PageLanguage="C#"AutoEventWireup="true"CodeFile="test.aspx.cs"Inherits="test" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Databound Controls</title>

</head>

<body>

<formid="form1"runat="server">

<atlas:ScriptManagerEnablePartialRendering=trueID="sm"runat="server"></atlas:ScriptManager>

<

asp:RepeaterID="rpt"runat="server"DataSourceID="xmlData"OnItemDataBound="rpt_ItemDataBound">

<ItemTemplate>

<%# XPath("name") %><asp:TextBoxID="textInside"runat="server"></asp:TextBox><br/>

<atlastoolkit:TextBoxWatermarkExtenderID="watInside"TargetControlID="textInside"runat="server"WatermarkText="Inside TextBox"/>

</ItemTemplate>

</asp:Repeater>

Somebody<asp:TextBoxID="textOutside"runat="server"></asp:TextBox><br/>

<atlastoolkit:TextBoxWatermarkExtenderID="watOutside"runat="server"TargetControlID="textOutside"WatermarkText="Outside TextBox"/>

<asp:XmlDataSourceID="xmlData"runat="server"XPath="contacts">

<Data>

<contacts>

<name>Peter</name>

</contacts></Data>

</asp:XmlDataSource>

</form>

</body>

</html>

This piece of code is not working anymore more. The responsible is the “new” behaviour of the TargetControlID. Now, you need the ClientID of the target control, not the usual control ID.

In the easy case, both (ClientID and ID) are the same, but not inside databound controls. So what you need now is:

First, you need to DataBind() the Page at load stage; this is because you need the extender controls in the control tree before the (Pre)Render stage; otherwise, they won’t render.

Second, you need to delete the TextBoxWatermarkExtender instantiated declaratively, and add it at runtime:

protectedvoid rpt_ItemDataBound(object sender,RepeaterItemEventArgs e)

{

AjaxControlToolkit.TextBoxWatermarkExtender tbe =new AjaxControlToolkit.TextBoxWatermarkExtender ();

tbe.TargetControlID = (e.Item.FindControl("textInside")asTextBox).ClientID;

tbe.ID ="watInside";

tbe.WatermarkText ="Inside Text";

tbe.ResolveControlID +=new AjaxControlToolkit.ResolveControlEventHandler(tbe_ResolveControlID);

e.Item.Controls.Add(tbe);

}

Finally, the extender control is not still able to find the TargetControlID, so you have to make up your own FindControl (like this one) :

void tbe_ResolveControlID(object sender, AjaxControlToolkit.ResolveControlEventArgs e)

{

e.Control =Util.FindControl(e.ControlID,"_",this.Page);

}

publicstatic System.Web.UI.Control Util.FindControl(string cliendid,string separator, System.Web.UI.Control root)

{

int i = 0;

string[] sepControls = cliendid.Split(separator[0]);

System.Web.UI.Control fc =null;

for (fc = root; (null != fc) && i < sepControls.Length; i++)

{

fc = fc.FindControl(sepControls[i]);

}

if (fc !=null && fc.ClientID == cliendid)return fc;

elsereturnnull;

}

If someone knows any other way to simplify this case, please tell me. Otherwise, my project has become a nightmare.

fran

Your original sample works fine for me with the addition of only:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
rpt.DataBind();
}

More details here:http://forums.asp.net/thread/1441672.aspx.

Monday, March 26, 2012

Exposing Web Services from an ASPX page does not work

Hi All,

I am working with ATLAS technology. i am trying to imitatehttp://atlas.asp.net/quickstart/atlas/doc/services/default.aspx#webpage
"Exposing Web Services from an ASPX page"

i am having an aspx page which is inherited from our own Page class not from System.Web.UI.Page.

public partial class MainSearchPage : some namespace.MyPage

but this example does not work in this context.because in javascript, function OnWebRequestComplete1(result) returns null as result.

please help me in this issue.

thanks
Jaideep

hello.

well, i think that it should work. here's an example:

pypage.cs
namespace Test
{
public class MyPage:Page
{
[WebMethod]
public string HelloWorld(string s)
{
return "Hello '" + s + "'";
}
}
}

now, the aspx page:


Untitled Page

function handle()
{
PageMethods.HelloWorld( "Luis", handleComplete );
}

function handleComplete( res )
{
alert( res );
}

here everything is working properly...

Yes, this should work. To debug your issue, I would try:

Check on the server whether your method gets called at all.

yes, my server side code in code behind of aspx is not getting called.
in javascript side:

function handle()

{

PageMethods.HelloWorld("jaideep", handleComplete );

}

function handleComplete( res )

{

debug.dump(res,"Result",true);

alert( res );

}

in debugger, result is null.also my aspx is having one user control also.i tried to put debug in my code behind.but function HelloWorld is not getting called.

any clue as to what is happening.
thanks

jaideep


after viewing request/response in Fiddler ,i got a message indicating a corrupted viewstate.
same is the problem in following post:
http://forums.asp.net/1205269/ShowPost.aspx "ViewState and Atlas ".
and as per the instruction given , i removed "type =="hidden" condition from file
Atlas.js on line 3495.and now it is working!! but this approach is correct or not?
please let me know about this.
Thanks

JAideep


Copying my reply from the other thread:

I tried a few things, but was not able to repro. Could you give this a try with the March CTP to see if it still happens?

thanks,
David

Saturday, March 24, 2012

Exception while adding ToolkitScriptManager dynamically in OnPreInit event

Hi,

I am trying to add ToolkitScriptManager dynamically as suggested in threadhttp://forums.asp.net/p/1039254/1777798.aspx in OnPreInit event. But I am getting exception "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." What could be reason?

Here is code...protectedoverridevoid OnPreInit(EventArgs e)

{

if (ToolkitScriptManager.GetCurrent(this.Page) ==null)

{

ToolkitScriptManager Manager =newToolkitScriptManager();

Manager.EnablePartialRendering =true;foreach (Control cinthis.Controls)

{

if (cis System.Web.UI.HtmlControls.HtmlForm)

c.Controls.AddAt(0, Manager);

}

}

}

Here is exception

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

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.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Source Error:

Line 519: if (c is System.Web.UI.HtmlControls.HtmlForm)Line 520:Line 521: c.Controls.AddAt(0, Manager);Line 522: }Line 523: }

Any idea what could resolve this issue?

Thanks, V

Hivishant.patel,

Thanks for your post!

As far as I know, This error happens when asp.net encounterscodeblocks like <%= %>,in a UserControl andthecode-behindthe UserControl is trying to modifythecontrolscollection ofthe page (usually a LoadControl statement).

Nice solutions:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

TheControlscollectioncannotbemodifiedbecausethecontrolcontainscodeblocks

Solution detail snippet:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Now I feel like a complete *** to never have run into this before. That's one hell of an assumption to miss about ASP.NET it seems. I have tons of pages where there's <%= %> markup on it. Especially in script code to get the appropariate ClientID:

<%= this.txtCompany.ClientID %>

Luckily there's a workaround for code like this by using DataBinding expressions instead:

<%# this.txtCompany.ClientID %>

This works fine for simple expressions. The difference here is that <%= %> expressions are embedded into the ASP.NET output as part of the generated Parse Tree class, whereas the <%# %> expressions are embedded at runtime.

Another workaround is to force any script code into the content of a server control and remove it from the Page class or the Content container that you're adding controls to.

<div runat="server">

<script type="text/javascript">

function ShowCreditCard()

{

var IsPayPal = false;

for(x=0; x<4; x++ )

{

var ctl = $("<%= this.txtCCType.ClientID %>_" + x.toString());

if (ctl == null)

break;

if (ctl.value == "PP" && ctl.checked)

{

IsPayPal = true;

break;

}

}

var loCC = $("<%= this.trCreditCard.ClientID %>");

if (loCC == null)

return;

var loCC2 = $("<%= this.trCreditCardExpiration.ClientID %>");

if (IsPayPal)

{

loCC.style.display = "none";

loCC2.style.display = "none";

}

else

{

loCC.style.display = "";

loCC2.style.display = "";

}

}

</script>

</div>

This works as well, although this is also pretty ugly. In my case this is probably the easier solution though, because most of my markup expressions are doing exactly what's happening above: Embedding ClientScript Ids into JavaScript.

I still don't see why the control collection can't be modified if there are <% %> blocks on the page. Those blocks are just turned into Response.Write() commands, or raw code blocks. I don't see how this affects the Controls collection that would require this sort of error.

In my situation here I was able to get by just switching to <%# %> or wrapping sections with a server tag. Even if that's not an option the above code captures the error and goes on. This means the warning icons don't show up, but the rest of the error handling showing the summary and error control linking etc. all still works.

Can anybody think of another more reliable way to inject markup into the page dynamically from outside of the control rendering? In a previous rev of my databinding tools I had custom controls and I simply took over post rendering which was reliable. But this is not so easily done externally… I can think of possibly hooking up the Render method and calling back into my custom control, but man does that seem ugly.

Related Posts:

The Controls collection cannot be modified because the control...

Error:The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

The Controls collection cannot be modified because the control contains code blocks ...


Jin-Yu Yin - MSFT:

This error happens when asp.net encounterscodeblocks like <%= %>, or <%# %>

Hi Jin-Yu Jin,

This is not correct, but your "Solution detail snippet" also clear about this.

So

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

only shown when you try to modify acontrol's control collection, when there is <%= %> in thecontrol's root childs . <%# %> not problem at all.

Example:

<asp:PlaceHolder runat="server" id="WeDynamicallyAddControlForThisInCodeBehind">

<%= DateTime.Now.ToString( ) %> <%-- Won't work! --%>

<%# DateTime.Now.ToString( ) %> <%-- Work! (Need a call to DataBind in code behind to real show something) --%>

<asp:PlaceHolder runat="server" id="SomeChild">

<%= DateTime.Now.ToString( ) %> <%-- Work!!! Because we modify the control collection of WeDynamicallyAddControlForThisInCodeBehind and not SomeChild! --%>

<%# DateTime.Now.ToString( ) %> <%-- Of course work! --%>

</asp:PlaceHolder>

<%-- Dynamic controls go here --%>

</asp:PlaceHolder>

</asp:PlaceHolder>

Codebehind:

WeDinamicallyAddControlForThisInCodeBehind.Controls.Add( ... ); //or similar to trigger the error in some case see above.


Thank you for pointing out my mistake,I've corrected it,thanks again:)


Hi Jin-Yu Yin,

Thanks for you reply.

I have visited the posts you specified before I created this post Smile All of posts you specified discuss about custom code we write for user controls, images etc. with <% %> tags inside its markup/code and we can resolve this error by updating code as specified in these post.

In my case; I am not adding any of my own user controls or images etc. I am receving error when I am trying to addToolkitScriptManager in the control list.

How can I change code for ToolkitScriptManager to resolve this error? What should I do to addToolkitScriptManagerdynamically in my base page?

Thanks, Vishant


Hi

It not means that the issue come up when you add any of user controls or images to the page.

It means that the issue come up when you add any contorl(inludeToolkitScriptManager) dynamically in thecode-behind to a page or a user contorl and there iscodeblocks like <%= %> at where the contorl add to.

For example:

<contorlA>

<%= %>

</contorlA>

When you try to add <contorlB> to <contorlA> and want to change the code to:

<contorlA>

<%= %>

<contorlB>

</contorlB>

</contorlA>

Then the error comes up,because you cann't modifythecontrolscollection ofthe contorlA as there is a <%= %>codeblocks in it.

As I mentioned in my first reply, You can resolve this issue by change the code to:

<contorlA>

<div><%= %></div>

</contorlA>

Then add contorls to contorlA dynamically without the error.

Good luck!


Hi V,

Usuallly there could be two problems when you try to add a scriptmanager/toolkitscriptmanager to a page dinamically:

a) Adding dynamically the scriptmanager to the page is tricky, because it should be donevery early in the page lifecycle (forget page_load immediatelly for this...)

This is a good thread how to do this:

Re: Is it possible to add a ScriptManager to a page dynamically?

b) The <% %> code block error.

If you follow the instruction from a) you need to eliminate all <%= %> below the form tag, except when <%= %> are in a child asp.net control of the form tag.

Ps:

Yin-Yu-Yin suggest to use this for workaround:

<contorlA>

<div><%= %></div>

</contorlA>

but this won't work!!!

You need aserver side asp.net control around your code block, for example:

<contorlA>

<asp:Panel runat="server"><%= %></asp:Panel>

</contorlA>

this should work...


example "Returning a DataTable" doesnt work!

on :http://atlas.asp.net/quickstart/default.aspx, the "Returning a DataTable" doesn't work( on atlas.asp.net. and on my local server too) I don't understand!

Any data are binding!

I'm using IE6! I see somewhere that it's not <binding> but <web:binding> it doesn't work better!

Help me please

ALCINA.

thanks to Garbin. he has provide one nice example in
http://forums.asp.net/1198570/ShowPost.aspx

check it out