Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

Wednesday, March 28, 2012

Transforming plain ID to adorned ID

I must be missing something obvious here. Suppose I create an ASP.NET Web User Control, and I use a ModalPopupExtender inside it. In the ascx, I set various properties of the extender to refer to controls within the Web User Control, such as PopupDragHandleControlID="TitleText". At runtime, the control with ID "TitleText" gets a more complicated ID on the rendered page (call it an 'adorned' ID, not sure what the offical term is), such as "ctl00_ContentPlaceHolder1_MyCtrl1_TitleText". The behaviour javascript for the extender does things like -

this._dragHandleElement = $get(this._PopupDragHandleControlID);

That wouldn't work with the 'plain' ID, in fact by adding various alert() boxes I can see that _PopupDragHandleControlID has been transformed - somewhere - to the 'adorned' version. I've written an extender that declares and uses ID properties in just the same way, but they don't get transformed from the 'plain' version at runtime. So, I either have to use ugly workarounds like setting that ID programmatically from server-side code (e.g. MyExtender1.BlahID = MyBlahCtrl.ClientID), or ... figure out how to get that transformation to happen automatically like it seems to for ModalPopupExtender. Can anyone point me in the right direction?

(Note, I'm not talking about the TargetControlID property, so ResolveTargetControlID wouldn't come into it, and I'm talking about an extender that's right next to the controls in question.)

Thanks in advance for any leads.

Kevin.

All extender properties that reference ID's are decorated with theIDReferencePropertyAttribute. It helps converts server side ids into client versions so that the behavior can resolve them easily. If you look at the ModalPopupExtender.cs file, the PopupDragHandleControlID has that attribute and so so any other properties that are used to get control IDs.


Do you have any references that confirm that's so and detail where it occurs? The reasons I ask are 1. My extender's properties that ref IDs already have that attribute, and no such conversion occurs, and 2. The documentation for that attribute doesn't mention anything like that, merely that it can be used by designers to help out by giving a list of control IDs at design time - no mention of anything happening at runtime. But if that attribute is the answer and if you have more information on it, please do say so I can figure out why it isn't working for me.

Or does anyone else have details on how the conversion occurs for PopupDragHandleControlID et al?

Thanks in advance for any help

Kevin.


It happens in ExtenderBaseDesignerHelpers.cs file. It is part of the Toolkit framework. If you are building on top of ExtenderControlBase this is taken care of automatically.

Should have included these links earlier

http://ajax.asp.net/ajaxtoolkit/Walkthrough/CreatingNewExtender.aspx

http://ajax.asp.net/ajaxtoolkit/Walkthrough/ExtenderClasses.aspx


Aha. In fact, I had a hand-rolled extender built with ref to various online docs rather than one done as per that walkthrough, which when followed showed up I'd not got the ECB derivation stuff right. In the interim, I'd done some tracing through the ACT code and you're quite right, that attribute is used as a signal for the base classes to apply some transformation before serialising. Funny the docs don't mention that. Anyway, thanks for the pointers - it's running ok now.

Kevin.

TreeView and UpdatePanel

I create a web form to display directory structure of the disks of the server. I put a treeview on the form and fill it with the drive and directory names. Everything was fine until I put a UpdatePanel (and ScriptManager ofcourse) on the form and move the TreeView into it. Now, I have a problem with the first node of the treeview (C:\). When I expand that node I see all the directories in the root of the C: drive. But when I expand any directory, all the nodes collapses. Than, other root nodes (D:, E: etc) works properly, but C: is not. What is the thing I miss? I use Visual Studio 2005 and AJAX January CTP. My code is as follows;

protectedvoid Page_Load(object sender,EventArgs e)

{

string[] drives;int i;TreeNode node, initialChildNode;DriveInfo driveInfo;if (!Page.IsPostBack)

{

tvFileSystem.Nodes.Clear();

drives =

Environment.GetLogicalDrives();for (i = 0; i < drives.Length; i++)

{

node =

newTreeNode();

driveInfo =

newDriveInfo(drives[i]);try

{

node.Text = driveInfo.VolumeLabel +

"(" + drives[i] +")";

}

catch

{

node.Text = driveInfo.DriveType +

"(" + drives[i] +")";

}

node.Value = drives[i];

initialChildNode =

newTreeNode();

initialChildNode.Text =

".";

initialChildNode.Value =

".";

node.ChildNodes.Add(initialChildNode);

tvFileSystem.Nodes.Add(node);

node.Collapse();

}

tvFileSystem.CollapseAll();

}

}

protectedvoid tvFileSystem_TreeNodeExpanded(object sender,TreeNodeEventArgs e)

{

TreeNode node = e.Node;TreeNode initialChildNode, newNode, tempNode;DirectoryInfo dirInfo;DirectoryInfo[] subDirInfo;string path ="";string[] subDirs;int i;string dirName, js;if (node.Value ==".")return;

tempNode = node;

while (tempNode !=null)

{

path = tempNode.Value +

"\\" + path;

tempNode = tempNode.Parent;

}

try

{

dirInfo =

newDirectoryInfo(path);

subDirInfo = dirInfo.GetDirectories();

}

catch (Exception e1)

{

js =

"<script>alert('" + e1.Message.Replace("\r","").Replace("\n","") +"');</script>";

Page.ClientScript.RegisterStartupScript(

typeof(string),"expandError", js);return;

}

node.ChildNodes.Clear();

for (i = 0; i < subDirInfo.Length; i++)

{

newNode =

newTreeNode();

dirName = subDirInfo[i].Name;

newNode.Text = dirName;

newNode.Value = dirName;

try

{

subDirs =

Directory.GetDirectories(subDirInfo[i].FullName);if (subDirs.Length > 0)

{

initialChildNode =

newTreeNode();

initialChildNode.Text =

".";

initialChildNode.Value =

".";

newNode.ChildNodes.Add(initialChildNode);

}

node.ChildNodes.Add(newNode);

}

catch { };

}

}

And the aspx file :

<%

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

<!

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>Untitled Page</title>

</

head>

<

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

</div><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate> <asp:TreeViewID="tvFileSystem"runat="server"OnTreeNodeExpanded="tvFileSystem_TreeNodeExpanded"Style="position: static"><SelectedNodeStyleBackColor="NavajoWhite"/><NodeStyleForeColor="Black"/></asp:TreeView></ContentTemplate></asp:UpdatePanel></form>

</

body>

</

html>

From what I have read, Treeviews and Menus do not work inside UpdatePanels. I did, however, get the menu to work without posting back by keeping the menu out of the UpdatePanel, but calling the appropriate menu event from the updatepanel.

<asp:UpdatePanel runat="Server" id="upSomeUpdatePanel" UpdateMode="Conditional">
<contentTemplate>
</contentTemplate>
<Triggers>
<AutoSyncPostBackTrigger ControlID="TreeViewID" Event="treevieweventname" />
</triggers>
</asp:UpdatePanel>

Don't copy and paste my code above because I did it all by memory and some of the control parameters might have slightly different names, but I hope you get the drift. The "treevieweventname" would be the name of the event you want to do without posting back - like "TreeNodeExpanded".

Hope this helps.


I have a treeview that works fine inside of an update panel.


What I'm doing differently is I'm not using the Expanded event,I'm using the TreeNodePopulate event and setting each node thathas children's "PopulateOnDemand" property to true. MyTreeView is inside an update panel and it expands, collapses,dynamically populates, etc. just fine.


By the way, I created a thread in this forum with a working example of a TreeView in an update panel using the technique I mentioned. I used the FileSystem since it's easy and doesn't requrie a database, it isn't exactly like yours but it works. It should show up soon upon moderation. Let me know if that helps!


Thank you aaron,

Your example is very helpful for me. But, what if I want user to select a node. I need to hold the selected node value, for example in a HiddenField. I modified your example by removing "Selected.Action = TreeNodeSelectAction.None" lines. And I add SelectedNodeChanged Event which includes following code:

hdSelectedPath.Value = tvFiles.SelectedNode.Value;

Now, it works except some thing. If I selects a node, then expands another node and select another node, the selected nodes changes in a wrong way. I mean, any other node is selected which I did not select.

What is the point I miss?

Thanks.


Thank you aaron,

Your example is very helpful for me. But, what if I want user to select a node. I need to hold the selected node value, for example in a HiddenField. I modified your example by removing "Selected.Action = TreeNodeSelectAction.None" lines. And I add SelectedNodeChanged Event which includes following code:

hdSelectedPath.Value = tvFiles.SelectedNode.Value;

Now, it works except some thing. If I selects a node, then expands another node and select another node, the selected nodes changes in a wrong way. I mean, any other node is selected which I did not select.

What is the point I miss?

Thanks.

Saturday, March 24, 2012

Triggers

Hi to all.

So, I create buttons in a usercontrol after it i want to create triggers on this buttons in page. Is it possible?

I tried to do something like this on page

private

void FindButton(Control control)

{

foreach (Control phin control.Controls)

{

if (ph.Controls.Count > 0)

FindButton(ph);

if (phisCMSLinkButton)

{

ControlEventTrigger cet =newControlEventTrigger();

cet.ControlID = ph.ID;

cet.EventName =

"Click";

((

UpdatePanel)this.FindControl("UPContent")).Triggers.Add(cet);

}

}

}

I got an error

The ControlID property of the trigger must reference a valid control.

Exception Details:System.InvalidOperationException: The ControlID property of the trigger must reference a valid control.

Can anyone help me? :)

I'm getting the same error when trying to set my triggers to buttons inside of a Gridview. I'm still looking for the answer, but thought I'd post here in case someone came across and knew the answer for both of us.


No one knows why does it happens ?
Did anyone find out how to reference a gridview button in atlas?

for gridview or datagird : you just put an update panel around the grid markup code, hence you don't need to use Triggers to trap event. code like this:

<

atlas:UpdatePanelID="upd1"runat="server"><ContentTemplate>

<asp:GridView ...>...</asp:GridView>

</

ContentTemplate></atlas:UpdatePanel>

Ok, that all. just format your grid as you want, the update panel around the grid will help you control the event from button, hyperlink button... inside the grid and, of course, you don't need to use Triggers here.

Extending idea from those above, if you put the update panel right after the form tag, and so you don't need to trigger any events. Like this:

<form runat="server">
<atlas:UpdatePanel....>
<ContentTemplate>
...your markup code ...
</ContentTemplate>
</UpdatePanel>
</form>


tuantran:

for gridview or datagird : you just put an update panel around the grid markup code, hence you don't need to use Triggers to trap event. code like this:

<

atlas:UpdatePanelID="upd1"runat="server"><ContentTemplate>

<asp:GridView ...>...</asp:GridView>

</

ContentTemplate></atlas:UpdatePanel>

Ok, that all. just format your grid as you want, the update panel around the grid will help you control the event from button, hyperlink button... inside the grid and, of course, you don't need to use Triggers here.

Extending idea from those above, if you put the update panel right after the form tag, and so you don't need to trigger any events. Like this:

<form runat="server">
<atlas:UpdatePanel....>
<ContentTemplate>
...your markup code ...
</ContentTemplate>
</UpdatePanel>
</form>

What if you have more than one update panel? How do I reference the GridView from a separate update panel?


Please copy and paste here the mark-up code of your .aspx in that you have some seperate update panel and brieftly explain your request. I will try to find a correct solution if I can.

Regards

Wednesday, March 21, 2012

Trouble creating ReorderList in codebehind

All,

I am trying to create a ReorderList in the codebehind for a page and am having a rediculous amount of trouble. When I set AllowReorder to 'false' the page displays what it should, but when i set AllowReorder to 'true' I get "Object reference not set to an instance of an object. " on the line where I am doing the databinding. I am using an SqlDataAdapter which implements both SelectCommand and UpdateCommand

This is my first time trying to set one of these up, so i think there is a good chance that I am missing something simple. I am copying and pasting in my code if that helps.

---begin code-----

1using System;2using System.Data;3using System.Configuration;4using System.Web;5using System.Web.Security;6using System.Web.UI;7using System.Web.UI.WebControls;8using System.Web.UI.WebControls.WebParts;9using System.Web.UI.HtmlControls;10using AjaxControlToolkit;11using System.Text;12using System.Data.SqlClient;1314public partialclass _Default : System.Web.UI.Page15{16protected void Page_Load(object sender, EventArgs e)17 {18 SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["GLISurveyGenerator_3"].ConnectionString);19 SqlDataAdapter dataadapter =new SqlDataAdapter();2021 SqlCommand selectcommand =new SqlCommand();22 selectcommand.CommandText="spGetPagesForSurvey";23 selectcommand.CommandType = CommandType.StoredProcedure;24 selectcommand.Parameters.Add("@dotnet.itags.org.SurveyID", SqlDbType.Int, 32).Value = 1;25 selectcommand.Connection = conn;2627 SqlCommand updatecommand =new SqlCommand();28 updatecommand.CommandText ="spUpdatePagesForSurvey";29 updatecommand.CommandType = CommandType.StoredProcedure;30 updatecommand.Parameters.Add("@dotnet.itags.org.SurveyPageID", SqlDbType.Int, 32);31 updatecommand.Parameters.Add("@dotnet.itags.org.SurveyPageNumber", SqlDbType.Int, 32);32 updatecommand.Connection = conn;3334 dataadapter.SelectCommand = selectcommand;35 dataadapter.UpdateCommand = updatecommand;3637 DataSet ds1 =new DataSet();3839 dataadapter.Fill(ds1);4041 ReorderList reorderlist =new ReorderList();42 reorderlist.ID ="RL1";43 reorderlist.AllowReorder =true;44 reorderlist.PostBackOnReorder =true;45 reorderlist.EnableViewState =false;46 reorderlist.ShowInsertItem =false;47 reorderlist.ItemTemplate =new pagereorderitemtemplate();48 reorderlist.ReorderTemplate =new pagereorderreorderitemtemplate();49 reorderlist.DragHandleTemplate =new pagereorderhandletemplate();50 reorderlist.EmptyListTemplate =new pagereorderemptytemplate();51 reorderlist.DataKeyField ="SurveyPageID";52 reorderlist.SortOrderField ="SurveyPageNumber";53 reorderlist.ShowInsertItem =false;5455 reorderlist.DataSource = ds1;56 reorderlist.DataBind();575859 contentholder.Controls.Add(reorderlist);60 }6162void reorderlist_UpdateCommand(object sender, ReorderListCommandEventArgs e)63 {64throw new Exception("The method or operation is not implemented.");65 }6667private class pagereorderitemtemplate : ITemplate68 {69public void InstantiateIn(System.Web.UI.Control container)70 {71 Literal lc =new Literal();72 lc.DataBinding +=new EventHandler(lc_DataBinding);73 container.Controls.Add(lc);74 }7576void lc_DataBinding(object sender, EventArgs e)77 {78 Literal lc;79 lc = (Literal)sender;80 ReorderListItem item = (ReorderListItem)lc.NamingContainer;81string dataitem = DataBinder.Eval(item.DataItem,"SurveyPageTitle").ToString();82 lc.Text = dataitem;83 }84 }8586private class pagereorderreorderitemtemplate : ITemplate87 {88public void InstantiateIn(System.Web.UI.Control container)89 {90 Literal lc =new Literal();91 lc.Text ="test2";92 container.Controls.Add(lc);93 }94 }9596private class pagereorderhandletemplate : ITemplate97 {98public void InstantiateIn(System.Web.UI.Control container)99 {100 Literal lc =new Literal();101 lc.Text ="| |";102 container.Controls.Add(lc);103 }104 }105106private class pagereorderemptytemplate : ITemplate107 {108public void InstantiateIn(System.Web.UI.Control container)109 {110 Literal lc =new Literal();111 lc.Text ="empty";112 container.Controls.Add(lc);113 }114 }115}

--end code--

I know most of the templates don't contain all that they should, I just wanted to put something in them so they at least existed. If anyone has any suggestions on how I can get around this error, I would greatly appreciate it.

-madrak

I have been able to pin down the error a little further. The reorderlist works with an sqldatasource that is created in the .aspx page using the following code

1"SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GLISurveyGenerator_3 %>"2 SelectCommand="SELECT [SurveyPageID], [SurveyPageTitle], [SurveyPageNumber] FROM [tblSurveyPages] WHERE ([SurveyID] = @.SurveyID) order by [SurveyPageNumber]" UpdateCommand="Update [tblSurveyPages] set [SurveyPageNumber] = @.SurveyPageNumber, [SurveyPageTitle] = @.SurveyPageTitle where [SurveyPageID] = @.SurveyPageID">34 "1" Name="SurveyID" Type="Int32">567 "SurveyPageID" Type="Int32">8 "SurvePageTitle" Type="Int32">9 "SurveyPageNumber" Type="Int32">1011

but does not work with the sqldatasource as created in the prior post, anyone have any ideas?


Hi,

I removed reorderlist.DataBind(); and add page.DataBind();. Now I am able to display the data, but unable to reorder the items in the list

- narendra


Hi,

I have the same problem :(

Have anyone solved it?

Trouble w/ Atlas and Custom User Control

I am trying to create a simple user control that shows a hidden div when an imagebutton is clicked. It works perfectly in a regular page, but in a user control it is conventionally posting back rather than refreshing via Atlas. I have an <atlas:ScriptManager> tag in the page for the control. Below is my code for the control:

<%@dotnet.itags.org. Control Language="C#" AutoEventWireup="true" CodeFile="RatingsBar.ascx.cs" Inherits="UserControls_RatingsBar" %>
<asp:ImageButton ID="showBarGraphButton" runat="server" ImageUrl="~/_Images/bargraph.gif"
OnClick="showBarGraphButton_Click" />

<atlas:UpdatePanel ID="updatePanel1" runat="server" RenderMode="Inline">
<Triggers>
<atlas:ControlEventTrigger ControlID="showBarGraphButton" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="barGraphPopup" CssClass="popupHide" runat="server">
<asp:Image ID="sampleImage" runat="server" ImageUrl="~/_Images/barGraphExample.gif"/>
</asp:Panel>
</ContentTemplate>
</atlas:UpdatePanel>

Below is my code for the default.aspx page:
<%@dotnet.itags.org. Page Title="Hi There" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="_Default" %><%@dotnet.itags.org. Register TagPrefix="uc" TagName="Karmevent" Src="~/_UserControls/Karmevent.ascx" %><%@dotnet.itags.org. Register TagName="RatingsBar" TagPrefix="uc" Src="~/_UserControls/RatingsBar.ascx" %><!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>Untitled Page</title><link href="_Stylesheets/StyleSheet.css" rel="stylesheet" type="text/css" /><atlas:ScriptManager ID="ScriptManager" runat="server" /></head><body><form id="form1" runat="server"><uc:RatingsBar ID="rb1" runat="server" /></form></body></html>
Nevermind, everyone. I re-did the code from scratch and it works. I still have no idea what I did wrong, but it's working now.

trouble with dynamic tabs

Hi I am having two issues with trying to create dynamic tabs with the AjaxControlToolkit Tabs Control. First when I click the button1 to create the tab it creates 2 tabs and if I hit the button again I get the error "Specified argument was out of the range of valid values"

My aspx is

<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" EnableEventValidation="false" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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" />
<script type="text/javascript">

</script>
<div>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Tab" /><br />
<br />
<br />
<br />
<ajaxToolkit:tabcontainer id="TabContainer1" runat="server" activetabindex="0">
</ajaxToolkit:tabcontainer></div>
</form>
</body>
</html>


My vb code is:

PartialClass _DefaultInherits System.Web.UI.PageProtected Sub Button1_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Button1.ClickDim tabAs AjaxControlToolkit.TabPanel =New AjaxControlToolkit.TabPanel()Dim frame1As System.Web.UI.HtmlControls.HtmlGenericControl =New System.Web.UI.HtmlControls.HtmlGenericControl("iframe") frame1.Attributes("src") ="http://search.msn.com" frame1.Attributes("frameborder") ="0" frame1.Attributes("width") ="800" frame1.Attributes("height") ="600" frame1.Attributes("scrolling") ="auto" tab.Controls.Add(frame1) tab.Width ="800" tab.Height ="600" tab.HeaderText ="Tab " TabContainer1.Tabs.Add(tab) TabContainer1.ActiveTab = tabEnd SubEnd Class
Any help would be greatly appreciated.

Hi,

This is a common problem when working with dynamic controls.

The problem is that dynamic controls don't exist on the second request before the Button1_Click method fires. You need to recreate it in Page_Init rather than Button_Click.

Please refer to this documentation for more information:

1. Why do I have to recreate dynamic controls every time? /Why dynamic controls are disappeared on PostBack?

Whenever a request comes, a new instance of the page that isbeing requested is created to serve the request even it's a PostBack. Allcontrols on the page are reinitialized, and there state can be restored fromthe ViewState in a later phase.

The dynamic controls have to be recreated again and added tothe control hierarchy. Otherwise, they won't exist in the page.

Please be careful with when to create dynamic controls. Inorder to keep their state, they have to be created before the LoadViewStatephase. Page_Init as well as Page_Load methods are options available.

For more information about this topic, please refer to thisarticle:

Creating Dynamic Data Entry User Interfaces[http://msdn2.microsoft.com/en-us/library/aa479330.aspx ]

Hope this helps.