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

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

Related

Html attribute based on server side variable?

I have something like this.
<input type="text">
I want enable/disable it based in certain variable value in server side.
I have tried this.
<input type="text" <%= DisableServiceInfo ? "disabled":"" %>/>
but not working.
I know this can be done.
<input type="text" disabled="<%= DisableServiceInfo ? "disabled":"invalid value" %>"/>
but this is not a valid mark-up. Because the only valid way to enable control is to remove disabled attribute.
I am not asking how value can be supplied based on variable but how attribute in injected
Please don't answer the ways to set it server side or by javascript. I just want to know if it is possible in this way?
Is this webforms? If you want to do this by the book, you can manually add an Id, and Runat="Server". Once you've done that, your control can be manipulated in your code-behind.
If your Id is ServiceInfo, you could do:
ServiceInfo.Attributes["disabled"] = "disabled";

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"];

In asp.net what is the difference between using asp.net web control and simple html imput control

In my asp.net web control form i am using two text box 1st is simple input html control and 2nd is asp.net input web control.
<form id="form1" runat="server">
Email: <input type="text" id="txt_email" name="txt_email" value="" /><br />
Email2: <asp:TextBox ID="txt_email2" runat="server"></asp:TextBox><br />
<asp:Button ID="btn_login" Name="btn_login" runat="server" Text="Button"
onclick="btn_login_Click" />
</form>
I need to know what is the difference using simple control and asp.net input control both of them pass the value to code behind after the form submit. can any one help me on this?
As defined in your example input type="text" won't even be visible to code-behind because it is missing runat="server" attribute.
If you do add it - there're still differences. ASP.NET TextBox is more advanced and in par with the rest of ASP.NET model (e.g. it has property .Text vs. .Value of an HtmlInput control, it has events and other properties).
But if you simple need to pass text information back to the server, either of them will do the job.
The biggest differences are that
the asp.net controls are rendered on the server, and thus they have more overhead on your server than using traditional controls - traditional controls (by default) are rendered once then basically reside on the client's browser, asp controls are persistent on the server side.
the asp controls can be accessed and worked with directly in the code behind files.
asp controls have some additional tags that can be used on their fields usually.
as was pointed out by #Yuriy-Galanter, how the value is accessed is slightly different.
The asp:Textbox renders HTML to the client/browser when the page request is made. Picture the ASP.NET control, in this case an asp:TextBox as a server side bit of code that knows to render a <input type="text"> HTML element when the request for the aspx page is made to the server.
The ASP.NET compiler, when parsing your aspx page just spits out the <input type="text"> HTML element you have for Email: and for Email2: the ASP.NET compiler knows that is a server control because of the runat="server" tag. So the ASP.NET compiler, having a reference to the ASP.NET assemblies on the server, reads the code for the <asp:TextBox> and knows to ultimately respond to the page request with an <input type="text" id="txt_email2" />
The server side controls are accessible in your code behind page. So is accessible in the code behind but the <input> element is not. Good for you to consider in your research at this point, that if you add runat="server" to your element, it is accessible in your code behind.

How to change name attribute of a hidden field?

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");
}

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.

Categories