Problem to pass parameters to a Widget!!!

Topics: ASP.NET 2.0, Controls
Feb 1, 2009 at 11:46 PM
Edited Feb 1, 2009 at 11:51 PM
Dear Bloggers:

I am not a c# programmer and i am trying to develop a widget. But, sorry, i'm doing it the hard way (with notepad and using the try and error method), just looking at other widgets, just because it is going to be a very simple widget.

I am trying to dislplay a text based on two parameters, but it doesn't work.

Here is my code:
******************** widget.ascx **********************************************************
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="widgets_TestWidget_edit" %>

<center>
<script type="text/javascript">

var maxItems = '<asp:literal ID="numItems" runat="server" />';
var WidgetLang = '<asp:literal ID="TestWidgetLang" runat="server" />';

document.write("You have #" + maxItems + "# of language #" + WidgetLang + "#!!!" );

</script>
</center>
******************** widget.asc.cs **********************************************************
#region Using

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;

#endregion

public partial class widgets_TestWidget_edit : WidgetBase
{
    public override void LoadWidget()
    {
        StringDictionary settings = GetSettings();    

        LiteralControl numItems = new LiteralControl(settings["numItems"]);    
        LiteralControl Idioma = new LiteralControl(settings["TestWidgetLang"]);        
   }

    public override string Name
    {
        get { return "TestWidget"; }
    }

    public override bool IsEditable
    {
        get { return true; }
    }

}
******************** edit.ascx **********************************************************
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="edit.ascx.cs" Inherits="widgets_TestWidget_edit" %>
<%@ Reference VirtualPath="~/widgets/TestWidget/widget.ascx" %>

<label for="<%=Idioma.ClientID %>">Choose language</label><br />
<asp:DropDownList runat="server" ID="Idioma">
    <asp:ListItem Value="1" Text="Spanish"/>
    <asp:ListItem Value="2" Text="English"/>
</asp:DropDownList>
<br />
<label for="<%=numItems.ClientID %>">Number of items</label><br />
<asp:TextBox ID="numItems" runat="server" />
******************** edit.ascx.cs **********************************************************
#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

#endregion

public partial class widgets_TestWidget_edit : WidgetEditBase
{

    protected override void OnLoad(EventArgs e)
    {            
        if (!IsPostBack)
      {
            StringDictionary settings = GetSettings();        
            Idioma.SelectedValue = settings["TestWidgetLang"];
            numItems.Text = settings["numItems"];
        }
   }
    
    public override void Save()
    {
        StringDictionary settings = GetSettings();      
        settings["TestWidgetLang"] = Idioma.SelectedValue;
        settings["numItems"] = numItems.Text;
      SaveSettings(settings);
    }
}
******************************************************************************
Where is the error?.

Thanks from Spain.


Feb 2, 2009 at 2:40 AM
What you are trying to achieve here can be simply done by adding a literal control on widget.ascx. And then in Page_Load you can set text for this control.

Widget.ascx
-----------

<asp:Literal ID="MyLiteral" runat="server" />

Widget.ascx.cs
---------------
public override void LoadWidget()
    {
        StringDictionary settings = GetSettings();   
        MyLiteral.Text = "You have #" + settings["numItems"] + "# of language #" + settings["TestWidgetLang"])+ "#!!!";  
   }

Let me know if you need some specific help with implementation of your widget.
Feb 2, 2009 at 9:56 AM
Edited Feb 2, 2009 at 12:24 PM
Thanks BitnBytes, your solution is simple and elegant and, of course solves my problem perfectly.
In a few days i'll release a new widget for all of you.

Greetings from Spain!!!

P.d.: I recognize my lack of experience in this case, but a good tutorial about BE framework and widget programming should be very usefull to all of us.
Feb 4, 2009 at 5:29 PM
Edited Feb 4, 2009 at 5:29 PM
Dear BitnBytes

My widget is nearly finished but i want to send the title of the widget as a parameter, even if it is unchecked (and hidden) through the edit option.
So: How do i get the title of a widget?

Thanks from spain!!!
Coordinator
Feb 4, 2009 at 5:43 PM
I was just replying to this other thread about this same question.  In LoadWidget(), you can set the title.

public override void LoadWidget()
{
    StringDictionary settings = GetSettings();   
    MyLiteral.Text = "You have #" + settings["numItems"] + "# of language #" + settings["TestWidgetLang"])+ "#!!!";
    Title = "My custom title";
}
Feb 4, 2009 at 10:03 PM
Edited Feb 4, 2009 at 10:06 PM
Perhaps i haven't expained correctly.
When you Edit a wiget you can set it's Title and you can uncheck the Show title option to hide it.
I want to send that Title as a parameter.


Thanks from Spain!!!