Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP.NET Control Custom Attribute

ASP.NET Control Custom Attribute

Scheduled Pinned Locked Moved ASP.NET
csharpjavascripthtmlasp-netsysadmin
12 Posts 4 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Arun Jacob

    Why you want to try like this?whats your exact requirement?

    Arun Jacob http://codepronet.blogspot.com/

    A Offline
    A Offline
    Ahmed Galal
    wrote on last edited by
    #3

    am just trying to add a custom attribute with the id of what he wrote in the TextBox in a custom attribute, i know i could use hidden field and it would work just fine, but am tryin to look for a clean clean solution, and also wondering why it doesnt work.. thanks for reply

    1 Reply Last reply
    0
    • A Ahmed Galal

      I'm tryin to add a custom attribute in the html of a TextBox control in an asp.net page, like the following:

      <asp:TextBox runat="server" ID="txtUserName" myAttr="10" ></asp:TextBox>

      when i try to change this attribute from javascript and then read it in the code-behind, i dont get the changes, however if i changed any of the other control attributes like value, it works just fine. i dont know why i cant add my own attributes. is there anything i should do to tell the asp.net to render my new custom attribute to the viewstate so i can read it later in the code-behind? i would appreciate any help. thanks in advance.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #4

      How are you trying to access the attribute? txtUserName.Attributes["myAttr"] Perhaps if you showed a snippet we could see a problem.


      I know the language. I've read a book. - _Madmatt

      A 1 Reply Last reply
      0
      • N Not Active

        How are you trying to access the attribute? txtUserName.Attributes["myAttr"] Perhaps if you showed a snippet we could see a problem.


        I know the language. I've read a book. - _Madmatt

        A Offline
        A Offline
        Ahmed Galal
        wrote on last edited by
        #5

        yes exactly, and it doesnt work :S and as i said before, if i tried to change any of the control attributes like value it works fine, i assume that the ViewState doesnt save my new custom attribute, dunno whats wrong .. i'm using .net 4 but i also tried on .net 2.0, and its the same. thanks for ur time

        N 1 Reply Last reply
        0
        • A Ahmed Galal

          I'm tryin to add a custom attribute in the html of a TextBox control in an asp.net page, like the following:

          <asp:TextBox runat="server" ID="txtUserName" myAttr="10" ></asp:TextBox>

          when i try to change this attribute from javascript and then read it in the code-behind, i dont get the changes, however if i changed any of the other control attributes like value, it works just fine. i dont know why i cant add my own attributes. is there anything i should do to tell the asp.net to render my new custom attribute to the viewstate so i can read it later in the code-behind? i would appreciate any help. thanks in advance.

          A Offline
          A Offline
          Arun Jacob
          wrote on last edited by
          #6

          Ahmed Galal wrote:

          when i try to change this attribute from javascript and then read it in the code-behind, i dont get the changes,

          Yes,you won't get it from code behind.

          Ahmed Galal wrote:

          however if i changed any of the other control attributes like value, it works just fine.

          If you change attribute value from javascript you'll get that change in code behind because its an input control type.

          Arun Jacob http://codepronet.blogspot.com/

          A 1 Reply Last reply
          0
          • A Arun Jacob

            Ahmed Galal wrote:

            when i try to change this attribute from javascript and then read it in the code-behind, i dont get the changes,

            Yes,you won't get it from code behind.

            Ahmed Galal wrote:

            however if i changed any of the other control attributes like value, it works just fine.

            If you change attribute value from javascript you'll get that change in code behind because its an input control type.

            Arun Jacob http://codepronet.blogspot.com/

            A Offline
            A Offline
            Ahmed Galal
            wrote on last edited by
            #7

            am just trying to figure out a way that could make it work, u think theres no way to read my custom attributes in code-behind ? or by any chance i could render this attribute to the ViewState or making it a control attribute like the others ? i think i will go for the hidden fields if i failed on this. thanks again

            A 1 Reply Last reply
            0
            • A Ahmed Galal

              am just trying to figure out a way that could make it work, u think theres no way to read my custom attributes in code-behind ? or by any chance i could render this attribute to the ViewState or making it a control attribute like the others ? i think i will go for the hidden fields if i failed on this. thanks again

              A Offline
              A Offline
              Arun Jacob
              wrote on last edited by
              #8

              Ahmed Galal wrote:

              i think i will go for the hidden fields if i failed on this.

              I think this is the solution.

              Arun Jacob http://codepronet.blogspot.com/

              1 Reply Last reply
              0
              • A Ahmed Galal

                yes exactly, and it doesnt work :S and as i said before, if i tried to change any of the control attributes like value it works fine, i assume that the ViewState doesnt save my new custom attribute, dunno whats wrong .. i'm using .net 4 but i also tried on .net 2.0, and its the same. thanks for ur time

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #9

                You would need to create a custom control that inherits from textbox. It does't matter if tis 4.0 or 2.0


                I know the language. I've read a book. - _Madmatt

                A 1 Reply Last reply
                0
                • A Ahmed Galal

                  I'm tryin to add a custom attribute in the html of a TextBox control in an asp.net page, like the following:

                  <asp:TextBox runat="server" ID="txtUserName" myAttr="10" ></asp:TextBox>

                  when i try to change this attribute from javascript and then read it in the code-behind, i dont get the changes, however if i changed any of the other control attributes like value, it works just fine. i dont know why i cant add my own attributes. is there anything i should do to tell the asp.net to render my new custom attribute to the viewstate so i can read it later in the code-behind? i would appreciate any help. thanks in advance.

                  A Offline
                  A Offline
                  Arindam Tewary
                  wrote on last edited by
                  #10

                  You can create your own text box control( a custom user control and can append the properties ) Is that you strictly want to write that in ???

                  public partial class CustomTextBox : System.Web.UI.UserControl
                  {
                  String sSampleData = String.Empty;
                  protected void Page_Load(object sender, EventArgs e)
                  {
                  if (this.sSampleData == "")
                  {
                  Page.Response.Write("No data set");
                  }
                  else
                  {
                  Page.Response.Write(this.sSampleData);
                  }
                  }
                  public String MyAttribute
                  {
                  set { sSampleData = value; }
                  get { return sSampleData; }
                  }
                  }

                  <form id="form1" runat="server">
                  <ADT:TextBox ID="aa" MyAttribute="Its working" runat="server" />
                  </form>

                  1. The equivalent .NET class of is pre-written and you can not modify that. hence you can not add an extra "attribute" the way you have been trying to do. 2. When an attribute is added in following way

                  TextBox1.Attributes.Add("onclik","something");

                  it is added to rendered control's attribute collection. These two attribute are different things. Thats why what you write in "design" view, after rendering you see different things.( asp:dropdownlist changes to select/option , asp:datagrid changes to <table > etc.

                  Thanks, Arindam D Tewary

                  modified on Wednesday, April 7, 2010 9:13 AM

                  A 1 Reply Last reply
                  0
                  • A Arindam Tewary

                    You can create your own text box control( a custom user control and can append the properties ) Is that you strictly want to write that in ???

                    public partial class CustomTextBox : System.Web.UI.UserControl
                    {
                    String sSampleData = String.Empty;
                    protected void Page_Load(object sender, EventArgs e)
                    {
                    if (this.sSampleData == "")
                    {
                    Page.Response.Write("No data set");
                    }
                    else
                    {
                    Page.Response.Write(this.sSampleData);
                    }
                    }
                    public String MyAttribute
                    {
                    set { sSampleData = value; }
                    get { return sSampleData; }
                    }
                    }

                    <form id="form1" runat="server">
                    <ADT:TextBox ID="aa" MyAttribute="Its working" runat="server" />
                    </form>

                    1. The equivalent .NET class of is pre-written and you can not modify that. hence you can not add an extra "attribute" the way you have been trying to do. 2. When an attribute is added in following way

                    TextBox1.Attributes.Add("onclik","something");

                    it is added to rendered control's attribute collection. These two attribute are different things. Thats why what you write in "design" view, after rendering you see different things.( asp:dropdownlist changes to select/option , asp:datagrid changes to <table > etc.

                    Thanks, Arindam D Tewary

                    modified on Wednesday, April 7, 2010 9:13 AM

                    A Offline
                    A Offline
                    Ahmed Galal
                    wrote on last edited by
                    #11

                    yea that would work although i was looking for a simpler method, i think hidden field is the way to go unfornatually, but thank u very much for answering.

                    1 Reply Last reply
                    0
                    • N Not Active

                      You would need to create a custom control that inherits from textbox. It does't matter if tis 4.0 or 2.0


                      I know the language. I've read a book. - _Madmatt

                      A Offline
                      A Offline
                      Ahmed Galal
                      wrote on last edited by
                      #12

                      your right it shouldnt differ between framework versions, i just doubted myself that they changed the way of rendering control or somethin. thanks again.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups