I have two labels:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
and I set innerHTML by javascript:
document.getElementById('Label1').innerHTML = position.lat();
document.getElementById('Label2').innerHTML = position.lng();
How I can get those labels values in codebehind? I try:
TextBox2.Text = Label1.Text;
UPDATE:I need to get pushpin location:
<artem:GoogleMap ID="GoogleMap1" runat="server"
EnableMapTypeControl="False" MapType="Roadmap" >
</artem:GoogleMap>
<artem:GoogleMarkers ID="GoogleMarkers1" runat="server"
TargetControlID="GoogleMap1" onclientpositionchanged="handlePositionChanged">
</artem:GoogleMarkers>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<script type="text/javascript">
var list = document.getElementById("Label1");
function handlePositionChanged(sender, e) {
printEvent("Position Changed", sender, e);
}
function printEvent(name, sender, e) {
var position = e.latLng || sender.markers[e.index].getPosition();
document.getElementById('Label1').innerHTML = position.lat();
document.getElementById('Label2').innerHTML = position.lng();
}
</script>
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = Label1.Text;// return value: Label
}
You cannot access the value on server side. You will have to use a hidden field for that:
<asp:HiddenField ID="Hidden1" runat="server" />
The set the innerHtml value in the Hidden field by doing:
document.getElementById('<%= Hidden1.ClientID %>').value = position.lat();
You can then access it from server side by doing:
TextBox1.Text = Hidden1.Value;
You are not able to do that with the Label control as when the page is posted back the content of labels are not posted to the server. You would need to make use of an input control of sorts. Probably a hidden input would be your best bet.
Take a hidden field like below
<asp:HiddenField ID="hdnBody" ClientIDMode="Static" runat="server" />
Then set its value in Jquery like below
<script>
function GetEmailID() {
var bodyHtml = $("#editor").html();
$("#hdnBody").val(bodyHtml);
}
</script>
And in the code behind do this to get it
string body = hdnBody.Value;
Related
Can I call textbox changed event as text changing using jquery in asp.net?
So, I want the action to be done while the user is entering the number.
Default.aspx
<asp:TextBox ID="txtCount" CssClass="form-control" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPrice" CssClass="form-control" runat="server" OnTextChanged="OnTextChanged"></asp:TextBox>
<asp:Label ID="lblTotal" CssClass="price" runat="server"></asp:Label>
Default.aspx.cs
protected void txtIskontoTutari_TextChanged(object sender, EventArgs e)
{
double price1=double.Parse(txtPrice.Text);
double count=double.Parse(txtCount.Text);
double total=0;
total=price1*count;
lblTotal.Text = string.Format("{0:#,##0.00}", total);
}
This could be the solution:
You have do take the ID Attribute of your asp tag.
$(document).ready(function () {
$('#txtPrice').change(function(){
$(this).....
}
});
I've got default.aspx/default.aspx.cs and other.cs where i "store" a bunch of functions.
In my default.aspx i've :
<asp:ImageButton ID="ImageButton1" CssClass="btn_img" ImageUrl="./images/1.png" runat="server" OnClick='<%# "setFunction(" +Eval("aGUID") +")" %>' />
in my default.aspx.cs
protected void setFunction(object sender, ImageClickEventArgs e)
{
string istr = e.ToString();
var iguid = new Guid(istr);
// this are classes/functions stored in other.cs
ManageFun mfun = new ManageFun();
mfun.setMM(iguid);
}
I though i was doing all right, but can't figure out what i'm doing wrong!
In matter of fact it does nothing!
Apreciate any help!
Thanks!
You can try this
<h3>ImageButton Sample</h3>
Click anywhere on the image.<br /><br />
<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="ImageButton 1"
ImageAlign="left"
ImageUrl="images/pict.jpg"
OnClick="ImageButton_Click"/>
<br /><br />
<asp:label id="Label1" runat="server"/>
and C# code
public void ImageButton_Click(object sender, ImageClickEventArgs e)
{
Label1.Text = "You clicked the ImageButton control at the coordinates: (" +
e.X.ToString() + ", " + e.Y.ToString() + ")";
}
No need to write these <%#
Here is the reference Click
Basically asp:ImageButton with Onclick which will not call the server side code with Eval.
Edit:
You can directly call like this :
<asp:ImageButton ID="ImageButton1" CssClass="btn_img" ImageUrl="./images/1.png" runat="server" OnClick="setFunction" />
if you want to pass the parameter to that function you can assign the param to the CommandArguement as below
CommandArguement='<%# Eval("aGUID") %>'
in your SetFunction you can retrieve like this
string val = e.CommandArguement.ToString()
I have a webs form page and I have a text box which once clicked passes a variable to the code behind and then back into another element of that page and I cannot get it to work.
This is the closest I have got.
<asp:Panel ID="Search" runat="server" Visible="true">
<tr>
<td>
<asp:Label ID="lblSearch" runat="server" Text="Search"></asp:Label>
</td>
<td>
<asp:TextBox ID="search" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="valSearch" runat="server"
ControlToValidate="movieSearch" Text="Please enter text" />
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Save"
OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlSearchResult" runat="server" Visible="false">
<script>
var search = '<%=Server.UrlDecode(Request.QueryString["Data"]) %>';
</script>
</asp:Panel>
And the code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValid)
{
pnlSearch.Visible = false;
pnlSearchResult.Visible = true;
Response.Redirect("search.aspx?Data=" + Server.UrlEncode(search.Text));
}
}
Also this does not change the visibility of the two panels for some reason.
I would appreciate any guidance I am very new to asp and c#.
The panel's visibility is not changing because you're forcing a new GET request to the page with this: -
Response.Redirect("search.aspx?Data=" + Server.UrlEncode(search.Text));
(I'm assuming your page is called 'search.aspx')
There's no need to do this. Remove this line.
Secondly, I see you want to force the textbox's Text value into a Javascript variable. Replace this
var search = '<%=Server.UrlDecode(Request.QueryString["Data"]) %>';
with this
var search = '<%= search.Text %>';
Write below code on page event.
Another more important point is you first panel Id is "Search" not "pnlSearch" on aspx page so please correct it
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Data"] != null)
{
Search.Visible = false;
pnlSearchResult.Visible = true;
}
}
I recommend solution without using Response.Redirect.
Code Behind Submit Button click:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValid)
{
pnlSearch.Visible = false;
pnlSearchResult.Visible = true;
}
}
In Markup:
<asp:Panel ID="pnlSearchResult" runat="server" Visible="false">
<script>
var searchTxt = document.getElementById('search');
if(searchTxt.value != null && searchTxt.value != ''){
//do your stuff
}
</script>
I have a dropdown list that when selected value is equal to "Closed". I want a Required field to become enabled. I have tried this
<asp:DropDownList ID="DropDownList9" runat="server"
DataSourceID="SqlDataCaseStatus" DataTextField="CaseStatus"
DataValueField="CaseStatus" Text='<%# Bind("Case_Status") %>' AutoPostBack="False" onchange="if(this.options[this.selectedIndex].text='Closed')ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', true);else ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', false); " >
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateOfWriteOff" runat="server"
ControlToValidate="TextBox13"
ErrorMessage="Date Of Write Off is a Required Field">*</asp:RequiredFieldValidator>
But when i change the dropdown i get an error:
Microsoft JScript runtime error: Unable to set value of the property 'visibility': object is null or undefined.
any help would be much appreciated
First of all try changing = to == when comparing text with 'Closed'.
You need to use the Enabled property. Give this a go, the code is very rough and is just for an example. I have extracted the behaviour into one method, just for ease of demonstration.
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function EnableValidator() {
var dropdown1 = document.getElementById('DropDownList1');
var a = dropdown1.options[dropdown1.selectedIndex];
if (a.text == "ValidateYes1") {
document.getElementById("RequiredFieldValidator1").enabled = true;
} else {
document.getElementById("RequiredFieldValidator1").enabled = false;
}
};
</script>
<div>
</div>
<asp:DropDownList ID="DropDownList1" runat="server" onchange="EnableValidator();">
<asp:ListItem>ValidateYes1</asp:ListItem>
<asp:ListItem>ValidateNo2</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator><asp:Button
ID="Button1" runat="server" Text="Button" />
</form>
</body>
ValidatorEnable requires control and you are passing id of control
ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', true)
That is why you are getting the object null error.
Please change it to
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateOfWriteOff.ClientID %>'), true);
<asp:DropDownList ID="DropDownList9" runat="server"
DataSourceID="SqlDataCaseStatus" DataTextField="CaseStatus"
DataValueField="CaseStatus" Text='<%# Bind("Case_Status") %>' AutoPostBack="False" onchange="validateSelection(this);" >
</asp:DropDownList>
<script language="javascript" type="text/javascript">
function validateSelection(drp) {
var vc = document.getElementById('<% = RequiredFieldValidatorDateOfWriteOff.ClientID %>');
if (drp.text == 'Closed') {
ValidatorEnable(vc, true);
}
else {
ValidatorEnable(vc, false);
}
}
</script>
I have 2 labels and 2 text boxes and 1 buttons displayed.
When the page loads the Name and Button (will be initially displayed). Later when i click on the Button i need to display the age label and textbox. How can i do this ?
<ol>
<li>
<asp:Label runat="server" AssociatedControlID="Name">
User name
</asp:Label>
<asp:TextBox runat="server" ID="Name" Width="167px" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="age">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
</ol>
code for button press
protected void Button1_Click(object sender, EventArgs e)
{
}
You could set the label/textbox Visible property to True in server side. Alternatively, you could use JavaScript to avoid post backs to the server.
Add OnClientClick to your button :
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ShowLabel();"/>
and declare the JavaScript function on page:
<script type="text/javascript">
function ShowLabel() {
// Note that the client ID might be different from the server side ID
document.getElementById('lblAge').style.display = 'inherit';
}
</script>
You need to set the Label Display style to none initially.
<asp:Label ID="lblAge" style="display: none;" runat="server" AssociatedControlID="age">age</asp:Label>
Try below code:
You need to set Visible property of controls to True or False according to your requirement. By default, all controls are visible on the screen whenever they are added on the page.You need to do following thing:
You need to remove TextMode="age" as there is not any supported textmode of type age.
Need to define id of control if you want to access a control server side in code behind. So define the ID of Label that you put corresponding to Age textbox.
By Default age label and textbox will not be visible by using below code:
<asp:Label ID="lblAge" runat="server" AssociatedControlID="age" Visible="false">age</asp:Label>
<asp:TextBox runat="server" ID="age" Width="240px" Visible="false"/>
Code behind:
After button click age label and the textbox will be visible by using below code:
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible = true;
age.Visible = true;
}
First add id to elements and set visible false
<asp:Label runat="server" AssociatedControlID="age" Visible="false" Id="lbl1">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" Visible="false" />
button click event set visible true
protected void Button1_Click(object sender, EventArgs e)
{
lbl1.Visible = True;
age.Visible = True;
}
this is the basic concept of asp.net. you can use visible property of the control.
your TextMode enumeration is wrong. there is no Age enumeration for Textbox.TextMode TextMode
<li>
<asp:Label runat="server" AssociatedControlID="age" id="lblAge">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
in code behind
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible=true;
age.Visible=true;
}
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Visible = false;
NameBox.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
NameLabel.Visible = true;
NameBox.Visible = true;
}