locked
Implementing DataBindingHandler in Custom User Control RRS feed

  • Question

  • User425811032 posted

    Hello

    I have a problem where I need to add a DataBindingHandler to my User Control so that the data bound to it will remain through paging in a gridview where this control is added inside and bound through DataBinder.Eval.
    If you rather just test and insert then I have uploaded the project here: https://ufile.io/pspmg
    First I have my default.aspx, where I simply refer to my WebUserControlLister.ascx:

    <%@ Register Src="~/Control/WebUserControlLister.ascx" TagPrefix="uc1" TagName="WebUserControlLister" %>
    <!DOCTYPE html>
    <head runat="server">
    </head>
    <body>
            <uc1:WebUserControlLister runat="server" id="WebUserControlLister" />
    </body>
    </html>

    In the WebUserControlLister I have the following markup:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControlLister.ascx.cs" Inherits="Control_WebUserControlLister" %>
    <%@ Register Src="~/Control/WebUserControlSplash.ascx" TagPrefix="uc1" TagName="WebUserControlSplash" %>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [EventName], [EventId], [EventSecret], [JustAbit] FROM [JustATestTable]">
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" Width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" PageSize="3" >
        <Columns>
            <asp:TemplateField HeaderText="EventSecret" ShowHeader="False">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EventSecret") %>'></asp:Label>
                    <uc1:WebUserControlSplash runat="server" eventSecret='<%#DataBinder.Eval(Container.DataItem, "eventSecret") %>' ID="WebUserControlSplash1" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    (Note: for now, I had a GridView1.DataBind() on the load event on the WebUserControlLister, but that should not be required at all if I get the DataBindingHandler to work, so that it works like the Label component sat right next to it - as I want my user control to act like the text property does in the label, as when I use the paging control the data is not passed to the WebUserControlSplash, but the label works fine)

    Inside my WebUserControlSplash I have a very simple markup(test), I simply put a button that In want to set the Text property of with the value of the eventSecret.

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControlSplash.ascx.cs" Inherits="Control_WebUserControlSplash" %>
    <asp:Button ID="Button1" runat="server"/>

    As for the codebehind(c#) for the WebUserControlSplash (where I need help Implementing DataBindingHandler, so I can set the button text in design time)

    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Runtime;
    using System.Reflection;
    
    [ControlValueProperty("Text"),
        DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
        DefaultProperty("Text"),
        ParseChildren(false)]
    public partial class Control_WebUserControlSplash : System.Web.UI.UserControl
    {
        [
            Bindable(true),
            DefaultValue(""),
            PersistenceMode(PersistenceMode.InnerDefaultProperty)
            ]
        public virtual string eventSecret
        {
            get
            {
                object o = ViewState["eventSecret"];
                return ((o == null) ? String.Empty : (string)o);
            }
            set
            {
                ViewState["eventSecret"] = value;
            }
        }
    
        public override void DataBind()
        {
            if (!String.IsNullOrEmpty(eventSecret))
            {
                Button1.Text = eventSecret;
            }
            else
            {
                Button1.Text = "IsNullOrEmpty, Error Here!";
            }
            base.DataBind();
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!String.IsNullOrEmpty(eventSecret))
            //{
            //    Button1.Text = eventSecret;
            //}
            //else
            //{
            //    Button1.Text = "IsNullOrEmpty, Error Here!";
            //}
        }

    Sadly above does not work, if you can see what I did wrong then please correct me, or you can try and see what is wrong by quickly opening the linked to project in the top.

    I have been stuck at this for days, tried multiple things but nothing worked so far - I could not find anybody who has the same requirement as me, so I have tried putting  things together, nothing worked so far.

    Thank you in advance.

    Tuesday, May 8, 2018 4:25 PM

All replies