How to change name attribute of a hidden field? - c#

I have a hidden field on my aspx page and I use masterpage.
Without using masterpage everything is fine and name attribute of hiddenfield is correct.
<asp:HiddenField ID="apiversion" ClientIDMode="Static" runat="server" />
After rendering, result html is;
<input type="hidden" name="apiversion" id="apiversion" value="v0.01">
But if use masterpage then result html is being like;
<input type="hidden" name="ctl00$ContentPlaceHolder1$apiversion" id="apiversion" value="v0.01">
But I have to use masterpage and need name attribute as 'apiversion', not as 'ctl00$ContentPlaceHolder1$apiversion'.
Any solution?

I am not sure but i think this should work... Change the name attribute at document ready..
$(document).ready(function(){
$("#<%=apiversion.ClientID%>").attr("name","apiversion");
});
Alternatively..
var arrayOfNames=[];
var actualName=$("#<%=apiversion.ClientID%>").attr("name");
arrayOfNames=actualName.split('$');
var whatYouActuallyWant=arrayOfNames[2];// This is the name you have without master page.
Keeping the comment below in consideration.. In case of partial postbacks like in case of update panel, add a pageLoad function...
function pageLoad(sender,args){
$("#<%=apiversion.ClientID%>").attr("name","apiversion");
}

Related

input "id" and "name" changes when set runat=server from

In my form I need to insert different inputs of type "hidden".
The inputs must be html controls with "name" and "id's". Because I send this form to a external url.
For the validation I do runat=server in a hidden input and then I can use requiredfieldvalidator.
But the problem is when I look in the source after visiting the page the name is changed. for example
<input type="hidden" name="hotelIdform" value="" runat="server" id="hotelIdform">
changed to
<input name="ctl00$ctl00$Master_Body$child_center_content$hotelIdform" type="hidden" id="hotelIdform" value="b4ba78fc-0b62-4809-9dca-000972573139" />
and i used ClientIDMode="Static" , just ID is okay
please help me
It seems you are using Master pages and not using static client ID. Thus, ASP.NET is changing the ID while it compiling the page.
You need to set ClientIDMode to Static.
Please refer this SO post

Get HTML element attribute value using Request[]

I have a page where many postback clicks are there.
In this page many input elements there, which values are i am geting from server side using Request[]. like below
.aspx
<input id="txtRefTypeCtrlType_3" name="txtRefTypeCtrlType_3" lastvalue="4" CTRLtype="4" style="display: none;">
.cs
string strCTRLtype = Request["txtRefTypeCtrlType_3"];
now i have a scenario, where i need to get the CTRLtype attribute value.
Can we get the attribute value using Request[] or something else.
Please advise.
Thanks.
No, you can't get the attribute value on the server side because it isn't posted. The only real way of achieving this is to create a hidden field with the attribute value and use that:
<input type="hidden" name="ctrlType_3" value="4" />
Codebehind:
string attrValue = Request["ctrlType_3"];

Access input hidden field Title attribute from code behind

I have a HTML hidden field:
<input type="hidden" title="aaa" value="bbb" id="TAB" runat="server" />
I want to access the title attribute from the code behind, like if its a input="text", there is a option TAB.ToolTip, but not for "hidden". Is it possible to get the title since it exists like an attribute?
I assume that your control is recognized by the webserver as a HtmlInputHidden instance. You should be able to fetch all attributes of the control using the Attributes property (see http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontrol.attributes(v=vs.90))
In you code behind:
TAB.Attributes["title"] = "aaa";

Form GET in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5.
On the page, there is a small form to submit your e-mail address to an external newsletter site. Here's the code:
<form name="emailForm" action="http://www.site.com/emailsignup.aspx" method="get">
<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
</form>
I was hoping I'd just be able to drop this on the page and it would work, but it doesn't, it just reloads the page.
How am I supposed to do this? Does it need to be done in the code behind of the button's click event?
In ASP.Net, by default controls - like the button - that cause postbacks will submit the page back to itself EVEN if you set the action attribute on the page to another file. What you want is called Cross-Page Posting. The following MSDN pages shows you how to do this with ASP.Net 4, but there is a link at the top to get to older versions:
http://msdn.microsoft.com/en-us/library/ms178140.aspx
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Otherwise you can just use the Button's Click Event Handler in the code behind.
Hope this helps.
you must be missing runat="server" in the form tag
try to create a page through the IDE and paste the code for input tags between the form tags
<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
It surely is ending nested in the form aspx get by default.
Depending on the layout of your page, you can modify it so you don't end with a nested form. If that's not possible, I think you can't get around to use a form, so instead you'll have to look at a different solution like building the get with js.
The easiest way would be to put that <form> tag outside the main <form runat="server"> tag that usually wraps all ASP.NET controls.
If you're using a master page and the only content placeholder you can use is within that <form runat="server" tag, or you need this form tag in the page structure within the main <form runat="server"> tag then you need to:
Take out the simple <form> tag but leave the HTML <input> tags. Handle the client onclick event (the JavaScript versions, not ASP.NET postback handlers) of the submit button. That handler should grab the e-mail from the text box and issue something like window.location = 'http://www.site.com/emailsignup.aspx?email=....' in Javascript. Make sure to cancel the default HTML button action handler so it doesn't bubble up and submit the ASP.NET form too.

ASP.NET How to read HTML Form Element?

I have an ASP.NET web form where I have an hidden field, like this:
<form id="form1" runat="server" action="http://localhost/fa/Default.aspx">
<div>
<input id="requestData" type="hidden" name="requestData" value="" runat="server" />
<asp:Button ID="btnPOST" Text="POST" runat="server" OnClick="do_POST" />
</div>
</form>
On the method do_POST I have this:
protected void do_POST(object sender, EventArgs e)
{
//requestDataField is of the type protected global::System.Web.UI.HtmlControls.HtmlInputHidden requestData;
requestDataField.Text = "FOO!";
}
When I submit the form (by pressing the button), it goes to the server (an handler) wheer I have this:
string requestData = context.Request.Form["requestData"];
I get an empty string..
But if I assign a value like this:
<input id="requestData" type="hidden" name="requestData" value="FOO" runat="server" />
I get the "FOO"
What am I missing?
The reason why it's not doing it is because the method is called after the page has been post back. Meaning, it is actually working if you change .Text to .Value unfortunately by that time you have already read your form and it was an empty value. I remember working on a project where you could tell your form not to submit until a function has been run (but it was with a javascript that needed to run an complete before aspx submitted). You should try to see if there is a way to force your form to run your function BEFORE doing the postback.
Your do_POST method runs on the server, not on the client, and so is setting the value of the server-side object which represents the <input> control. Your context.Request.Form["requestData"] gets the value of the field from the client side data submitted in the POST request, which was never set, so it is blank.
If you want the onClick to be a client-side function, then you need to do it a little differently. Use the OnClientClick attribute (instead of onClick). Then create a javascript method to set the field value:
<asp:Button ID="btnPOST" Text="POST" runat="server" OnClientClick="do_POST" />
<script>
function do_POST() {
document.getElementById("requestData").value = "FOO!";
}
</script>
I tried your code and did few changes to it.
Change requestDataField.Text = "FOO!"; to requestData.Value = "FOO";
Also I added two buttons. One for do_POST function and the UseSubmitBehaviour property is set as False. The other one was to submit the form.
If you want to set it on client side then you will have to use Javascript.
Use "Value" instead of "Text" property for HtmlInputHidden control:
requestDataField.Value = "FOO!";
instead of
requestDataField.Text = "FOO!";

Categories