Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Wednesday, March 28, 2012

Accordian - How to refer to contained controls in code behind?

Hi,

If i put regular asp.net controls into an accordian pane they are not visible in the code behind file. In other words i cant get mydropdown.selectedvalue if mydropdown is contained in an accordian pane because the compiler will complain that mydropdown doesn't exist? My work around has been to put content into user controls and put the ascx's into the content panes but is this standard practise?

Thanks

Richard

Hi Richard,

Since the AccordionPanes can be databound, they implement theINamingContainer interface and you're unable to access them directly in your code behind. You can work around this withDropDownList list = MyAccordion.FindControl("MyDropDown") as DropDownList;.

Thanks,
Ted

Accessing the ScriptManager in code behind

In a User Control, how do you access the ScriptManager in code behind? The ScriptManager gets placed on the "parent" aspx page, but seems not to be accessible from any user controls ascx that get added to that page.

Specifically, I would like to capture the ScriptManager.PreRender event for use in a Repeater control.

Is there a way to raise an event in the parent ASPX page that the child user control can pick up?

if you want to call parent control,

you can use

this.Parent.FindControl..

i am not sure why you want user control to do something with "ScriptManager.PreRender event"

why cant you use ScriptManager.PreRender event to do something for your user control ?

you can always use

Page.FindControl to call your use control in the ScriptManager.PreRender event


Hey, thanks for your reply. I could really use some help.

The goal is to use an update panel inside a repeater, both of which are use controls loaded onto a page. There are some inherint challenges with this. The first being that 1) the ScriptManager is not accessible because it is in the parent, 2) the update panel elements don't seem available at the time of the event. Below is my code to access them, but the me.parent.findcontrol always returns "nothing."

My research has shown me that the ScriptManager.PreRender is the event that I can access/ change these values.

My entire page works great, except the update panel inside the repeater does not update and I cannot access the objects within it (i,e, a label)

Sub handleTypeTest(ByVal senderAs System.Object,ByVal eAs System.EventArgs)

Try

Dim tempVAsString = ddlAnswerType.SelectedValue

ddlAnswerType.Visible =False

Dim tempUPAs UpdatePanel =Me.Parent.FindControl("upDropDowns")

IfNot tempUPIsNothingThen tempUP.Update()

Dim tempLabelAs Label =CType(Me.Parent.FindControl("lblTest"), Label)

tempLabel.Text ="changed"

Catch exAs Exception

EndTry

EndSub

Accessing objects outside update panel

I am trying to access the label or div which is outside the update panel after its post back occurs.

here is code...

how can I make that alert work in Button1_Click method?

thanks in advance...

protected void Page_Load(object sender, EventArgs e) { Label1.Text ="Standard Page Load: " + DateTime.Now.ToLongTimeString(); Label2.Text ="Standard Page Load: " + DateTime.Now.ToLongTimeString(); }protected void Button1_Click(object sender, EventArgs e) { lt.Text ="alert(document.getElementById('" + Label1.ClientID +"').value);"; Label2.Text ="Ajax Page Load: " + DateTime.Now.ToLongTimeString() + lt.Text; }

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><div> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <br /> <br /> <script language="javascript"> <asp:Literal id="lt" runat="server" /> </script> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> </form></body></html>

Try this:

protected void Button1_Click(object sender, EventArgs e)
{
string script = "alert($get('" + Label1.ClientID + "').innerText)";

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", script, true);

Label2.Text ="Ajax Page Load: " + DateTime.Now.ToLongTimeString();

}


thanks Rashid... that was helpful.


accessing javascript from code behind

Hello,

Ok, heres my scenario:

On the Client Side - I have a javascript event that populates a variable with a new value from an embeded activex control.

On the Server Side - I have a dataview that hooked to a database.

Desired Result - When the javascript event fires I need to pass the value of the changed variable to the server via a callback or something and populate the dataview based on the value received.

I know how to callback to the server on client event but My question is how do I access or pass that javascript value to the server? (Better yet does anybody know how to access a com object embeded in the browser from C#?)

I thought this would be easy but I just can't figure it out.

Thanks, Justin.

If I can gather what your saying, all you need to do is add runat="server" and an ID to whatever control you want and you can access it serverside. What most people do is add it to a hidden input, set that in javascript, and then access it via its ID in c#.

<input type="hidden" runat="server" id="theValue" />

theValue.Value = "Hello";


Create a hidden field on the page. Then from the client side JavaScript populate the hidden field with the value from the ActiveX control. You can then refer the value of the hidden field in the .CS file.

Let me know if you need a code example.


Thanks for the replies.


Hi...

I am using a code as below but getng an error saying hdClientUTC undefined evnthough i declared tht can any one help me. I want to get the content of offset variable in my C# code.

HtmlInputHidden hdClientUTC;

ClientScript.RegisterStartupScript(this.GetType(),"MyScript","<Script for=" +"window" +" " +"event=" +"onload" +" " +"language=" +"jscript" +">" +" var now = new Date(); var offset = now.getTimezoneOffset(); alert(offset); hdClientUTC.Value = offset; document.Form1.submit(); " +"</Script>");

Accessing controls within in an Accordion ?

Hi all,

A simple problem : I can't get access by to a control placed within a Accodion pane.

More exactly, code completion in C# is giving me the name of the component (a Literal within a Pane of an Accordion) but at runtime I get an error (null reference), the Literal is not created, whatever the moment I try to catch it.

I tried a lot of different events (of the Page, of the Accordion..), I tried to use FindControl instead of direct acces to "Literal1", all failled..

I need to dynamicaly load the content of a Pane when the Page is rendered. I generaly use a Literal since I inject html code. What is the trick to access an asp control within an Accordion Pane ??

Thanks in advance for your help.

The easiest solution would be using FindControl(). Have a look at the following example:

.aspx page:

<%

@.PageLanguage="C#"%>

<%

@.RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit"TagPrefix="ajaxCT" %>

<!

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">

<

headid="Head1"runat="server"/>

<

body><formid="form1"runat="server"><asp:ScriptManagerID="smr"runat="server"></asp:ScriptManager>

<

ajaxCT:AccordionID="Accordion1"runat="server"><Panes><ajaxCT:AccordionPaneID="AccordionPane1"runat="server"><Header>Header-1</Header><Content>

<asp:GridViewID="GridView1"runat="server"></asp:GridView>

</Content></ajaxCT:AccordionPane></Panes>

</

ajaxCT:Accordion></form>

</

body>

</

html>

.cs file(code behind):

protectedvoid Page_Load(object sender,EventArgs e)

{

GridView myGridView = AccordionPane1.FindControl("GridView1")asGridView;

myGridView.DataSource= myDataTable; // myDataTable is defined somewhere else

myGridView.DataBind();

}

Hopefully this would be helpful.

Cheers.

Sazzad

Monday, March 26, 2012

Access Objects in diferent webControl

Hello,

I've been wondering if it's possible to manipulate objects from a webcontrol inside the vb code of diferent webcontrol.

Mainly what I'm trying to archieve is the usage of AJAX and its async triggers. I have a list of items, each with a button associated, all created at runtime, and I want to make them triggers of a shopping bag located inside an updatePanel on a different webcontrol that is already loaded inside the same page.

Is this possible? If so, how?

Thx

You can add you controls to the UpdatePanel's trigger collection which will cause these to update the UpdatePanel, seehttp://morewally.com/cs/blogs/wallym/archive/2006/09/20/359.aspx

-Damien


Hi,

Here:Trigger PostBack to an UpdatePanel from a separate UserControl?

A example for you to do this.

Best Regards,

Access denied bug

Hi. Just letting yuo know that this code on the line 5959 of one of MS AJAX files, in getLocation func, yield "Access denied" bugs in cross-domain scenarios. Or http with https iframe scenario in our case. top is obviously inaccessible...

var w = element.ownerDocument.parentWindow;
var offsetL = w.screenLeft - top.screenLeft - top.document.documentElement.scrollLeft + 2;
var offsetT = w.screenTop - top.screenTop - top.document.documentElement.scrollTop + 2;

See this: http://weblogs.asp.net/bleroy/archive/2007/01/31/how-to-work-around-the-quot-access-denied-quot-cross-domain-frame-issue-in-asp-net-ajax-1-0.aspx

Saturday, March 24, 2012

A Simple Drag & Drop problem

Hello, I'm learning Atlas and I wrote the following code for a drag and drop issue. It doesn't work so, help me to correct this code.

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="FirstDragAndDrop.aspx.cs" Inherits="FirstDragDrop" %>"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >First Drag and Drop"server"> "text/css" rel="Stylesheet" href="FirstDragAndDropSS.css" /> "form1" runat="server"> true ID="ScriptManager" runat="server"> "ScriptLibrary/AtlasCompat.js" Browser="Firefox" /> "ScriptLibrary/AtlasUIDragDrop.js" />

class="header">Drag & Drop Example

class="main">

class="up">

class="login" id=log>

class="loginHandle">Login

class="loginContent"> "Login" runat="server">

class="calendar" id=cal>

class="calendarHandle">Calendar

class="calendarContent"> "Calendar1" runat="server">

class="down" id="dropzone">

"dropCueTemplate"class="dropCue">

Thanks

"text/xml-script"> "ScriptLibrary/AtlasUI.js"/> "ScriptLibrary/AtlasControls.js"/> "ScriptLibrary/AtlasUIDragDrop.js"/> "dropzone"> "HTML" acceptedDataTypes="'HTML'" dragMode="Move" direction="Vertical"> "dropCueTemplate"/> "log"> "loginHandle" dataType="HTML"/> "cal"> "calendarHandle" dataType="HTML"/>
the source isn't posted correctly... can't help soz!
I don't know how to do for the post to be better. I'll try to do it better.

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="FirstDragAndDrop.aspx.cs" Inherits="FirstDragDrop" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>First Drag and Drop</title>
<link type="text/css" rel="Stylesheet" href="FirstDragAndDropSS.css" />
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager EnablePartialRendering=true ID="ScriptManager" runat="server">
<Scripts>
<atlas:ScriptReference Path="ScriptLibrary/AtlasCompat.js" Browser="Firefox" />
<atlas:ScriptReference Path="ScriptLibrary/AtlasUIDragDrop.js" />
</Scripts>
</atlas:ScriptManager>
<div class="header">Drag & Drop Example</div>
<div class="main">
<div class="up">
<div class="login" id=log>
<div class="loginHandle">Login</div>
<div class="loginContent">
<asp:Login ID="Login" runat="server">
</asp:Login>
</div>
</div>
<div class="calendar" id=cal>
<div class="calendarHandle">Calendar</div>
<div class="calendarContent">
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
</div>
</div>
</div>
<div class="down" id="dropzone">
<div id="dropCueTemplate" class="dropCue"></div>
</div>
</div>
<script type="text/xml-script">
<page>
<references>
<add src="ScriptLibrary/AtlasUI.js" />
<add src="ScriptLibrary/AtlasControls.js" />
<add src="ScriptLibrary/AtlasUIDragDrop.js" />
</references
<components>
<control id="dropzone">
<behaviors>
<dragDropList dataType="HTML" acceptedDataTypes="'HTML'" dragMode="Move" direction="Vertical">
<dropCueTemplate>
<template layoutElement="dropCueTemplate" />
</dropCueTemplate>
</dragDropList>
</behaviors>
</control
<!--Draggable Items--> <control id="log">
<behaviors>
<draggableListItem handle="loginHandle" dataType="HTML" />
</behaviors>
</control
<control id="cal">
<behaviors>
<draggableListItem handle="calendarHandle" dataType="HTML" />
</behaviors>
</control
</components>
</page
</script>
</form>
</body>
</html>

This looks like a sample for an older build. Here's a similar sample that works with the March CTP:

 <atlas:ScriptManagerProxy runat="server" ID="scriptManagerProxy"> <Scripts> <atlas:ScriptReference ScriptName="AtlasUIDragDrop" /> </Scripts> </atlas:ScriptManagerProxy> <div class="shim column"></div> <div class="page" id="links"> <div id="content"> <h2>Atlas.UI.DragDrop</h2> <p>This file contains script that demonstrates the drag & drop functionality.</p> <div class="dragDrop"> <div style="visibility:hidden; display:none"> <ul> <li id="dropCueTemplate" style="border-top: 1px solid #000" /> <li id="emptyHostTemplate">No draggables in this host.</li> </ul> </div> Zone 1 <ul id="zone1"> <li id="item1"> <div class="draggableFrame"> <div id="item1handle" class="handle"> <span class="handleText">Draggable 1.1</span> </div> <div id="item1data" class="draggableBody"> Draggable list item 1 from zone 1 </div> </div> </li> <li id="item2"> <div class="draggableFrame"> <div id="item2handle" class="handle"> <span class="handleText">Draggable login</span> </div> <div id="item2data" class="draggableBody"> <asp:Login ID="Login1" runat="server" /> </div> </div> </li> </ul> Zone 2 <ul id="zone2"> <li id="item3"> <div class="draggableFrame"> <div id="item3handle" class="handle"> <span class="handleText">Draggable 2.1</span> </div> <div class="draggableBody"> Draggable list item 1 from zone 2 </div> </div> </li> <li id="item4"> <div class="draggableFrame"> <div id="item4handle" class="handle"> <span class="handleText">Draggable 2.2</span> </div> <div class="draggableBody"> Draggable list item 2 from zone 2 </div> </div> </li> <li id="item5"> <div class="draggableFrame"> <div id="item5handle" class="handle"> <span class="handleText">Calendar</span> </div> <div> <asp:Calendar ID="Calendar1" runat="server" /> </div> </div> </li> </ul> </div> </div> </div> <script type="text/xml-script"> <page xmlns:script="http://schemas.microsoft.com/xml-script/2005"> <components> <control id="zone1"> <behaviors> <dragDropList dataType="HTML" dragMode="Move" acceptedDataTypes="'HTML'"> <dropCueTemplate> <template layoutElement="dropCueTemplate" /> </dropCueTemplate> <emptyTemplate> <template layoutElement="emptyHostTemplate" /> </emptyTemplate> </dragDropList> </behaviors> </control> <control id="item1"> <behaviors> <draggableListItem handle="item1handle" /> </behaviors> </control> <control id="item2"> <behaviors> <draggableListItem handle="item2handle" /> </behaviors> </control> <control id="zone2"> <behaviors> <dragDropList dataType="HTML" dragMode="Move" acceptedDataTypes="'HTML'"> <dropCueTemplate> <template layoutElement="dropCueTemplate" /> </dropCueTemplate> <emptyTemplate> <template layoutElement="emptyHostTemplate" /> </emptyTemplate> </dragDropList> </behaviors> </control> <control id="item3"> <behaviors> <draggableListItem handle="item3handle" /> </behaviors> </control> <control id="item4"> <behaviors> <draggableListItem handle="item4handle" /> </behaviors> </control> <control id="item5"> <behaviors> <draggableListItem handle="item5handle" /> </behaviors> </control> </components> </page> </script>

Is it possible to retain these postions for the widgets when the user logs in for the second time.

manjunath