Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 86
Visual Studio npm 1 29
reading excel file in .net 2 31
Validation for a field in asp.net 3 11
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…
Are you using email marketing software? If not, you're missing out on effortless marketing and the reaching of desired conversion rates through email marketing software.
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.
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…

756 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