JavaScript display property - javascript

In my form I have a textbox and a calendar along with other controls:
<asp:TextBox ID="TextBox2" runat="server" onfocus="CalOpen()" asp:TextBox>
<asp:Calendar ID="Calendar1" runat="server" style="display:none;"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<script type="text/javascript">
function CalOpen()
{
var cal = document.getElementById('<%=Calendar1.ClientID%>');
cal.style.display='block';
}
</script>
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox2.Text = Calendar1.SelectedDate.ToLongDateString();
Calendar1.Visible = false;
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Calendar1.Visible = true;
}
For the first time it worked fine,
but, second time when I click TextBox2, that is, after selecting date for the first time the
browser is throwing error "object required".
I am unable to know where I went wrong.
Please help me in making my code correct.

When you write Calendar1.Visible = false; in server-side code, it doesn't render the calendar at all. Therefore, there is no calendar element for the Javascript to show.
Instead, you should make a CSS class that applies display: none to the calendar, and set the CssClass property to that class on the server.
For example:
<style type="text/css">
.Hidden {
display: none;
}
</style>
<asp:Calendar ID="Calendar1" runat="server" CssClass="Hidden"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox2.Text = Calendar1.SelectedDate.ToLongDateString();
Calendar1.CssClass = "Hidden";
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Calendar1.CssClass = "";
}

Related

Set and get session variable on callback

I am using Devexpress controls in asp.net c#.
I have a GridView with a CustomButtonColumn. When I click on that button it performs a callback and gets the row key. There I set a Session variable with the row key.
On Page2 I want to use that Session to bind a parameter in a XtraReport.
[C#]
Page1
<script type="text/javascript">
function OnClick(s, e) {
if (e.buttonID == "SF") {
//Session["parameter_id"] = e.visibleIndex;
// gridAdmin.GetRowValues(gridAdmin.GetFocusedRowIndex(), 'ID', OnGetRowValues);
sf.PerformCallback(e.visibleIndex);
window.location = "PrintView.aspx";
}
}
</script>
<dx:ASPxPopupControl ID="ASPxPopupControl3" runat="server" CloseAction="CloseButton" ClientInstanceName="pcSF"
AllowDragging="True" PopupAnimationType="None" Modal="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
CssClass="panel1" Style="font-size: 20px" CloseOnEscape="true" HeaderText="SF">
<HeaderStyle BackColor="#FF8000" Font-Bold="True" HorizontalAlign="Center" />
<ContentCollection>
<dx:PopupControlContentControl>
<dx:ASPxCallbackPanel ID="pnlSF" runat="server" BackColor="White" ClientInstanceName="sf" OnCallback="pnlSF_Callback" >
<PanelCollection>
<dx:PanelContent>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
</dx:PopupControlContentControl>
</ContentCollection>
</dx:ASPxPopupControl>
protected void pnlSF_Callback(object sender, CallbackEventArgsBase e)
{
Session["parameter_id"] = GridViewAdmin.GetRowValues(Convert.ToInt32(e.Parameter), "ID").ToString();
// Response.Redirect("PrintView.aspx");
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
XtraReport1 repTest = new XtraReport1();
repTest.RequestParameters = false;
repTest.parameter1.Value = Session["parameter_id"];
repTest.parameter1.Visible = false;
repTest.CreateDocument();
this.ASPxDocumentViewer1.Report = repTest;
this.ASPxDocumentViewer1.DataBind();
}
When I click on that custom button from gridview and loads page2 it appears this error.
Why?
I saw that on debug when that error appears the callback is not done. It does not enter in pnlSF_Callback() function.

session cookie asp.net c#

please need help . actully i want to pass the info from page 1 to 2 using cookie but every i click buttom add to set a new cookie ....no just set the same nae and modify value . and read all cookie as list i dont know if u will understaidn what i want !!
here my code
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie Cookie = new HttpCookie("result");
Cookie["text1"] = TextBox1.Text;
Cookie["text2"] = TextBox2.Text;
Cookie.Expires = DateTime.Now.AddDays(2);
Response.Cookies.Add(Cookie);
Response.Redirect("WebForm2.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie Cookie = Request.Cookies["result"];
if (Cookie != null)
{
Ldonate.Text = Cookie["text1"] ;
Litem.Text = Cookie["text2"] ;
}
Ldonate.Text = Request.Cookies["result"]["text1"];
Litem.Text = Request.Cookies["result"]["text2"];
this is how you can access to cookies

Sending ASP HiddenField Value through another ASP Control

I have a asp.net site in which I have two files that need to talk to one another.
Below is a snippet of code from my footer.ascx file. I need to send a string to the MobileAd.ascx.cs file. Below is my relevant code from each file.
I believe everything is set up right I just have no clue how to pass the value properly. The value that is not being sent properly is SendA.value
Here is a snippet from footer.ascx
<%# Register TagPrefix="PSG" TagName="MobileAd" Src="~/MobileAd.ascx" %>
<asp:HiddenField runat="server" ID="SendA" value="" />
<script>
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ||
(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.platform)))
{
document.getElementById('<%=SendA.ClientID%>').value = "mobile";
}
else
{
document.getElementById('<%=SendA.ClientID%>').value = "other";
}
</script>
<div class="bottom" align="center">
<PSG:MobileAd ID="MobileAd" runat="server" AdType = <%=SendA.value%> />
</div>
Here is the receiving end at MobileAd.ascx.cs
private string _AdType;
public string AdType
{
set
{
this._AdType = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string html = null;
if (!string.IsNullOrEmpty(_AdType))
{
if (_AdType == "mobile")
{
html = "Mobile Ad Code";
}
else
{
html = "Tablet or Desktop Ad Code";
}
divHtml.InnerHtml = html;
}
You are detecting the user-agent with javascript. But, being a server control, the MobileAd.ascx got executed before javascript could execute. You should do it on server-side by checking Request.UserAgent or Request.Browser.IsMobileDevice. If the sole purpose of the property AdType is to just hold user-agent type, you can remove it and try modifying your Page_Load method like this:
protected void Page_Load(object sender, EventArgs e)
{
string html = null;
if (Request.Browser.IsMobileDevice)
{
html = "Mobile Ad Code";
}
else
{
html = "Tablet or Desktop Ad Code";
}
divHtml.InnerHtml = html;
}

Cannot get document.GetElementById working right

I am trying to call a javascript function from onClick event of an asp server button. I have this button and a label in an update panel.
In aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server"
style="z-index: 1; left: 49px; top: 119px; position: absolute; height: 26px;" Text="Button"
onclick="Button1_Click"/>
<asp:Label ID="Label1" runat="server" Text="l1"
style="position:absolute; top: 123px; left: 127px;"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, typeof(Button), "me", "me()", true);
}
Javascript in aspx:
<script type="text/javascript">
function me()
{
document.getElementById('<%= Label1.ClientID %>').value="clicked";
}
</script>
All I am trying to do is on button click the label's value should be changed to 'clicked', which isn't happening and no error as well in firebug. Where am I wrong in the me( )?
P.S: I am doing this to learn calling a javascript function from code behind. So any optimizations to the code are also welcome. (I actually have much lot of code to implement in the JS function later so appropriate suggestions please)
As far as I remember the Label control is rendered as a <div> or <span> (can't remember exactly), not a text input field. So you should be setting its innerHTML property not the value:
document.getElementById('<%= Label1.ClientID %>').innerHTML = 'clicked';
or simply do it on the server side instead of registering clientside callbacks:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "clicked";
}
UPDATE:
As requested in the comments section here's how you could pass parameters to the clientside callback from the server:
protected void Button1_Click(object sender, EventArgs e)
{
string param1 = ...
double param2 = ...
int param3 = ...
var serializer = new JavaScriptSerializer();
ScriptManager.RegisterClientScriptBlock(
this,
typeof(Button),
"me",
string.Format("me({0})", serializer.Serialize(new { param1 = param1, param2 = param2, param3 = param3 })),
true
);
}
and your callback:
function me(values) {
// you could use values.param1, values.param2, values.param3, ... here
}

GridView Row Fading Effect

EDIT:
here is what its generating at runtime after i debug the code
<script type='text/javascript'>
$('#ctl00_ContentPlaceHolder1_tabControl_gv_ctl03').show().fadeIn(8000).fadeOut(90000)
</script>
ere is what i am looking for...
how do i highlight the gridview control row after i am done updating with the row?
<asp:GridView runat="server" CssClass="DataWebControlStyle">
<AlternatingRowStyle CssClass="AlternatingRowStyle" />
<RowStyle CssClass="RowStyle" /> <
HeaderStyle CssClass="HeaderStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
</asp:GridView>
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//update....
}
Untested, but should be something like this:
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridViewName.Rows[e.RowIndex];
var sb = new StringBuilder();
sb.Append(#"<script type='text/javascript'>");
sb.Append(string.Format(#"$('#{0}').hide(200)", row.ClientId)); // hide or show here
sb.Append(#"</script>");
ScriptManager.RegisterStartupScript(GridViewName, this.GetType(), "showhide", sb.ToString(), false);
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridViewName.Rows[e.RowIndex];
row.CssClass = "some class that has background-color set";
}
This should (untested as well) work as long as you have not explicitly set each cells' background color.
Note: I realize you specified jQuery but since you posted a server-side function I figured this was easier.

Categories

Resources