Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem in getting the control on runtime

Posted on 2009-05-13
12
Medium Priority
?
176 Views
Last Modified: 2013-11-08
Hi

I have created a web custom control.The class is inherited from TextBox class.
I have made a property called MTBID as ID of the control.

I can put that control on aspx page and everything is working fine, but when I try to access the control in code behind file.

It is not working well.
0
Comment
Question by:KaranGupta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24382025
Are you sure you made your property with Public rather than Private or Protected?
0
 

Author Comment

by:KaranGupta
ID: 24382043
yes

public string MTBID
        {
            get;
            set;
        }      


0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24382115
Can you please paste the code how are you creating your custom control within ASPX Source.
Have you referenced it properly at the top of the page?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:KaranGupta
ID: 24382118
Here we go
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register TagPrefix="FTB" Namespace="MultiPurposeTextBox" Assembly="MultiPurposeTextBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <FTB:MultiPurposeTextControl ID="karan"  MTBErrorMessageControlType="VALIDATIONSUMMARY"  MTBText="karan"
                 runat="server"></FTB:MultiPurposeTextControl>
    
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24382154
You may be annoyed by these questions but...
Have you referenced the correct version of the assembly containing the control in this project?
And make sure that the DLL of the custom object is either registered within GAC or placed within bin folder of the current project.
After referencing, try closing the page from design and code behind mode. Re-open ASPX file in Design mode. Try double click anywhere on the form to move to code behind and see if you can now refernce your control or not. If you are still unable to do that, try creating a reference by yourself in the main .vb file of .designer.vb file and see if it gets to work.
 
0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24382157
Are you setting MTBID at runtime and it gets cleared on postback?
if so you'll need to keep the value in viewstate for example. See below how ASP.NET TextBox Text property is implemented.

you could also use the control like this without needing the viewstate implementation:
  <FTB:MultiPurposeTextControl ID="karan" MTBID="somevalue"  MTBErrorMessageControlType="VALIDATIONSUMMARY"  MTBText="karan"
                 runat="server"></FTB:MultiPurposeTextControl>



public virtual string Text
{
    get
    {
        string str = (string) this.ViewState["Text"];
        if (str != null)
        {
            return str;
        }
        return string.Empty;
    }
    set
    {
        this.ViewState["Text"] = value;
    }
}

Open in new window

0
 

Author Comment

by:KaranGupta
ID: 24382167
Hi tetorvik

I think we need to specify some attribute over the property

so that we can use it as ID
0
 

Author Comment

by:KaranGupta
ID: 24382173
Hi moghazali

Please don't hesitate in asking the question. I'll never be annoyed

Yes I have added the reference of the dll by "Add Reference"
0
 

Author Comment

by:KaranGupta
ID: 24382185
Hi

I more thing I want to share is

there is another attribute called ID
 <FTB:MultiPurposeTextControl MTBID="karan122"  MTBErrorMessageControlType="LABEL"  MTBText="123"
                 runat="server"></FTB:MultiPurposeTextControl>

when I define ID then I can get the value in code behind but not in case of MTBID
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 2000 total points
ID: 24382208
I think the ASP.NET may need the ID property to appropriate emit it as HTML and resolve many other aspects. See the following link for a little more detail:
http://msdn.microsoft.com/en-us/library/hw8sf6fb.aspx
However, you may still use MTBID in both declaration and code behind as an additional property defined in your class.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24382213
I did not dig into details but this page may also give idea on the similar but necessarily the same issue:

http://msdn.microsoft.com/en-us/library/hw8sf6fb.aspx 
0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24382321
I think ID is really required. At least I haven't seen/found any attribute that would make a custom property to replace original ID property.
It also seems that if you declare a custom control without ID like:<FTB:MultiPurposeTextControl MTBID="karan122"  MTBErrorMessageControlType="LABEL"  MTBText="123" runat="server"></FTB:MultiPurposeTextControl>
the runtime generates a "random" ID for it. In that case it is really difficult to use the control in code-behind without knowing the ID. To use the control in code behind you would need to create a helper method to browser through the page controls and returns the correct control based on control type and MTBID attribute.

I think this can be done, but I don't think you should.







0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question