I have textbox in asp.net where i am writting Address.
On linkbutton below i wanted to direct it to goole map.
I have below textbox:
<asp:TextBox ID="txtJobAddress" runat="server" TextMode="MultiLine" Width="95%" Height="20px"
onBlur="javascript:saveChanges('JobAddress');"></asp:TextBox>
Linkbutton as below:
<a href="http://maps.google.com/maps?q=<%=txtJobAddress.Text%>" target="_blank">
<img src="images/gmap_button.gif" alt="Map" />
</a>
but when its getting directed through anchor tag, its not catching "q" parameter.
Its giving as:
https://www.google.com/maps/preview?q=
why its not taking value of:
<%=txtJobAddress.Text%>
Please help me , how can i attach textbox string here in
http://maps.google.com/maps?q=<%=txtJobAddress.Text%>
as below
http://maps.google.com/maps?q=NewYork
The <%=txtJobAddress.Text%> will be empty on page load. Presumably your saveChanges method prompts an ajax call to save the "JobAddress"? If this is the case then a full page postback won't occur, and therefore the <%=txtJobAddress.Text%> isn't populated with the updated value as this would occur on the server. You'll want to populate the query string value client side
e.g. in your blur event you could add a call to a javascript function that updates your link:
onBlur="javascript:saveChanges('JobAddress'); updateLink()"
in your updateLink function you would update the link query string value
Related
I have a TextBox:
<asp:TextBox ID="textSearch" runat="server" Width="80" ForeColor="Black" />
and a Repeater with ImageButtons:
<asp:Panel ID="panRepeater" runat="server" ScrollBars="Vertical">
<asp:Repeater ID="Repeater" runat="server">
<ItemTemplate>
<asp:ImageButton ID="imgSearchResult" runat="server" ImageUrl='<%# Eval("ImageUrl") %>'/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
In code behind, I add the Images to the ImageButtons with this function:
private void LoadImages()
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images/ORAS"));
List<System.Web.UI.WebControls.Image> images = new List<System.Web.UI.WebControls.Image>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
image.ImageUrl = (String.Format("~/Images/ORAS/{0}", System.IO.Path.GetFileName(item)));
images.Add(image);
}
Repeater.DataSource = images;
Repeater.DataBind();
}
I want to be able to search and only display the wanted ImageButtons similar to this example:
I would type in 00 into the Search-TextBox and all ImageButtons, which contain 00 will be shown. The thing is that I'd have to do this after every KeyDown.
This is a complex scenario and can be implemented in two different ways.
Using ajax postback by hooking up a server-side text changed event of search textbox. In this server-side event, you would get records from database using the search text box text value. An example of a similar scenario using gridview can be found at this URL: Server-side Search
The second approach, which is more prefered is calling the server-side method to get records directly from JavaScript or jquery. This can be very quick and fast. An example of such a scenario using gridview can be found at this URL: Search using client-side code.
For the second option, you have to hookup client-side keyup event to get search text before making an ajax call to a web service method from jquery, and also only do a search if user has input a minimum number of characters like 2 or 3 or 4 or whatever you think is a good number. The web service method on server-side will return a JSON collection of the searched items for your repeater control. Finally, in the success event of jquery ajax call you would need to populate the repeater items with the search items.
A Suggestion
Also, as another suggestion to make it easier to implement the client-side solution you could create an html string in the web service method and then in success event of jquery ajax call just set the innerHTML of a div to this html, so in jquery just one line of code in success event would populate the repeater control. This would be easier than returning a JSON collection from web service and then in success event of jquery ajax call you would need to create/populate html into the repeater control using DOM manipulation which would need some jquery.
i'm working in a webform page and i want to pass data from the "Client-Side" (ASPX) a value returned by jquery event to the "code behind" (ASPX.CS).
Thanks.
You can save the value in a hidden field and access to it from the server:
or
Set value of hidden field in a form using jQuery's ".val()" doesn't work
Just set the value to an input field like normal. When the page posts to the code behind, the value will be resident.
Example
HTML Markup:
<asp:Textbox id="myTextbox" runat="server" ClientIDMode="Static"/>
jQuery:
$('#myTextbox').val('myVal');
Again, when the form is posted, the value will be sent..
This is a two-fer, and I've looked in a lot of places and could not put together a solution for this particular scenario.
I will have the user enter multiple values using a single text field, such as:
<div style="margin-left: 5px; display: block;">
<div id="divTemplateLine" style="display: none;">
x
</div>
</div>
Using Javascript, the Add button will keep cloning divTemplateLine and include the text within the parent node.
I got all that working fine.
Now, I'd like to read all these lines (salmon, soup, miso, japanese) on the postback.
I'm assuming at this point I'm in the realm of parsing HTML, since these div's I added are not "runat" server.
One answer could be using a server-run hidden value, where the Javascript will keep appending to it.. yes that's a good solution, but I'd like to see how I can parse out the HTML elements and nodes just as I did in Javascript, because my real scenario is more complicated than a single value, so a hidden value will not quite do.
Any input and/or judgment is welcome.
The solution is to keep two versions of your data, one that you show on the user, and one on a hidden input that you post back and get the data that user select and keep.
For example, when your user enter the 'salmon', you place it both on html render, and on an input hidden control with your way of serialize. When your user remove it, you remove it from your hidden control also.
At the end you going to have something like that, and you post it back and analyse it and save it on your database.
<input name="final_select" type="hidden" value="salmon;soup;miso;japanese" />
#BeemerGuy: take one text box and auto complete extender as follows:
<asp:TextBox ID="txtCustName" AutoPostBack="true" AutoComplete="off" runat="server"
OnTextChanged="txtCustName_TextChanged" />
<cc1:AutoCompleteExtender ID="ace1" TargetControlID="txtCustName" ServiceMethod="GetCustomers"
MinimumPrefixLength="1" OnClientItemSelected="ace1_itemSelected" FirstRowSelected="true"
runat="server" />
Auto Complete Extender has service method property which will be called when you enter text on text box and will add text box value in list view
<<****** Webmethod code is here ******>>
ListBox list = new ListBox();
[WebMethod]
public static DataSet GetCustomers(string prefixText)
{
list .Items.Add(New ListItem(prefixText, 0));
}
This code doesn't display the value, I don't know why?
I have server control:
<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll" />
in javascript function:
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
I enter text then click on button that call the javascript function, but the alert box doesn't display the entered text.
EDIT: when I initialize the text with Text="some text", it is displayed in alert, I want to enter text in client side and get the value of it in the Javascript function.
Thanks
Using label or textbox visible set false so it can access the value in JavaScript
Sol
1)
Make a div and set style="display:none;" so label is not display at UI(browser) but value can access in JavaScript.
This is because you server Control is called "txtTest" not "txtEventDescription"
change your javascript function to :
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
EDIT: ok, I see you've now changed the post to show the code and renamed the js control, so above is no longer relevant (for those who are confused by my answer) :-)
The problem is the Visible="false" - this control will not render into the client and will therefore not be accessible via javascript (as the HTML element does not exist client side)
So, hide the element using CSS and then call alert on it. Sample snippet
CSS
.hide-element {
display: none;
}
HTML Markup
<asp:TextBox ID="txtTest" runat="server"
Columns="23"
CssClass="white-scroll hide-element"
Rows="3"
TextMode="MultiLine"/>
JavaScript
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
This way you will definitely get an alert.
You alert is empty because you have not set the property Text for your asp:Textbox
Make it Visible="true" to your textbox and than test.
<asp:TextBox ID="txtTest" runat="server" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll" />
if "txtTest" textbox has visible="false" in that case its not render on html code on client machine and if it hasn't on client's html code then how javascript calls this textbox. Because when javascript search this textbox by its id it doesn't find and it gives an error.
you can assign any other custom attribute to the control i.e.
<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll"
clientID="myClientID" />
and then access control using jquery like
var txtVal = $('textbox[clientID=myClientID]').val();
hope it helps.
I'm learning C# and working on my first ASP.Net e-commerce website.
I have the following repeater:
<asp:Repeater ID="Cartridges" runat="server" OnItemCommand="Cartridges_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton>
</ItemTemplate>
However, when the code is rendered the repeater produces JavaScript to postback which is no good when populating a shopping cart if the user has JavaScript turned off!
<div class="wrap-cart">
<div class="cartbuy"><a id="ContentPlaceHolder1_Cartridges_buy_0" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Cartridges$ctl00$buy','')">Buy</a></div>
<div class="cartimg"><img src="pic/epson-comp-set50.jpg" alt="product" /></div>
<div class="carttext">
<p class="cartdesc"><span class="homeprinters">product</span></p>
<p class="cartprice">£x.xx</p>
</div>
All I want to do is to pass the cartID to a data table. Is there a way round it so JavaScript is not involved at all?
For completeness here's my code behind:
protected void Cartridges_ItemCommand(object source, RepeaterCommandEventArgs e)
{
cartidtest.Text = "added";
if (e.CommandName == "AddtoCart")
{
string varCartID = (e.CommandArgument).ToString();
//more code here
}
}
Sure, you can pass the selected ItemID via QueryString
First, Generate an anchor tag like this:
AddToCart
This may be accomplished in an ItemTemplate:
<a href='<%# "NextCheckoutStep.aspx?ItemID=" + Eval("ItemID").ToString() %>'>AddToCart</a>
Finally, in NextCheckoutStep.aspx, access:
Request.QueryString["ItemID"]
Notes
You can pass information back to the same page. Just replace NextCheckoutStep.aspx with CurrentCheckoutStep.aspx. You will just need to put some routing code into your Page Load (or init) handlers (with postback = false) to react to the various page states that are created by this method. Sometimes a state diagram is helpful in working with this design.
Make sure to 100% sanitize your QueryString-Reads, because malicious users will attempt to put nasty values in the query string.
You don't have to use the QueryString, you can also get by with the HTTP POST collection; however, you need to sanitize here too, because you can never trust inputs coming from the client.
I would have a link which includes the item id in a querystring. I would expect the user's cartId to be stored in a cookie or in a session variable.
<asp:Repeater ID="Cartridges" runat="server">
<ItemTemplate>
<p>Buy</p>
</ItemTemplate>
</asp:Repeater>
A LinkButton renders as a link that immediately forces a submit, so it needs javascript.
You will have to use a plain HyperLink to render a url like mypage.aspx?id=cartid.
You can use instead of LinkButton