Web Form User Control Render HTML - c#

I have 2 controls in ascx file.
1) <input type="checkbox" id='<%= ECA_AdditionalOffers[i].Value["text"] %>_x'/>
2 )<asp:Literal Text='<%= ECA_AdditionalOffers[i].Value["text"] %>' runat="server" />
Everything is fine with checkbox. But literal control is not allowing me, to use '<%=' correctly. What can I do?
It just renders me this:
<div class="fleft for_dis" style="padding-top:2px;">
<input id="M:Vairuotojo ir keleivių draudimas <b>(+ 3 EUR)</b>_x" type="checkbox">
<%= ECA_AdditionalOffers[i].Value["text"] %>
</div>

Yes. You are right. Server side controls can't be touched with this method.

Related

asp.net can't take the text of input text

This is my aspx
<p>Date: <input type="text" id="datepicker" >
<asp:Button id="BookingForDate" runat="server" OnClick="BookingForDate_Click" Text="Search"/> </p>
<table id="DateBookingTable" runat="server" style="visibility:hidden">
<tr><th>ID</th><th>PlanTime</th></tr>
</table>
on my code in c# I can't see datepicker
why?
note please that i can take all the controllers from id except this one.
I already tried restart the visual studio
You have 2 options
add runat="server" to the input
<input type="text" id="datepicker" runat="server" />
or create an TextBox
<asp:TextBox runat="server" id="datepicker"></asp:TextBox>
You should add
runat="server"
to your input. No you are using an html control and not an asp.net server side control. Hence you can't access the value of the textbox the way you want.
So try the following and then you will be able to access the value of your textbox:
<asp:TextBox id="datepicker" runat="server"/>
Then in your code behind class you can access the value of this textbox
datepicker.Text
Another approach, as I pointed initially, it would be the following:
<input type="text" id="datepicker" runat="server">
You forgot to add the runat-attribute to your input-tag. Try this:
<input type="text" id="datepicker" runat="server">
With that change, you should be able to access the element from code behind
See MSDN
You have not given name to your control and also add runat="server"
<input type="text" id="datepicker" runat="server" name='datepicker' />

Post data to another site from code behind

I have a web form which is posting a few fields to a payment site. The structure the example is using is
<form method="post" action="<%= FormAction %>">
<input type="hidden" name="Hash" value="<%= sHash %>" />
<input type="hidden" name="MID" value="<%= sMID %>" />
......
<div class="submit">
<input type="submit" value="Post Data" />
</div>
Within the example project they have set the same variables within the code behind file (after declaring them) with values. When the button above is clicked it posts the data to the relevant page without errors and the user is redirected to the payment page.
So i am now trying the same under the click event of an image button on my site but doesnt work:
<asp:ImageButton ID="ButtonCheckout" runat="server" Text="Go to payment page" ImageUrl="~/Images/Payment.gif" />
and of course it doesnt work, ive added all the markup as above and added all sample code.
When i run the app it flashes and stops. I look in Fiddler and notice its not going to the payment site at all. The masterpage has declared in it - incase this makes any difference.
Reading around it seems i could use the image button to post the data and do exactly the same as the submit button (submit button is above which was part of the sample application from the payment provider) by using the onClick event in the markup with Javascript but im struggling to understand how to tie it all together and lost to what i could do next to get this working?
You can't just post form data with an image button. You need to submit the form data - otherwise your image button is just a link. I do this a lot with Javascript.
Try adding this.form.submit() to the clientclick. Or name your form and use this.formname.submit()
Either of those should post your form data.
You can such so write:
<form id="Form2" method="post" runat="server" action="<%= FormAction %>">
<input type="hidden" name="Hash" value="<%= sHash %>" />
<input type="hidden" name="MID" value="<%= sMID %>" />
......
<div class="submit">
<asp:ImageButton ID="ButtonCheckout" runat="server" Text="Go to payment page"/>
</div>
</form>
but you must add a few attributes in top of web page,eg:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableEventValidation="false" EnableViewStateMac="false" ValidateRequest="false" ViewStateEncryptionMode="Never" %>

ASP.net posting a form with element name prefix "ct100$"

I am writing a website master page with a form for member login
Below is the markup from the master page:
<form id="loginForm" action="account.aspx" method="POST">
<div class="div-topHead">
<input type="button" id="submitLogin" title="Login" value="Login" />
<input type="button" id="forgetPW" title="Forget password" value="?" onclick="window.open('forgetpassword.aspx','_self')" />
</div>
<div class="div-topTail">
<div class="div-login">
<div class="row-body row-def">
<input id="input_memID" name="input_memID" runat="server" type="text" maxlength="255" />
</div>
<div class="row-header row-def">
Member ID:
</div>
</div>
<div class="div-login">
<div class="row-body row-def">
<input id="input_memPW" name="input_memPW" runat="server" type="password" maxlength="255" />
</div>
<div class="row-header row-def">
Password:
</div>
</div>
</div>
</form>
and below is the posting script
$(function(){
$('#submitLogin').click(function(){
var f = $('#loginForm').get(0);
$.post("../script/loginCheck.aspx", { "memID": f.inputID.value, "memPW": f.inputPW.value }, function(data){
var result = JSON.parse(data);
if(result[0] == 1){
//Login validated
f.submit();
} else {
//Login is invalid
alert(result[1]);
}
});
});
});
The problem is, when i open the website on browser,
the text input field names are automatically added a prefix "c100$"
and then from console I can see I got below error
Uncaught TypeError: Cannot read property 'value' of undefined
I have tried to lookup on the net what is this happening,
and seems it is something .NET automatically does.
Yet I could not find a way to make my post script works.
I don't need to keep the ID as it was without ct100,
as long as the posting script can work
I tried changing to f.ct100$input_memID etc but not working
Also tried the clientIDMode="Static" but can't get it work either
the browser complains clientIDMode is not an attribute of <% PAGE %>
So could someone please kindly advise what can I do?
Thanks in advance!
ClientIDMode is a property that can be added to individual controls.
In addition (from MSDN):
You can set the ClientIDMode value for all pages in a Web site by setting the pages element in the site's Web.config file. You can set the ClientIDMode value for a page in the # Page directive.
This for instance:
<div id="dvDiv" runat="server" clientidmode="Static" />
Will make the div's name be "dvDiv" in both client and server side.
So $("#dvDiv") will work fine on the client side.
About your last remark, make sure you put it in <%# Page %> and not in <% Page %> (notice the # sign)
Although Blachshma's answer is perfectly reasonable and will solve your problem, I recommend simply taking off the runat="server" attributes on your inputs.
I can't see that you have any real reason to have these exist as HtmlInputText objects if you don't need to reference them from page code-behind.
EDIT: as per comment, if you do need to access them from code-behind, then it's a better idea to simply change the ClientIDMode to Static.
You can simply use this jquery selector to get any element you want :-
$("[id$='yourElementID']")// Select an Element whose ID ends with yourElementID
Example :-
$("[id$='submitLogin']").click(function(){
var f = $("[id$='loginForm']").get(0);
....................
See The Detailed Information here
If the Javascript code is contained inside the aspx markup, you can also use
var f = $('#<%= loginForm.ClientID %>').get(0);

How to retrieve HTML control values which are placed in contentPlaceHolder in code behind file?

I've designed a web site. In that web site I used master page for my web page . And in that web page I've placed HTML controls which are in the content place holder. I want to retrieve values of the HTML control in the code behind file. How to do this?
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<span>
<select id="Select1">
<option value="1">Low</option>
<option value="2">Medium</option>
<option value="3">High</option>
<option value="4">Not Assigned</option>
</select></span>
<span>
<input id="Text1" type="text" /></span>
<span>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
</span>
</div>
</asp:Content>
I want to retrieve the HTML control value on the click event of the Button1.
As TBohnen.jnr says, you can mark them as runat="server".
If you don't want to do that then you can use Request.Form["idOfInput"], but that's not generally recommended when using WebForms.
Mark them as runat="server" on the aspx page and give them an ID and you should see them in the code behind
****Edit****
You can alternatively just use an ajax call either via web method or do a seperate postback to send only the data you want if you are worried about performance.
Have a look at this article.

Eval() and expression code

I have such code in ASP.NET web page but when I run it I get error.
> Databinding methods such as Eval(), XPath(), and Bind() can only
> be used in the context of a databound control.
Where is the problem?
<% if (Helpers.GetInt(Eval("Price")) != 0)
{ %>
<input type="button" name="btnSignUp" value="Sign Up - Plimus »" onclick="window.location='<%#Eval("BuyUrl2")%><%= Common.GetUserIdUrl("&","custom_user_id") %>'" />
<% } %>
I get error code on line
<% if (Helpers.GetInt(Eval("Price")) != 0)
I complete Repeater code is below. As I said early everything in this code works fine except IF statement. I want to evaluate PRICE and if it is !=0 show button html code.
<ItemTemplate>
<div class="row">
<div class="col title">
<%#Eval("Title")%>
</div>
<hr/>
<div class="col price">
<%#string.Format("{0} USD/{1} </br> Instant activation", Helpers.GetDecimal(Eval("Price")).ToString("N"), Portal.GetMembershipTypeLabel(Helpers.GetInt(Eval("Credits"))))%>
</div>
<div class="col">
<input type="button" name="btnSignUp" value="Sign Up - PayPal »" onclick="window.location='<%#Eval("BuyUrl1")%><%= Common.GetUserIdUrl("&","custom_user_id") %>'" />
<br />
<br />
<% if (Helpers.GetInt(Eval("Price")) != 0)
{ %>
<input type="button" name="btnSignUp" value="Sign Up - Plimus »" onclick="window.location='<%= Eval("BuyUrl2")%><%= Common.GetUserIdUrl("&","custom_user_id") %>'" />
<% } %>
</div>
<hr/>
</div>
</ItemTemplate>
yes, you are trying to use Eval in an HTML input control, which is not allowed as stated correctly by the error.
Eval or Bind and fellows are executed usually when you bind a data-boundable control like DataList, DataGrid, DataRepeater and so on because these commands (Eval...) are applied against the DataSource you are binding. doing so against a control like raw HTML input has no meaning because there is nothing to bind against.
notice that your call/usage of <%= Common.GetUserIdUrl... looks correct and can stay there :)

Categories

Resources