Simple re-use of QueryString paremetr with BindableHyperLink

When you want to use a query parameter passed to your page you’ll need litle bit of extra coding. There are few ways to accomplish that but in simple cases I like to use data binding expression just inside NavigateUrl attribute on Hyperlik’s and friends:

<wc:BindableHyperLink ID="newItem" runat="server" Text="Add"  NavigateUrl='<%# "Details.aspx?action=new&ClientId=" + Request.QueryString["ClientId"] %>' />

Plain Hyperlink control does not dupport data binding expression, so I decided to write a adapter class adding binding support to Hyperlink (full code source attached below). Aside from usage using simple data binding expression (yet without actual data binding 🙂 ), you can use this control eg. within templates with actual data binding.

/* Copyright 2003-2007 Janusz Skonieczny
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created by: WooYek on 13:20:43 2003-01-30
 *
 * Last changes made by:
 * $Id: SqlCommon.cs 4318 2007-09-03 13:55:12Z wooyek $
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WooYek.Web.UI.WebControls {
    public class BindableHyperLink : DataBoundControl {
        private HyperLink link;

        protected override void OnLoad(EventArgs e) {
            DataBind();
            base.OnLoad(e);
        }

        protected override void CreateChildControls() {
            base.CreateChildControls();
            link = new HyperLink();
            link.Text = Text;
            link.NavigateUrl = NavigateUrl;
            Controls.Add(link);
        }

        public string NavigateUrl {
            get { return (string) ViewState["NavigateUrl"]; }
            set { ViewState["NavigateUrl"] = value; }
        }

        public string Text {
            get { return (string) ViewState["Text"]; }
            set { ViewState["Text"] = value; }
        }

        public string DataNavigateUrlFields {
            get { return (string) ViewState["DataNavigateUrlFields"]; }
            set { ViewState["DataNavigateUrlFields"] = value; }
        }

        public string DataNavigateUrlFormatString {
            get { return (string) ViewState["DataNavigateUrlFormatString"]; }
            set { ViewState["DataNavigateUrlFormatString"] = value; }
        }

        public string DataTextField {
            get { return (string) ViewState["DataTextField"]; }
            set { ViewState["DataTextField"] = value; }
        }

        public object SelectedItem {
            get { return ViewState["SelectedItem"]; }
            set { ViewState["SelectedItem"] = value; }
        }

        public string DataTextFormatString {
            get {
                string s = (string) ViewState["DataTextFormatString"];
                if (string.IsNullOrEmpty(s)) {
                    return "{0}";
                }
                return s;
            }
            set { ViewState["DataTextFormatString"] = value; }
        }

        protected override void PerformDataBinding(IEnumerable data) {
            EnsureChildControls();
            if (data == null ) {
                return;
            }
            if (!string.IsNullOrEmpty(DataSourceID)) {
                //Guard.ArgumentNotNull(data,"data");

                    foreach (object dataItem in data) {
                        object textValue = DataBinder.GetPropertyValue(dataItem, DataTextField);
                        link.Text = String.Format(DataTextFormatString, Convert.ToString(textValue));
                        string[] fields = DataNavigateUrlFields.Split(',');
                        List urlParams = new List();
                        foreach (string field in fields) {
                            string fieldValue = Convert.ToString(DataBinder.GetPropertyValue(dataItem, field));
                            urlParams.Add(fieldValue);
                        }
                        link.NavigateUrl = String.Format(DataNavigateUrlFormatString, urlParams.ToArray());
                        // interesuje nas tylko pierwszy element
                        break;
                    }
            }
        }

        protected override void OnDataBinding(EventArgs e) {
            base.OnDataBinding(e);
        }
    }
}

Leave a Reply

Back to Top