Avatar billede jensrud Nybegynder
21. februar 2009 - 23:45

Hvordan fanger man input fra webcontrols ved postback

Jeg har en webside med flere custom webcontrols jeg har skrevet. Den ene er fx. en formular til at indtaste kreditkort oplysninger.
Jeg har en submit knap i bunden, men når man klikker på den og prøver at fange input fra textboxene er de alle blanke.

Er der en måde at fange alle inputs fra forskellige kontroller på en gang, i stedet for at skulle tage een kontrol af gangen og finde input via postback?


Min kode til den ene webcontrol:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;

internal class resfinder { }

namespace TCAS.WebUI.CustomComponents
{
    /// <summary>
    /// Summary description for CreditCardForm
    /// </summary>
    [DefaultProperty("CardholderNameText")]
    [ToolboxData("<{0}:CreditCardForm PaymentMethodText='Payment Options' CreditCardNoText='Credit Card Number' CardholderNameText='Cardholder Full Name' runat='server'></{0}:CreditCardForm>")]
    [ToolboxBitmap(typeof(resfinder), "CreditCardControl.CreditCardForm.bmp")]
    public class CreditCardForm : WebControl, IPostBackEventHandler, IPostBackDataHandler
    {
        #region Labels

        public virtual string PaymentMethodText
        {
            get { return ViewState["PaymentMethodText"] != null ? (string)ViewState["PaymentMethodText"] : "PaymentMethod"; }
            set { ViewState["PaymentMethodText"] = value; }
        }

        public virtual string CreditCardNoText
        {
            get { return ViewState["CreditCardNoText"] != null ? (string)ViewState["CreditCardNoText"] : "CreditCardNo"; }
            set { ViewState["CreditCardNoText"] = value; }
        }

        public virtual string CardholderNameText
        {
            get { return ViewState["CardholderNameText"] != null ? (string)ViewState["CardholderNameText"] : "CardholderName"; }
            set { ViewState["CardholderNameText"] = value; }
        }

        public virtual string ExpirationDateText
        {
            get { return ViewState["ExpirationDateText"] != null ? (string)ViewState["ExpirationDateText"] : "Expiration Date"; }
            set { ViewState["ExpirationDateText"] = value; }
        }

        public virtual string SubmitButtonText
        {
            get { return ViewState["SubmitButtonText"] != null ? (string)ViewState["SubmitButtonText"] : "Submit"; }
            set { ViewState["SubmitButtonText"] = value; }
        }

        public virtual string HeaderText
        {
            get { return ViewState["HeaderText"] != null ? (string)ViewState["HeaderText"] : "Payment"; }
            set { ViewState["HeaderText"] = value; }
        }

        #endregion

        #region Control Style

        protected override Style CreateControlStyle()
        {
            return new TableStyle(ViewState);
        }

        public virtual GridLines GridLines
        {
            get { return ((TableStyle)ControlStyle).GridLines; }
            set { ((TableStyle)ControlStyle).GridLines = value; }
        }

        public virtual int CellSpacing
        {
            get { return ((TableStyle)ControlStyle).CellSpacing; }
            set { ((TableStyle)ControlStyle).CellSpacing = value; }
        }

        public virtual int CellPadding
        {
            get { return ((TableStyle)ControlStyle).CellPadding; }
            set { ((TableStyle)ControlStyle).CellPadding = value; }
        }

        public virtual HorizontalAlign HorizontalAlign
        {
            get { return ((TableStyle)ControlStyle).HorizontalAlign; }
            set { ((TableStyle)ControlStyle).HorizontalAlign = value; }
        }

        public virtual string BackImageUrl
        {
            get { return ((TableStyle)ControlStyle).BackImageUrl; }
            set { ((TableStyle)ControlStyle).BackImageUrl = value; }
        }

        #endregion

        #region Names

        protected virtual string PaymentMethodSelectName
        {
            get { return this.UniqueID + ":PaymentMethod"; }
        }

        protected virtual string CreditCardNoTextInputName
        {
            get { return this.UniqueID + ":CreditCardNo"; }
        }

        protected virtual string CardholderNameTextInputName
        {
            get { return this.UniqueID + ":CardholderName"; }
        }

        protected virtual string MonthSelectName
        {
            get { return this.UniqueID + ":Month"; }
        }

        protected virtual string YearSelectName
        {
            get { return this.UniqueID + ":Year"; }
        }

        protected virtual string HeaderSelectName
        {
            get { return this.UniqueID + ":Header"; }
        }

        protected virtual string SubmitInputName
        {
            get { return this.UniqueID; }
        }

        #endregion

        #region Ids

        protected virtual string PaymentMethodSelectId
        {
            get { return this.UniqueID + "_PaymentMethod"; }
        }

        protected virtual string CreditCardNoTextInputId
        {
            get { return this.UniqueID + "_CreditCardNo"; }
        }

        protected virtual string CardholderNameTextInputId
        {
            get { return this.UniqueID + "_CardholderName"; }
        }

        protected virtual string MonthSelectId
        {
            get { return this.UniqueID + "_Month"; }
        }

        protected virtual string YearSelectId
        {
            get { return this.UniqueID + "_Year"; }
        }

        protected virtual string HeaderSelectId
        {
            get { return this.UniqueID + "_Header"; }
        }

        protected virtual string SubmitInputId
        {
            get { return this.UniqueID; }
        }

        #endregion

        #region Credit Card Info Fields

        public PaymentMethod PaymentMethod
        {
            get { return ViewState["PaymentMethod"] != null ? (PaymentMethod)ViewState["PaymentMethod"] : PaymentMethod.Visa; }
            set { ViewState["PaymentMethod"] = value; }
        }
        public string CreditCardNo
        {
            get { return ViewState["CreditCardNo"] != null ? (string)ViewState["CreditCardNo"] : string.Empty; }
            set { ViewState["CreditCardNo"] = value; }
        }
        public string CardholderName
        {
            get { return ViewState["CardholderName"] != null ? (string)ViewState["CardholderName"] : string.Empty; }
            set { ViewState["CardholderName"] = value; }
        }
        public DateTime ExpirationDate
        {
            get { return ViewState["ExpirationDate"] != null ? (DateTime)ViewState["ExpirationDate"] : DateTime.Now; }
            set { ViewState["ExpirationDate"] = value; }
        }

        private bool hasPaymentMethodChanged;
        private bool hasCreditCardNoChanged;
        private bool hasCardholderNameChanged;
        private bool hasExpirationDateChanged;

        #endregion

        #region Postback methods

        void IPostBackEventHandler.RaisePostBackEvent(string args)
        {
            RaisePostBackEvent(args);
        }

        protected virtual void RaisePostBackEvent(string args)
        {
            ValidatingCreditCardEventArgs ve = new ValidatingCreditCardEventArgs(PaymentMethod, CreditCardNo, CardholderName, ExpirationDate);
            OnValidatingCreditCard(ve);
        }


        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection values)
        {
            return LoadPostData(postDataKey, values);
        }

        protected virtual bool LoadPostData(string postDataKey, NameValueCollection values)
        {
            PaymentMethod paymentMethod = values[PaymentMethodSelectName] == "0" ? PaymentMethod.Visa : PaymentMethod.MasterCard;
            if (paymentMethod != PaymentMethod)
            {
                PaymentMethod = paymentMethod;
                hasPaymentMethodChanged = true;
            }

            string creditCardNo = values[CreditCardNoTextInputName];
            if (creditCardNo != CreditCardNo)
            {
                CreditCardNo = creditCardNo;
                hasCreditCardNoChanged = true;
            }

            string cardholderName = values[CardholderNameTextInputName];
            if (cardholderName != CardholderName)
            {
                CardholderName = cardholderName;
                hasCardholderNameChanged = true;
            }

            int month = int.Parse(values[MonthSelectName]);
            int year = int.Parse(values[YearSelectName]);
            DateTime expirationDate = new DateTime(year, month, 25);
            if (expirationDate != ExpirationDate)
            {
                ExpirationDate = expirationDate;
                hasExpirationDateChanged = true;
            }

            if (!string.IsNullOrEmpty(values[SubmitInputName]))
                Page.RegisterRequiresRaiseEvent(this);

            return hasPaymentMethodChanged ||
                  hasCreditCardNoChanged ||
                  hasCardholderNameChanged ||
                  hasExpirationDateChanged;
        }

        void IPostBackDataHandler.RaisePostDataChangedEvent()
        {
            RaisePostDataChangedEvent();
        }

        protected virtual void RaisePostDataChangedEvent()
        {
            if (hasPaymentMethodChanged)
                OnPaymentMethodChanged(EventArgs.Empty);

            if (hasCreditCardNoChanged)
                OnCreditCardNoChanged(EventArgs.Empty);

            if (hasCardholderNameChanged)
                OnCardholderNameChanged(EventArgs.Empty);

            if (hasExpirationDateChanged)
                OnExpirationDateChanged(EventArgs.Empty);
        }

        #endregion

        #region Events

        private static readonly object ValidatingCreditCardEventKey = new object();
        public event ValidatingCreditCardEventHandler ValidatingCreditCard
        {
            add { Events.AddHandler(ValidatingCreditCardEventKey, value); }
            remove { Events.RemoveHandler(ValidatingCreditCardEventKey, value); }
        }

        protected virtual void OnValidatingCreditCard(ValidatingCreditCardEventArgs e)
        {
            ValidatingCreditCardEventHandler handler = Events[ValidatingCreditCardEventKey] as ValidatingCreditCardEventHandler;

            if (handler != null)
                handler(this, e);
        }

        private static readonly object PaymentMethodChangedEventKey = new object();
        public event EventHandler PaymentMethodChanged
        {
            add { Events.AddHandler(PaymentMethodChangedEventKey, value); }
            remove { Events.RemoveHandler(PaymentMethodChangedEventKey, value); }
        }

        protected virtual void OnPaymentMethodChanged(EventArgs e)
        {
            EventHandler handler = Events[PaymentMethodChangedEventKey] as EventHandler;

            if (handler != null)
                handler(this, e);
        }

        private static readonly object CreditCardNoChangedEventKey = new object();
        public event EventHandler CreditCardNoChanged
        {
            add { Events.AddHandler(CreditCardNoChangedEventKey, value); }
            remove { Events.RemoveHandler(CreditCardNoChangedEventKey, value); }
        }

        protected virtual void OnCreditCardNoChanged(EventArgs e)
        {
            EventHandler handler = Events[CreditCardNoChangedEventKey] as EventHandler;

            if (handler != null)
                handler(this, e);
        }

        private static readonly object CardholderNameChangedEventKey = new object();
        public event EventHandler CardholderNameChanged
        {
            add { Events.AddHandler(CardholderNameChangedEventKey, value); }
            remove { Events.RemoveHandler(CardholderNameChangedEventKey, value); }
        }

        protected virtual void OnCardholderNameChanged(EventArgs e)
        {
            EventHandler handler = Events[CardholderNameChangedEventKey] as EventHandler;

            if (handler != null)
                handler(this, e);
        }

        private static readonly object ExpirationDateChangedEventKey = new object();
        public event EventHandler ExpirationDateChanged
        {
            add { Events.AddHandler(ExpirationDateChangedEventKey, value); }
            remove { Events.RemoveHandler(ExpirationDateChangedEventKey, value); }
        }

        protected virtual void OnExpirationDateChanged(EventArgs e)
        {
            EventHandler handler = Events[ExpirationDateChangedEventKey] as EventHandler;

            if (handler != null)
                handler(this, e);
        }

        #endregion

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Table; }
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");                     
            writer.RenderBeginTag(HtmlTextWriterTag.Th);
            writer.Write(HeaderText);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(PaymentMethodText);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, PaymentMethodSelectName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, PaymentMethodSelectId);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Option);
            writer.Write("Visa");
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
            writer.RenderBeginTag(HtmlTextWriterTag.Option);
            writer.Write("MasterCard");
            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();


            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(CreditCardNoText);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, CreditCardNoTextInputName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, CreditCardNoTextInputId);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();


            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(CardholderNameText);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, CardholderNameTextInputName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, CardholderNameTextInputId);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(ExpirationDateText);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, MonthSelectName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, MonthSelectId);
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            for (int month = 1; month < 13; month++)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.Option);

                if (month < 10)
                    writer.Write("0" + month.ToString());
                else
                    writer.Write(month);

                writer.RenderEndTag();
            }

            writer.RenderEndTag();
            writer.Write(" ");

            writer.AddAttribute(HtmlTextWriterAttribute.Name, YearSelectName);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, YearSelectId);
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            for (int year = DateTime.Now.Year; year < DateTime.Now.Year + 10; year++)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.Option);
                writer.Write(year);
                writer.RenderEndTag();
            }

            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();

            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            //writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            //writer.AddAttribute(HtmlTextWriterAttribute.Value, "Submit");
            //writer.AddAttribute(HtmlTextWriterAttribute.Name, SubmitInputName);
            //writer.AddAttribute(HtmlTextWriterAttribute.Id, SubmitInputId);
            //writer.RenderBeginTag(HtmlTextWriterTag.Input);
            //writer.RenderEndTag();
            //writer.RenderEndTag();
            //writer.RenderEndTag();
        }
    }
}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester



IT-JOB

Danske Spil A/S

Senior backend-udvikler

Rohde & Schwarz Technology Center A/S

FPGA-udvikler

Capgemini Danmark A/S

Project Manager