Solved

Manually controlling name attribute of tags for asp.net html controls

Posted on 2007-11-22
4
2,569 Views
Last Modified: 2013-11-29
I'm writing a form to send credit card info to a payment gateway and the API requires me to include a number of hidden fields woth very specific names. So when asp.net prefixes my names with e.g. ctl00$ContentPlaceHolder1 rendering this html tag:

<input type="hidden" name="ctl00$ContentPlaceHolder1$shopid" id="ctl00_ContentPlaceHolder1_PaymentBox1_shopid" value="12xxxxxxxxxx" />    

The form doesn't comply with the API spec and the transaction is rejected.

How do I force ASP.NET not to prefix the name attribute? Or how do I programatically set the value attribute of a plain html tag from my code behind file?

Thanks,
Kaare
0
Comment
Question by:krubak
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:gocemi
ID: 20334083
gocemi:
1.you should add this in the code behind:
/// <summary>
/// Override to force simple IDs all around
/// </summary>
public override string ClientID
{
get
{
if(this.Id == "hidden field")
//return changed name for this control only
return "canged_name";
//for others return real client id
else return this.ClientID;
}

4. With this you actually override the Client ID value for a control
5. Be very carefull not to have 2 controls with same id, cos it will throw exception
6. I've tried and it is functioning, actually you can have each control to have same id as the server's one.

with this name will be chaned as well
0
 
LVL 3

Author Comment

by:krubak
ID: 20334988
Could You elaborate a bit? I've tried to put this in my partial class for the page, e.g.

public partial class CheckoutDK : System.Web.UI.Page
{
    public override string ClientID
    {
        get
        {
            if (this.ID == "shopid")
                //return changed name for this control only
                return "shopid";
            //for others return real client id
            else
                return this.ClientID;

        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
       ....
    }
}

I've tried to put a breakpoint on the if (this.ID == "shopid") but it's apparently not called when the page is rendered.
0
 
LVL 5

Accepted Solution

by:
gocemi earned 500 total points
ID: 20337731
Hi,

One more thing i forgot ( sorry)

Every webUsercontrol that you are using will need to have it's Client and uniqueid Changed.
So  In the web users control, not the PAGE since all controls that are directly in the page will be named OK

public override string ClientID/UNIQUEID
    {
        get
        {
                return "";

        }
    }


So whenever page is rendering ListBox inside WebUserControl that is placed on a page,
PAge will ask the Control to give it's uniqueid that with this code be "", so controls inside will be not prefixed.

0
 
LVL 3

Author Comment

by:krubak
ID: 20340477
Thanks, I got it. I ended up using the overrides described above while creating new server controls for the html controls I needed on the form to control the UniqueID and the name attribute.

If others find this thread these links might be helpfull:
http://www.west-wind.com/WebLog/posts/4605.aspx
http://forums.asp.net/p/1050481/1483968.aspx#1483968
http://msdn2.microsoft.com/EN-US/library/aa310915(vs.71).aspx
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now