Send HTML Parameter to button click event in ASCX child control - c#

I'm trying to access some page HTML to use for an email in a Button_Click event.
I cannot set this content easily anywhere else at runtime (Such as in a TAG).
So I'm wondering if I can use JQuery to set a variable to .innerHtml(), and pass that in the button click. How would I go about doing this?

Something like this...
<div id="myDiv">
some content...
</div>
<asp:HiddenField ID="hdnHtml" ClientIDMode="Static" runat="server" />
<script type="text/javascript">
$(function () {
$('#hdnHtml').val($('#myDiv').html());
});
</script>

To add a hidden field to a form: fill its value using javascript/jQuery and submit the form via a Button_Click event.

Related

How to set the current tab/content on page postback

I have the following JSFiddle which changes the tab/content based on user selection: http://jsfiddle.net/sikni8/3d64w6gf/1/
HTML snippet sample:
<asp:Button ID="btnRegister" ClientIDMode="Static" runat="server" Text="Register" OnClick="btnRegister_Click" />
<!--<input type="submit" name="commit" value="Register" runat="server" />-->
The btnRegister button perform some action when clicked. When the page does a postback, how can I ensure the tab/content which was there is displayed.
For example, if I click on the Register button the page refreshes and brings the first tab/content to view. I would like to change it so that when Register button is clicked and the page reloads, it should remain with the same tab/content.
I can use sessionStorage but I would like to request some assistance.
Tried this but didn't work:
<script>
$(document).ready(function (e) {
localStorage.setItem("whichTab", $("#tabs nav ul li.tab-current"));
alert(localStorage.getItem("whichTab"));
});
</script>
I am using cbpFWTabs.js file to achieve the tab class switch which is in the fiddle.
I did something similar by using hidden field, where I stored the id/class of the current tab in hidden field and after postback I just displayed the current tab again.

Call ASP.net function / method from div onclick

I have a basic url redirect on a DIV's onclick.
<div onclick="location.href = 'page.aspx';">
I have a need to do some work server side, rather than just a redirect, when this Div is clicked.
How do I call one of my code behind functions from the DIV onclick? The DIV is the parent control inside a usercontrol. I suppose I can "fix" it by using a literal control and writing a query string into the onclick, but I would use that as a last resort as I don't want to use a query string.
Are there any other ways of catching a DIV click in code behind that I may have missed?
You can have a button whose display can be 'none'.
Handle click of Div on client side and then from there fire the Click of that button and handle the thinks Server Side on the Click Event of the button.
<asp:Button runat="server" id="btnHidden" style="display:none" onclick="btnHidden_OnClick" />
<div onclick="javascript:DivClicked(); return true;"></div>
<script>
function DivClicked()
{
var btnHidden = $('#<%= btnHidden.ClientID %>');
if(btnHidden != null)
{
btnHidden.click();
}
}
</script>

Store innerhtml by jQuery in asp.net?

I have
<div id="ulAndil" runat="server">
</div>
and button
<asp:Button ID="btnAssignJudToCourse" runat="server" Text="تاكيد "
CssClass="button" CausesValidation="false"
OnClientClick="javascript:getShape();"
OnClick="btnAssignJudToCourse_Click" Visible="false" />
At runtime I create a lot of sortable html controls and then click confirm
I want to save inner html of this div before postback of button in session or anything
Using jQuery so when user click the button jQuery take inner html of div and stores it in cookie session any thing to store it after postback I can retrieve it
Please I want the exact code for this jQuery or javascript method and how to call on button
Why don't you put the generated HTML in a hidden field and postback.
Say getShape() is your javascript function
function getShape()
{
//your validations applied
document.getElementById('hdnHTML').value = document.getElementById('ulAndil').value;
return true;
}
where hdnHTML is the hidden element of HTML.

How to call a javascript from master page?

I'm trying to call a javascript alert from a master page, where i got an update panel. Within that a button and a text box.
I need to call the alert on clicking the button in my master page. So far it seems not working. Please help.
Page.ClientScript.RegisterStartupScript
(this.GetType(), "alert", "invokeMeMaster();", true);
This is what i wrote in my button click. invokerMEMaster include just an alert message.I need to reload the page on the ok button click of the alert. How can i do that as well?
Update Panel clears javascript code when Postback so try to put this code on the header.
<script type="text/javascript">
$(document).ready(
function(){
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {//put your code here}
});
</script>
You need a ScriptManagerProxy, with the ScriptManager residing in your content page
If I may guess your problem (psychic debugging): When the update panel updates the javascript is not rendered of fired on your page.
Try to register your javascript like in the update panel:
ScriptManager.RegisterClientScriptBlock(Page,typeof(string),"JavaScriptCall",script.ToString(), false);
If I understand your question correctly you have a JS function in a content page that needs to be called from the master page?
What I would do is add a hidden input control and an alert function to your master page:
<input runat="server" id="hdnAlert" name="Alert" type="hidden" />
<script type="text/javascript" language="javascript">
function AlertMe(){ alert(document.getElementByID("hdnAlert").value); }
</script>
You could then change the value of the input control from a content page:
HtmlInputHidden hdnTemp = new HtmlInputHidden();
hdnTemp = (HtmlInputHidden)Master.FindControl("hdnAlert");
hdnTemp.Value = "Message To Alert";
Then just have the button in your master page call the "AlertMe" function located in the master page.

c# how to enable button ontextchange without posting back

so i have a lightbox in which pops up an aspx page with textboxes and two buttons (submit - disabled and cancel - enabled). I wanted to enable my submit button ontextchange. it works fine when opened separately (not as a lightbox) but when i let it run normally with the lightbox function everytime ontextchange gets triggered the whole page refreshes disabling the lightbox.
<asp:TextBox ID="textBox1" runat="server" OnTextChanged="OnTextChanged_AttributesEdited" autopostback="true">
protected void OnTextChanged_AttributesEdited(object sender, EventArgs e)
{
btnSubmit.Enabled = true;
}
now if i take out the "autopostback=true" it then will not trigger the the ontextchanged. was wondering if is it better if javascript will be the way to go for enabling the button or is there a way where i can prevent the postback when ontextchanged is triggered?
thanks a lot!
I think this would be a prime use for some jQuery in your application. Without posting back to the server for enabling / disabling a button, this would look a lot smoother, load faster and keep your current page state intact. An example might be as follows:
<script type="text/javascript">
$(document).ready(function() {
$("#textBox1").change(function() {
$("#btnSubmit").removeAttr("disabled");
});
});
</script>
Just put the above script tag in your HTML, just before closing the body tag.
An even better solution, however, would be to assign a specific CSS class to all the textboxes that should inherit that behaviour. Assuming that you assign a CSS class called "someCssClass" to all those textboxes, your script would then look something like this:
<script type="text/javascript">
$(document).ready(function() {
$("input.someCssClass").change(function() {
$("#btnSubmit").removeAttr("disabled");
});
});
</script>
I'd use jQuery as mentioned by #FarligOpptrenden, but if you don't have it and just want plain javascript, this should work.
Input within your lightbox:
<input type="text" id="textbox" onKeyUp="enableSubmitButton()" />
<input type="button" id="submitButton" value="Submit" />
<input type="button" id="cancelButton" value="Cancel" />
Javascript:
<script type="text/javascript">
function enableSubmitButton()
{
document.getElementById('submitButton').disabled = false;
document.getElementById('cancelButton').disabled = true;
}
</script>
You could also do your enabling/disabling buttons on load in javascript:
<script type="text/javascript">
window.onload = function () {
document.getElementById('submitButton').disabled = true;
document.getElementById('cancelButton').disabled = false;
}
</script>

Categories