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

Wednesday, March 28, 2012

How do I recieve a JavaScript object as a webmethod parameter?

I create a custom JavaScript object on the client side attempted to send to a webmethod and I kinda got it - but do not know how to get the date. The test method looks like this:

[WebMethod]
public string TestMethod(Object obj)
{
return "test";
}

So how can cast this in to the right object to access the data? I can see the contents if I watch it but don't know how to cast it to an object I can use it

Assuming your javascript object is something like the following

{index:-1,fileId:-1,fileExt:\"xls\",issueDate:\"1 Jan 2007\"}

you can create a structure in your serverside code to handle it

Public Structure Doc
Public index As Integer
Public fileId As Long
Public fileExt As String
Public issueDate As Date
End Structure

<WebMethod()> _
Public Function SaveDocs(ByVal docs As Doc()) As Doc()


One more question - if you don't mind:

If my JSON contains an Array - Can you tell how to make that happen? :


My BO:

 [Serializable]public class Supplier : Base {public string Name {get;set; }public string Phone {get;set; }public string Fax {get;set; }public string Website {get;set; }public string Status {get;set; }public Site Site {get;set; }public DateTime Since {get;set; }public Array Contacts {get;set; }public Supplier() {this.Site=new Site();this.Contacts =new Member[0]; } }

Member is also another Object.

My JSON looks something like:

Supplier = {"ID":-1,"Name":"","Phone":"","Website":"","Site":{"Address1":"","Address2":"","City":"","State":{"Name":"","Abbreviation":"","ID":-1},"ZipCode":""},"Contacts":[]};

Its the Contacts that is tripping me up.


Asp.net ajax does the trick for us, I mean the serialization/deserialization.


But for some reason it not filling all the objects - I check the JS object prior to calling the webmethod and the fields are populated but when it casted in the server object - some properties are not filled. Also, my other question is how to pass the array object that is a property of the JS object.


My mistake - I was expecting to receive the parent object and passing the child.

Monday, March 26, 2012

How do I extend an Atlas extender?

Let's say I create my own Atlas Control Extender. For example, I create an extender called MyHoverExtender. But instead of extending Control or WebControl, I want to extend the HoverMenuExtender. But when I change it to use that, I get this error:

error BC32044: Type argument 'SearchResultItem.MyHoverExtenderProperties' does not inherit from or implement the constraint type 'Microsoft.AtlasControlExtender.TargetControlPropertiesBase(Of AtlasControlToolkit.HoverMenuExtender)'.

Can anyone help me with this error?

Thanks

The extender class and the properties class have to travel in pairs. So if you derive from the extender, you need to also do so from the properties class. I think if you just have your MyHoverExtenderProperties class inherit from HoverMenuProperties you should be OK.



So here's what I've got right now:

-----------------

MyHoverExtenderDesigner.vb:

Class MyHoverExtenderDesigner
Inherits Microsoft.AtlasControlExtender.Design.ExtenderControlBaseDesigner(Of MyHoverExtenderProperties, AtlasControlToolkit.HoverMenuExtender)
End Class

Produces this error:

error BC32044: Type argument 'SearchResultItem.MyHoverExtenderProperties' does not inherit from or implement the constraint type 'Microsoft.AtlasControlExtender.TargetControlPropertiesBase(Of AtlasControlToolkit.HoverMenuExtender)'.

-----------------

MyHoverExtenderExtender.vb:

<Assembly: System.Web.UI.WebResource("SearchResultItem.MyHoverExtenderBehavior.js", "text/javascript")>

<System.ComponentModel.Designer(GetType(MyHoverExtenderDesigner))> _
<Microsoft.AtlasControlExtender.ClientScriptResource("MyHoverExtender", "MyHoverExtenderBehavior", "SearchResultItem.MyHoverExtenderBehavior.js")> _
Public Class MyHoverExtenderExtender
Inherits Microsoft.AtlasControlExtender.ExtenderControlBase(Of MyHoverExtenderProperties, AtlasControlToolkit.HoverMenuExtender)
End Class

Produces this error:

error BC32044: Type argument 'SearchResultItem.MyHoverExtenderProperties' does not inherit from or implement the constraint type 'Microsoft.AtlasControlExtender.TargetControlPropertiesBase(Of AtlasControlToolkit.HoverMenuExtender)'.

-----------------

MyHoverExtenderProperties.vb

<System.ComponentModel.DefaultProperty("MyProperty")> _
Public Class MyHoverExtenderProperties
Inherits Microsoft.AtlasControlExtender.TargetControlPropertiesBase(Of AtlasControlToolkit.HoverMenuProperties)

Public Property MyProperty() As String
Get
Return GetPropertyStringValue("MyProperty")
End Get
Set(ByVal value As String)
SetPropertyStringValue("MyProperty", value)
End Set
End Property

End Class

This file builds without any errors as far as I can tell.


Oh, sorry, just find my problem.

The MyHoverExtenderProperties class was declared thusly:

Public Class MyHoverExtenderProperties
Inherits Microsoft.AtlasControlExtender.TargetControlPropertiesBase(Of AtlasControlToolkit.HoverMenuProperties)

But it should be declared:

Public Class MyHoverExtenderProperties
Inherits Microsoft.AtlasControlExtender.TargetControlPropertiesBase(Of AtlasControlToolkit.HoverMenuExtender)


That's closer. Does that work?

I think you actually want:

public class MyHoverExtender inherits HoverMenuExtender

public class MyHoverExtenderProperties inherits HoverMenuProperties

What you have above says that you'll be extending controls of type HoverMenuExtender, which I don't think you want.


Yeah, I think you're right. I'm in the process of confusing myself today between wanting to extend my own custom control using a modified version of the hover extender. I think I've almost got my head around it all.

:) Okay if you want more help confusing yourself, just let us know and we'll be glad to lend a hand.

Seriously, our refresh should help with this - I'm concerned you may run into some issues getting the right scripts to load as is. I haven't tried what you're doing so do let us know.

How do I dynamically create updatepanel and then contenttemplate linkbutton and triggers a

I need to be able to create multiple updatepanels which have a link button in them. I have no idea how many updatepanels I'm going to have which have link buttons as the data coming back is from a database. Right now I'm taking the data from a database and putting that into a datalist.

It all works great until I wanted to add a updatepanel to it; then it said the updatepanel cannot be used in that context. So, if it cannot be as simple as what I tried below, how do I dynamically create an updatepanel for each row of data returned from the database?

Each row in the database has a unique ID so the ID of the linkbutton and the asyncpostbacktrigger controlid could be assigned that unique ID.

This works fine without adding updatepanel:
<asp:ScriptManager ID="ajaxClientComponentMgr" EnablePartialRendering="true" runat="server">
<Services>
<asp:ServiceReference Path="~/AjaxWebService.asmx" InlineScript="true" />
</Services>
<Scripts>
<asp:ScriptReference Path="~/Javascript/ajaxJScript.js" />
</Scripts>
</asp:ScriptManager>

<asp:DataList ID="DataSource_Stories_Display" runat="server">
<ItemTemplate>
<asp:Table CssClass="stories_table_block" runat="server">
<asp:TableRow runat="server">
<asp:TableCell CssClass="stories_right_content" runat="server">
<span class="stories_content_header"><%#DataBinder.Eval(Container.DataItem, "Title Name:")%></span><br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:DataList>

This complains of being "not in this context" after adding updatepanel:
(Note: unique_id_for_this_link added just to show that each iteration would have a unique number at that location)

<asp:ScriptManager ID="ajaxClientComponentMgr" EnablePartialRendering="true" runat="server">
<Services>
<asp:ServiceReference Path="~/AjaxWebService.asmx" InlineScript="true" />
</Services>
<Scripts>
<asp:ScriptReference Path="~/Javascript/ajaxJScript.js" />
</Scripts>
</asp:ScriptManager>

<asp:DataList ID="DataSource_Stories_Display" runat="server">
<ItemTemplate>
<asp:Table CssClass="stories_table_block" runat="server">
<asp:TableRow runat="server">
<asp:TableCell CssClass="stories_right_content" runat="server">
<span class="stories_content_header"><%#DataBinder.Eval(Container.DataItem, "Title Name:")%></span><br />

<asp:UpdatePanel ID="something_unique_id_for_this_panel" ChildrenAsTriggers="False" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:LinkButton ID="unique_id_for_this_link" OnClientClick="paneDataRequest();return false;" CssClass="story_link" runat="server">Click Me</asp:LinkButton>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="unique_id_for_this_link" />
</Triggers>
</asp:UpdatePanel>

</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:DataList>

UPDATE: Okay...Swapping where UpdatePanel was located fixed part of the issue.

UpdatePanel, if it is outside of Datalist will work as follows:

<asp:UpdatePanel ChildrenAsTriggers="False" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:DataList ID="DataSource_Stories_Display" runat="server">
<ItemTemplate>
<asp:Table CssClass="stories_table_block" runat="server">
<asp:TableRow runat="server">
<asp:TableCell CssClass="stories_right_content" runat="server">
<span class="stories_content_header"><%#DataBinder.Eval(Container.DataItem, "Title Name:")%></span><br />
<asp:LinkButton OnClientClick="paneDataRequest();return false;" CssClass="story_link" runat="server">Click Me</asp:LinkButton>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>

New problem discovered!

If I try to assign an ID to the asp:LinkButton (a unique value for each row of database data) I just end up getting all types of errors:

<asp:LinkButton id="some_unique_id" OnClientClick="paneDataRequest();return false;" CssClass="story_link" runat="server">Click Me</asp:LinkButton>

If the ID is omitted, it works fine...not what I'm after.

Next Question:

How do I assign a unique ID number to LinkButton? If I cannot assign it, how can I determine the IDs which are assigned to it so that I can build the <triggers></triggers> part of the updatepanel control?


UPDATE 2:

Visual Studio .Net Beta 2, using the scenario above from second post, does not auto-generate IDs (when you view source of the generated page) for:

<asp:LinkButtonOnClientClick="paneDataRequest();return false;"CssClass="story_link"runat="server"></asp:LinkButton>

Visual Studio .Net Beta 2, using the scenario above from second post, does auto-generate name values (although not IDs) for buttons such as:

<asp:ButtonOnClientClick="paneDataRequest();return false;"Text="default"runat="server"></asp:Button>

How do I dynamically create TabPanels in codebehind?

I have tried creating TabPanels dynamically in Page_Init but they aren't showing up even though I do see them when doing View Source on the resulting page. When I add them statically with the designer, they do show up. I am using 10618 version of the toolkit.

Here is a code snippet:

protected void Page_Init(object sender, EventArgs e){ AjaxControlToolkit.TabPanel tp =new AjaxControlToolkit.TabPanel(); tp.ID ="DynamicTab1"; tp.HeaderText ="DynamicTab1" Label tabContents =new Label(); tabContents.Text = tp.HeaderText; tp.Controls.Add(tabContents); ExistingTabContainer.Tabs.Add(tp);}

Hi,

I'd do this dynamic tab panel creation this way (it also works in the Page_Load event handler):

protected override void OnInit(EventArgs e){base.OnInit(e);// Create the tab panel's content container Control tabContent =new Control(); TabPanel tab =new TabPanel(); tab.ID ="tabPanel1"; tab.HeaderText ="Dynamic Panel"; Label label =new Label(); label.Text ="This is a dynamic tab panel"; tabContent.Controls.Add(label); tab.Controls.Add(tabContent);this.tabContainer1.Tabs.Add(tab);}

This pice of code works for me perfect. The tabContainer1 control is of type AjaxControlToolkit.TabContainer and is declared on the page this way:

<ajaxcc:TabContainer ID="tabContainer1" runat="server"></ajaxcc:TabContainer>

Kind regards,

sbogus.



sbogus, I got it to work using your sample code. I really appreciate your help.

Cheers!

Ron

How do I create static functions in JavaScript?

According to the docs online, the Error class has static functions

I can do the following
var err = Error.create(message, errorInfo);

without having to create a new object first. The documentation online does not explain how to create your own static functions using the MS approach.

Any ideas how I can create my own static functions?

Andre

try this:

var StaticClass =
{
//Static Variables
varInt : 0,
varString : '',
varArray : [],

StaticMethod1 : function()
{
//Accessing the Vars
StaticClass.varInt = 5;
},

StaticMethod2 : function()
{
//Calling another static method
StaticClass.StaticMethod1();
},
}


//Outside the Class:

StaticClass.StaticMethod1()

Hope this help.


When I use this approach, it seems that I can not register the class with ScriptManager anymore.
I have been digging around in MS Ajax code to see how MS creates static classes like the Error class.

Here is a snippet:

Error.__typeName = 'Error';
Error.__class = true;

Error.create = function Error$create(message, errorInfo) {

}

...

I tried to use this approach but it did not work.

Also I would be able to use a namespace instead of only a class name.


Yes you will be able to add it in ScriptManager, but won't be able to use registerClass. Unless you are not creating any component, behavior or control there is no need to use registerClass. To include namespace, add your name as you did for regular class and acess theclass with namespace.

How do i create autocomplete with database ?

How do i create autocomplete with database like sql words list.. pls, thanks!

Hope this helps:

http://forums.asp.net/thread/1563579.aspx

If noone else answers here, try searching forum... 100s of autocomplete questions and codes..


Have you taken a look at theCascadingDropDown Walkthrough that does something very similar? You can apply the same principles to the autocomplete extender.

What I do is create a local web service that queries the database. An example of the code for the web service is as follows:

using

System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

///

<summary>
/// Summary description for roster_list
///</summary>
[WebService(Namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
publicclassroster_list : System.Web.Services.WebService {

public roster_list () {

}

[WebMethod]
publicstring[] GetRosterListValues(string prefixText,int count)
{
SqlConnection RosterListConnection;
SqlCommand RosterListCommand;
SqlDataReader RosterListReader;
DataTable RosterListDataTable =newDataTable();

RosterListConnection =newSqlConnection();
RosterListConnection.ConnectionString =ConfigurationManager.ConnectionStrings["RosterConnectionString"].ConnectionString;
RosterListCommand =newSqlCommand();

RosterListCommand.CommandText ="SET ROWCOUNT @.Count " +
" SELECT column " +
" FROM t_table" +
" WHERE column LIKE @.PrefixText + '%') " +
" ORDER BY column " +
" SET ROWCOUNT 0";

RosterListCommand.Parameters.AddWithValue("@.PrefixText", prefixText);
RosterListCommand.Parameters.AddWithValue("@.Count", count);
RosterListCommand.CommandType =CommandType.Text;
RosterListCommand.Connection = RosterListConnection;

try
{
RosterListCommand.Connection.Open();
RosterListReader = RosterListCommand.ExecuteReader(CommandBehavior.CloseConnection);
RosterListDataTable.Load(RosterListReader);
}
catch { }
finally
{
RosterListDataTable.Dispose();
RosterListCommand.Dispose();
RosterListConnection.Dispose();
}

string[] ReturnValues =newstring[RosterListDataTable.Rows.Count];
for (int i = 0; i < RosterListDataTable.Rows.Count; i++)
ReturnValues[i] = RosterListDataTable.Rows[i][0].ToString();

return ReturnValues;
}
}

Then for my extender I have:

<cc1:AutoCompleteExtenderID="AutoCompleteExtender_Roster"runat="server"
TargetControlID="TextBox_Roster"
CompletionInterval="250"
MinimumPrefixLength="1"
ServicePath="../web_services/roster_list.asmx"
ServiceMethod="GetRosterListValues"
CompletionSetCount="15" />

How do I create a WebService with mandatory elements in the request?

I need a bit of assitance creating a WebService. Say that I have something like this -

'The objectsPublic Class productPublic nameAs StringPublic priceAs DecimalEnd ClassPublic Class orderPublic customerAs StringPublic productsAs List(Of product)End Class'The DB layerPublic orderDBPublic Function commitOrder(orderAs order)As IntegerDim orderIdAs Integer'DB code ommittedReturn orderIdEnd FunctionEnd Class'The Web Service<System.Web.Services.WebService(Namespace:="http://localhost/")> _<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ToolboxItem(False)> _Public Class order_wsInherits System.Web.Services.WebService <WebMethod()> _Public Function commitOrder(ByVal orderAs order)As Integer Dim orderDBAs New orderDB()Return orderDB.commitOrder(order)End FunctionEnd Class
The WSDL I get contains something like -

<s:element name="commitOrder"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="order" type="tns:order" /></s:sequence></s:complexType></s:element><s:complexType name="order"><s:sequence><s:element minOccurs="0" maxOccurs="1" name="customer" type="s:string" /><s:element minOccurs="0" maxOccurs="1" name="products" type="tns:ArrayOfproducts" /></s:sequence></s:complexType><s:complexType name="ArrayOfproducts"><s:sequence><s:element minOccurs="0" maxOccurs="unbounded" name="product" nillable="true" type="tns:product" /></s:sequence></s:complexType><s:complexType name="product"><s:sequence><s:element minOccurs="0" maxOccurs="1" name="name" type="s:string" /><s:element minOccurs="0" maxOccurs="1" name="price" type="s:decimal" /></s:sequence></s:complexType>

Notice that everything is minoccurs="0" and the product entry in the ArrayOfproducts is nillable. How do I make it so that specific bits are required (minoccurs="1" and/or product element not nillable)?

Thanks

Martin

You could set a web service header so they always send you those requiered fields.


I'm not sure that I understand what you mean. But, just to elaborate on what I am looking for, the WSDL should contain tags that specify minOccurs="1".


Might be better to use discrete parameters rather than sending an order object. Most people find that more straightforward to use.


How would you control mandatory / optional elements using discrete parameters?

In any case, I'm already severely restricted in my attempts to use best coding practices by the inadequacies of .Net's WebService implementation. If I can't even control mandatory elements in this scenario I'll give it a miss and look at using Java and Mule.

How do I create a popup that has a button that submits to server?

I have this in my page

<div>
<asp:ButtonID="RefreshAreasButton"runat="server"OnClick="RefreshAreasButton_Click"Text="Refresh"/>
<asp:ButtonID="DeleteAreaButton"runat="server"Text="Delete"/>
<!-- Delete popup -->
<asp:PanelID="DeletePanel"runat="server"CssClass="modalPopup"Style="display: none"Height="115px"Width="255px">
<asp:TextBoxID="TextBox1"runat="server"Text="Delete Selected Areas"></asp:TextBox>
<asp:ButtonID="DeleteOKBtn"runat="server"Text="OK"OnClick="DeleteOKBtn_Click"/>
<asp:ButtonID="DeleteCancelBtn"runat="server"Text="Cancel"/>
</asp:Panel>
<cc1:ModalPopupExtenderID="ModalPopupExtender2"
TargetControlID="DeleteAreaButton"
PopupControlID="DeletePanel"
runat="server"
BackgroundCssClass="modalBackground"
DropShadow="true"
OkControlID="DeleteOKBtn"
CancelControlID="DeleteCancelBtn"/>
</div>

I want the OK button to submit to server, but my "protectedvoid DeleteOKBtn_Click(object sender,EventArgs e)" method is never called.
What am I doing wrong?

I know you that it looks like sort of confirmation popup, but later I would like the user to enter a value in the popup, and to be able to use the value in the server - is it doable?
Am I using the right control, or can you recommend on a better way to gain that.

Thanks in advance!

Tamir

What

Remove the OkControlID="DeleteOKBtn" property, after that your button should submit back to the page.

Contrary to the documentation the OkControlID property will cause the button click to fire off thejavascript function which is defined in OnClick. The same is true for the CancelControlID property, if you do not define it the button will post back normally.

Wednesday, March 21, 2012

how can i make draggable/dockable elements on a web page

I want to create a web page where I can drag and drop content, and dock it on other parts of the page. I also want that content to remain in the most current position on the page when the page is reloaded. I want to do something along the lines of what they did atPageflakes but I have no clue how to implement it. Can anyone offer suggestions? I just need some pointers as how I should approach this.

Thanks!

I have discovered the wonderful world of webparts, which answers my question here. Check outthis article if you want to learn to create these kinds of sites too.