Solved

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

Posted on 2007-11-22
4
2,567 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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:
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

15 Experts available now in Live!

Get 1:1 Help Now