when my button is clicked I fire the javascript to get the session..
But value of session not updated...
alert('<%= Session["file"]%>');
It won't be up to date if its changed after the page is rendered.
You might want to look at page methods (an ajax system) or another ajax approach.
Any inline code once rendered to the page will not change, which is a normal behavior.
use a Hidden field instead.
mark-up:
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
Session["file"] = "Data Here";
HiddenField1.Value = Session["file"].ToString();
}
javascript:
alert(document.getElementById('<%= HiddenField1.ClientID %>').value);
Related
I want to call a onclick function of server side like below(i.e in test.aspx.cs)
protected void linkTest_Click(object sender, EventArgs e)
{
string a=HiddenField1.value;
Response.Write("<script>alert("+a +" is called)</script>");
}
And i am passing the test.html as src in Iframe(i.e in test.aspx)
<asp:HiddenField ID="HiddenField1" runat="server" />
<iframe id="Iframe" src="../test.html" ></iframe>
My HTML control is like below and it is inside test.html
<a id="linkTest" runat="server" onclick="linkTest_Click">Test</a>
Now,I want to call a server side onClick event(linkTest_Click) on this <a> which is inside iframe control which is inside test.aspx
You cannot call a server side function directly. But with some simple jQuery it is possible.
On the page containing the iframe place the following code
<iframe src="index.html" width="200" height="100" frameborder="1"></iframe>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" OnClick="Button1_Click" />
<script type="text/javascript">
function executeIframeCode() {
$('#Button1').click();
}
</script>
Then in the iframe put a button or whatever you want and call the function executeIframeCode in the parent.
<button onclick="parent.executeIframeCode()" type="button">
Execute Parent Code
</button>
Parent code behind code
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Called from Iframe!";
}
This only works if the iframe page is on the same domain.
This is my markup:
<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtEmail" AutoPostBack="true" OnTextChanged="txtEmail_TextChanged" />
<asp:Panel runat="server" Visible="false" ID="pnl">
<asp:TextBox runat="server" ID="txtUname" AutoPostBack="true" OnTextChanged="txtUname_TextChanged" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
and this is the code behind:
and these are the event handlers in the code behind:
protected void txtEmail_TextChanged(object sender, EventArgs e)
{
pnl.Visible = true;
updPnl.Update();
txtUname.Focus();
}
protected void txtUname_TextChanged(object sender, EventArgs e)
{
}
If I press enter on txtEmail textbox it works fine in all browsers and the focus comes to txtUName textbox except Chrome. On debugging I found that is happening because Chrome makes Ajax request twice. Is there anyway to fix this Chrome bug?
Note that it works fine in Chrome too if I use Tab instead of enter. However, I need to fix this Chrome bug of Enter too.
you may enter key trigger a post to the form,
you should prevent the submit action on enter (so enter key and tab key would have the same behavior)
you can do it with JavaScript (onkeydown) for instance.
however, I think update panel here is an overkill,
why not to use only JavaScript on the client side?
$("#txtEmail").change(function() {
var email = $(this).val();
// now you can do whatever you want with the value.
});
I am trying to obtain the return of a Javascript function that I call ont server side using ScriptManager.RegisterClientScriptBlock .
Currently, my javascript function save the value in a HiddenField that is contained in an UpdatePanel. When I call the method , I run the ScriptManager.RegisterClientScriptBlock and after that get the value of the HiddenField , but always returns me the value of the previous call. Here I show the code:
My User Control ASPX Side:
<script>
var num = 0;
function getReturn() {
num = num + 1;
var hr= document.getElementById('<%= hdf.ClientID %>');
hRetorno.value = num;
}
</script>
...
<asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:HiddenField ID="hdf" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
My User Control Server Side Code:
public String GetReturn()
{
return MyControl.jsGetReturn(this.Page, this.hdf);
}
private static String jsGetReturn(Page page, HiddenField hid)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), "key", "getReturn();", true);
return hid.Value;
}
Index Page ASPX Side:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div style="width:720px; height:480px;">
<uc1:MyControl runat="server" id="MyControl1"/>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" />
<asp:TextBox ID="txt" runat="server" ></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
Index Page Server Side Code:
protected void btn_Click(object sender, EventArgs e)
{
txt.Text = MyControl1.GetReturn();
}
You can not do these things simultaneously.
Set value of hidden field from server side code using javascript via RegisterClientScriptBlock or RegisterStartupScript.
And retrieve that hidden filed value in server side code at the same time.
This is because, (changed) hidden field value is available to server-side code only when there is postback, when you set the value from server-side, there is no postback happening, that is why you are always getting previous value , i.e. that old value which is posted back to page.
EDIT
When you invoke RegisterClientScriptBlock or RegisterStartupScript, it won't make JS call instantly, rather it just append a javascript call before or after <form.. tag based on what you used and they are called on document load, so that means in jsGetReturn when you call RegisterStartupScript, it will set value of hidden field in document load, - hid.Value will not have updated value, as it is yet to be incremented via document load.
I have a aspx page (default.aspx), inside which I am loading a user control (tree.ascx).
inside the tree.ascx there is a hidden field.
<asp:HiddenField ID="HiddenField1" runat="server"/>
I am assigning a value to the hidden field using javascript.
document.getElementById('<%=HiddenField1.ClientID%>').value = "some text here";
alert(document.getElementById('<%=HiddenField1.ClientID%>').value);
document.getElementById('form1').submit();
The alert displays the value absolutely fine. which means the value gets inserted in the hidden field properly.
But when I am posting back to server and checking the value, it is always null.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// do something.
}
else
{
string str = this.HiddenField1.Value;
}
}
My code is always getting a empty string here. somehow the postback is erasing the value from the hidden field.
What could be the reason?
Try using below syntax. It works for me even after postback.
ASPX code
<asp:HiddenField runat="server" ID="aspHiddenField" />
<input type="hidden" id="inputHidden" value='<%= aspHiddenField.ClientID %>' />
JavaScript Code
var inputHidden = document.getElementById('inputHidden');
$("#" + inputHidden.value).val("some text");
Code Behind
if (!string.IsNullOrEmpty(aspHiddenField.Value))
{
//Your code goes here
}
Check if your control is within a master page, if it is, then you need to access it like that, 1st Master Page->within master page look for the control's value, it will work surely.
Place your hidden field in update panel like :
<asp:UpdatePanel ID="UpnlHidden" runat="server">
<ContentTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
This will work for you :-)
I am basically trying to do what this article says.
link text I am able to postback, but my handler is not getting hit. Any ideas?
Code Behind
protected void Page_Init(object sender, EventArgs e)
{
WireEvents();
}
private void WireEvents()
{
btnAuthOk.Click += new EventHandler(btnAuthOk_Click);
btnAuthOk.OnClientClick = string.Format("fnClickOK('{0}','{1}')", btnAuthOk.ClientID, string.Empty);
}
private void btnAuthOk_Click(object sender, EventArgs e)
{
DoSomeCodeHere();
}
Javascript & HTML
function fnClickOK(sender, e) {
__doPostBack(sender, e);
}
<p>To allow this payment to be processed, enter an authorized User ID and Password</p>
<p>User ID: <asp:TextBox runat="server" ID="txtAuthUser" CssClass="underlinedTextBox" Columns="8" />
<asp:Literal runat="server" ID="spauth" Text=" " />
Password : <asp:TextBox runat="server" ID="txtAuthPass" TextMode="Password" CssClass="underlinedTextBox" Columns="10" />
</p>
<asp:Button runat="server" ID="btnAuthOk" Text="Submit" CssClass="popupAuthButton" UseSubmitBehavior="false" />
</asp:Panel>
<cc1:ModalPopupExtender ID="authPE" runat="server" PopupControlID="popupAuth"
OkControlID="btnAuthOk" TargetControlID="hdnPopupTarget" BackgroundCssClass="modalBackground" />
Thanks for any help here...
Cheers,
~ck
PS Stack is mucking my html a bit as I don't know how to properly post HTML. Is someone can edit and fix, I appreciate it. :)
I found that setting UseSubmitBehavior="false" actually allows me to hit my code.
If you just want your OK button to do a postback, simply remove the OkControlID attribute from your ModalPopupExtender markup.
I know, I know - that sounds ridiculous. But when you specify OK and Cancel button ID's to the MPE, it actually disables them from posting back on the client side.