This is what I have implemented, for further code, how to send the text of the text box to the server to store in variable or database without post back?
It can be done by using Ajax and update plane, but I would like to implement it using a JavaScript script.
<div id="CommentID" style=" width:30%; height:30%">
<asp:Button ID="Button1" runat="server"
Text="Comment"
OnClientClick="visibleDiv('id1'); return false;" />
<div id="id1" runat="server" style="visibility: hidden; background-color:Green; width:100%; height:100%">
<asp:TextBox ID="TextBox1" runat="server"
AutoCompleteType="Disabled" Rows="3"
TextMode="MultiLine" Width="98%">
</asp:TextBox>
<asp:Button ID="Button2" runat="server"
Text="Post"
onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server"
Text="Cancel"
OnClientClick="visibleDiv('id1'); return false;" />
</div>
</div>
Skip jQuery, just add use a PageMethod
This will expose a static server side method in javascript and facilitate all the Ajax stuff
Add this to your code behind
[System.Web.Services.WebMethod]
public static string SendString( string text ) {
// store the string in the database
return text;
}
Add a scriptmanager to your markup
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
...
And now you can call SendString from javascript using
PageMethods.SendString("mystring", function(text){
alert("wooot! " + text);
});
Also note that you can use strongly typed parameters and return values.
ASP.NET AJAX will automatically create client side classes in javascript to emulate the server side ones, and will automatically serialize and deserialize them :)
I suggest you to use jQuery:
<asp:Button ID="Button1" runat="server"
Text="Comment"
OnClientClick="$.post("you_url", {text: $("#TextBox1").val()});"/>
See http://api.jquery.com/jQuery.post/ for details.
Related
beginner here looking for a good way to post data from a textbox to another page as a query string
<div class="col-md-4">
<h2>Find a Player</h2>
<p>
<img alt="Find a Player" src="images/user.png" style="width: 64px; height: 64px" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Submit" />
</p>
<p>
Just trying to get it to post to:
http:\website\depth_chart?recid=griffey
where griffey is the data that was put into the textbox.
You can use the property PostBackUrl:
<asp:Button ID="Button2" runat="server" Text="Submit" PostBackUrl="http:\website\depth_chart?recid=griffey" />
where the value for PostBackUrl should be changed on the client while the users write in the textbox using the onkeyup or onkeydown property:
<asp:TextBox ID="TextBox1" onkeyup="alert('TextBox1 onkeyup fired');"
The issue you could face in mixing the javascript with the back-end asp controls is retrieving the IDs, you could solve by creating the js in the code-behind:
string changeScript = "<script language='javascript'> function SomeValueChanged() {" +
"document.getElementById('" + MyTextBox.ClientID +
"').value = 'Some values may have been changed.'; }</script>";
// Add the JavaScript code to the page.
if (!ClientScript.IsClientScriptBlockRegistered("SomeValueChanged"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "SomeValueChanged", changeScript);
}
BUT if you're trying to apply this logic to your app, forcing how an ASP.NET WebForm project should work, I can tell you that you're going in the wrong direction. It feels like there is a design issue.
I am using Visual Studio 2015 to create a ASP.NET web site. I have a DIV that is hidden by default and I am trying to display it if the user checks a check box. I am wondering if there is a way using C# to immediately display the DIV as soon as the user checks the box. I can't seem to find a way to recognize the change in state without the form being first submitted. I can easily achieve this using JavaScript however this is for a uni assignment and requires functionality to be implemented in C#.
This is the HTML for the checkBox:
<form id="form1" runat="server">
Limit Stations To My Location<asp:CheckBox ID="limitOptions" runat="server" onClick="toggleOptions()" />
<br /><br />
<div id="locationOptions" runat="server" hidden="hidden">
Radius (KM)<asp:TextBox ID="radius" runat="server" Text="100"></asp:TextBox>
<asp:Button ID="updateList" runat="server" Text="Button" OnClick="updateList_Click"/>
</div>
Any help would be greatly appreciated.
You can do this with C# in code behind. Add a OnCheckedChanged event to the CheckBox and set the AutoPostBack to true and handle the change. Note that a Panel becomes a div in HTML.
<asp:CheckBox ID="limitOptions" runat="server" OnCheckedChanged="limitOptions_CheckedChanged" AutoPostBack="true" />
<asp:Panel ID="locationOptions" runat="server" Visible="false">
Radius (KM) <asp:TextBox ID="radius" runat="server" Text="100"></asp:TextBox>
</asp:Panel>
However this causes a PostBack, so just using JavaScript could be a better option.
<asp:CheckBox ID="limitOptions" runat="server" onclick="toggleOptions()" />
<div id="locationOptions" runat="server" style="display: none">
Radius (KM)<asp:TextBox ID="radius" runat="server" Text="100"></asp:TextBox>
</div>
<script type="text/javascript">
function toggleOptions() {
var id = '#<% =locationOptions.ClientID %>';
if ($(id).css('display') == 'none') {
$(id).slideDown('fast', function () { });
} else {
$(id).slideUp('fast', function () { });
}
}
</script>
I have a asp:Button that has an onclick function. Is there a way to eliminate the refresh that occurs?
Current HTML containing button.
<div id="user" class="font">
<div id="userPanel">
<asp:TextBox ID="txtSearch" runat="server" Width="400px" align="left"></asp:TextBox>
<asp:Button ID="searchbutton" text="enter" runat="server"
onclick="searchbutton_Click" />
<asp:Label ID="test" runat="server"></asp:Label>
</div>
<asp:UpdatePanel ID="userinfoupdatepanel" runat="server">
<ContentTemplate>
<div class="topcontent">
<ul>...</ul>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The button click then calls a function that fills the UL content
You can use OnClientClick to bind a Javascript function to handle the click in the browser. If you return false from that fumction, the postback doesn't happen.
You can also add client events using Attributes collection.
searchbutton.Attributes.Add("onclick", "alert('Test')");
searchbutton.Attributes.Add("onclick", "return fnSomeThing(this);");
Is there a way I can display an error message in a popup box? I'm using Asp.Net (C#), and on our webpage, if an order goes throug incorrectly we display an error message. Now, we would like to be able to do so using some sort of pop-up method (maybe Ajax?) - it should be able to take a string value to display the error message.
For a simple approach, you can have a script block that contains alert("your error message"). If you want the popup to be styled as the rest of your website then you could render your error message into a div element and use a jQuery dialog to display it as a modal dialog within your page.
I have used Ajax to accomplish this myself.
Using the ModalPopupExtender and setting the PopupControlID to an Asp Panel, I usually put this into a User Control so it can be easily used through a website.
However below is a snippet of the asp.net code
<div class="modalPopupAlign">
<asp:LinkButton ID="lnkConfirm" Style="display: none;" CausesValidation="false" runat="server" PostBackUrl="#">Confirm</asp:LinkButton>
<ajax:ModalPopupExtender ID="lnkConfirm_ModalPopupExtender" runat="server" TargetControlID="lnkConfirm" PopupControlID="pnlConfirmation" BackgroundCssClass="modalBackground" DropShadow="true" RepositionMode="None">
</ajax:ModalPopupExtender>
<div id="pnlConfirmation" class="modalPopup" style="display: none;">
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="modalPopupContainerAlign">
<div>
<asp:Image ID="imgIcon" CssClass="modalPopupImage" runat="server" />
<asp:Label ID="lblMessage" CssClass="modalPopupMessage" runat="server"></asp:Label>
<div class="modalPopupTextbox"><asp:TextBox ID="txtValue" Width="200px" MaxLength="100" Visible="false" runat="server"></asp:TextBox></div>
<asp:Button ID="btnAction" runat="server" CausesValidation="false" CssClass="defaultButton" Text="OK" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
It does take some time to get it working properly as I have had a few errors with the script manager, which I currently have setup in a master page.
Just might give you a direction to head for, and the CSS can help shape and colour the message box.
Although this is a complex way in some respect but has many uses.
You can do this with simple javascript like this...
alert("my error message");
Here is more info on using javascript
I have a repeater control in which i have loaded questions. I want to display a textbox and a button when some one click the question and answer that question with out posting back the page. the textbox and button get hide when the user post the answer
U could do this with ajax using an UpdatePanel.
The javascript will be generated for you by the ASP.NET engine.
You can learn more on the UpdatePanel here.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<span onclick="ShowTextBox(<%# Container.ItemIndex %>)"><%# Eval("Question") %> </span>
<div style="display:none" id='<%# "dv_"+Container.ItemIndex %>'>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button" />
</div>
</ItemTemplate>
</asp:Repeater>
AND Add some script
`<script type="text/javascript">
function ShowTextBox(index) {
$("#dv_" + index).show();
}
</script>`
Remember you should include the jquery file in the head.
download from jquery.com