开发者

ASP.NET HtmlInputHidden control - stop name from changing?

I have an asp.net page and I'm trying to add a couple of hidden HTML input controls. I will be submitting this form to another site (PP) so I need the names of the controls to NOT change when rendered. Is there a way to make ASP.NET honor the name开发者_StackOverflow property that I set in the code-behind?

As an alternate, I don't need to dynamically create these controls, I can also just assign the value to an existing HTML field but I don't know how to do that with ASP.NET/C#


Are you using ASP.NET 4.0? You can use ClientIDMode on the control with a value of static, and the ID will not change.

Otherwise you could use a literal to output the element itself or just its value. On you're page you'd have:

<input type="hidden" id="ppID" value='<asp:Literal ID="ppIDValue" runat="server" />' />

or

<input type="hidden" id="ppID" value="<%= ppIDValue %>" />

And in the code behind or wherever:

this.ppIDValue.Text = "value for ppID";

or (abbreviated):

public class MyPage : Page
{
    public string ppIDValue = "0";
    override OnLoad()
    { this.ppIDValue = "100"; }
}


The problem with putting literals inside HTML-tags is that the literals are very likely to, at some annoying point in time, to be removed from the designer-codebehind file. Even though you could manually add them, this will in most case screw up the mapping between the GUI-based editor and the designer-codebehind.

Further, if you are using Master Pages, they will anyway change the value of the name-attribute on your hidden fields.

I have been in search for the best way to do this myself, and was just about to write my own custom control, so I could preserve the value of the name-attribute. Then I came across this answer to a similar question, where Page.ClientScript.RegisterHiddenField("ppID", "1234") was mentioned. Bingo!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜