.ascx user control button_click is not firing Sitefinity C# - c#

I have come across this weird problem where my button click event is not firing. I have tried almost all the possiblities but still no luck. googling for couple of hours now but still no help. Some ppl experienced exactly the same problem on different forums but no specific answer. I tried the button_click event and also tried registring an event handler, none of them work.
In my scenario I have a sitefinity5 custom module where I am showing employee information along with employee data. on employee picture click i am displaying a JQUERY dialog where i have this button. When application load, it fires the page_load of code behind file and also when i click on employee picture at first, it fires the page_load but dont fire button_click ever. Any subsequent click on picture dont even fire the Page_Load.
Anyone's help will really really be appreciated. Following is my code snippet.
<%# Control Language="C#" AutoEventWireup="true" Inherits="SitefinityWebApp.SfCtrlPresentation.OpenAccessDataProvider_a4a794260c0b4440b466f75d11146db8" Codebehind="OpenAccessDataProvider,a4a794260c0b4440b466f75d11146db8.ascx.cs" %>
<%# Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
<%# Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
<%# Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
<%# Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Fields" Assembly="Telerik.Sitefinity" %>
<%# Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%--<%# Register TagPrefix="jq" Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI" %>--%>
<telerik:RadListView ID="dynamicContentListView" ItemPlaceholderID="ItemsContainer" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
<LayoutTemplate>
<ul class="sfitemsList sfitemsListTitleDateTmb">
<asp:PlaceHolder ID="ItemsContainer" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="sfitem sfClearfix">
<h2 class="sfitemTitle">
<sf:DetailsViewHyperLink ID="DetailsViewHyperLink" TextDataField="Title" runat="server" />
</h2>
<sf:AssetsField ID="AssetsField1" runat="server" DataFieldName="Picture" />
<sf:SitefinityLabel ID="SitefinityLabel1" runat="server" Text='<%# Eval("Designation")%>' WrapperTagName="div" HideIfNoText="true" CssClass="sfitemShortTxt" />
<sf:SitefinityLabel ID="SitefinityLabel2" runat="server" Text='<%# Eval("CompanyName")%>' WrapperTagName="div" HideIfNoText="true" CssClass="sfitemShortTxt" />
<sf:AssetsField ID="AssetsField2" runat="server" DataFieldName="Documents"/>
</li>
</ItemTemplate>
</telerik:RadListView>
<sf:Pager id="pager" runat="server"></sf:Pager>
<sf:ResourceLinks ID="resourcesLinks" runat="server">
<sf:ResourceFile JavaScriptLibrary="JQuery" />
</sf:ResourceLinks>
<div class="dialogTest"> <br />
<br />
Please enter your email address: <input type="text" name="emailAddress" style="width: 300px;" /><br />
<br />
<asp:TextBox ID="txtBox" runat="server"></asp:TextBox> <br/>
<asp:LinkButton Runat="server" ID="btnSubmit" Text="Submit" /> <br/>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False"
onclick="Button1_Click" />
<%--<button name="btnSubmit" onclick="btnSubmit_Click" >Submit</button>--%>
</div>
<script type="text/javascript">
$j = jQuery.noConflict();
$j(document).ready(function () {
$j(".sfClearfix .sfimageWrp img").click(function () {
$j(".dialogTest").addClass("open");
//return $j(this).attr("src");
});
});
</script>
//.cs file
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Modules.Events;
using Telerik.Sitefinity.Events.Model;
namespace SitefinityWebApp.SfCtrlPresentation
{
public partial class OpenAccessDataProvider_a4a794260c0b4440b466f75d11146db8 : System.Web.UI.UserControl
{
//protected override void OnLoad(EventArgs e)
//{
// base.OnLoad(e);
//}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
btnSubmit.Click+= new EventHandler(btnSubmit_Click);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}

is this for a user control on a public front end page? go into the page properties (in the Action menu under the Pages list in the admin) and make sure to check the box to enable ViewState
By default, Sitefinity disables viewstate to improve performance.
Hope this is helpful!

Related

Is there a simple way to call a method used to change display from a parent control?

I am trying to create a page using Ajax Tabs and user controls. The .aspx page contains a reference to a default control
<%# Register src="~/Controls/DefaultControl.ascx" tagname="DefaultControl" tagprefix="uc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<uc1:DefaultControl ID="DefaultControl1" runat="server" />
<%--<uc2:CorrespondenceControl ID="CorrespondenceControl" runat="server" />--%>
</asp:Content>
And the DefaultControl.ascx is using Ajax Tabs, one of which contains a child control within an Update Panel
asp:TabPanel ID="tbpnl2" runat="server" HeaderText="Tab With GridView with select buttons" Visible="True">
<ContentTemplate>
<asp:UpdatePanel ID="updpnl2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc2:Control1 ID="Control1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
The DefaultControl holds a method in the code behind page which is successfully called directly from other tabs (with the markup contained directly in DefaultControl.ascx) on the DefaultControl.ascx page to change the display when Select is clicked on a gridview -
public void ShowPage()
{
gv1.DataBind();
fv1.DataBind();
tbpnl1.Visible = true; //show details tab
tbpnl2.Visible = true;
tab1.ActiveTabIndex = 1; //set details tab as current tab
txt.Text = String.Empty;
updPnl1.Update();
}
I am trying to call this method from the child Control1 when Select on a gridview is selected there, but obviously none of the elements referenced are in Control1.
I have been searching for a way to be able to use the existing method and have seen a number of suggestions including Interfaces, references like ((DefaultControl)this.DefaultControl).ShowPage(); on the code behind Control1
But as I am just starting to program I have no idea how to implement any of these solutions or what the syntax should be to get them to work.
Is there a simple, even if dirty, way to use the method from a parent control in a child control contained in an Ajax tab?
Not sure if this is what you are looking for... Below example shows calling of direct UserControl and nested UserControl method's from Web page
Default.aspx
<%# Register TagPrefix="uc" TagName="WebUserControl" Src="WebUserControl.ascx" %>
<%# Register TagPrefix="uc2" TagName="WebUserControl2" Src="WebUserControl2.ascx" %>
<form runat="server" id="form1">
<uc:WebUserControl ID="control1" runat="server" />
<hr />
<h4>
At Default.aspx</h4>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Call the function" />
</form>
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
control1.CallMe();
var control2 = (WebUserControl2)control1.FindControl("control2");
control2.CallMe2();
}
WebUserControl.ascx
<%# Register TagPrefix="uc2" TagName="WebUserControl2" Src="WebUserControl2.ascx" %>
<div runat="server">
<h3>
WebUserControl</h3>
<asp:Label ID="lbl1" Text="I am ready at WebUserControl" runat="server"></asp:Label>
<div runat="server" id="toAdd" style="color: Red;">
</div>
</div>
<hr />
<uc2:WebUserControl2 ID="control2" runat="server" />
WebUserControl.ascx.cs
public void CallMe()
{
Label lbl = new Label();
lbl.Text = "I am at WebUserControl";
toAdd.Controls.Add(lbl);
}
WebUserControl2.ascx
<div runat="server">
<h3>
WebUserControl2</h3>
<asp:Label ID="lbl1" Text="I am ready at WebUserControl2" runat="server"></asp:Label>
<div runat="server" id="toAdd" style="color: Red;">
</div>
</div>
WebUserControl2.ascx.cs
public void CallMe2()
{
Label lbl = new Label();
lbl.Text = "I am at WebUserControl2";
toAdd.Controls.Add(lbl);
}
Hope it helps someone...!!

How can I recapture form data?

I am attempting to recapture some form values after they have been posted, but am having quite a bit of difficulty. My form has 2 fields:
<strong>Username:</strong> <asp:TextBox ID="txtUsername" runat="server" Width="200px" /><br /><br />
<strong>Password:</strong> <asp:TextBox ID="txtPassword" runat="server" Width="200px" TextMode="Password" />
And in the code behind, I have tried to capture the username, but keep coming up empty handed. Here is my current implementation:
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "POST")
{
txtUsername.Text = Request.Form["txtUsername"];
}
}
What am I missing?
your code works perfectly fine for me:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" id="MainHTML" runat="server">
<head runat="server">
<title></title>
<script type="text/javascript">
</script>
</head>
<body>
<form runat="server">
<strong>Username:</strong> <asp:TextBox ID="txtUsername" runat="server" Width="200px" /><br /><br />
<strong>Password:</strong> <asp:TextBox ID="txtPassword" runat="server" Width="200px" TextMode="Password" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "POST")
{
txtUsername.Text = Request.Form["txtUsername"];
}
}
}
Without seeing the rest of the .aspx page, there's nothing glaringly obviously wrong there, but:
are the TextBoxes within a form with runat="server"?
have you run a trace to see what the Form variables, if any, contain?
are you sure the page is being submitted, and not just refreshed?
have you tried a breakpoint to see whether the if condition evaluates to true (similar to above point)?
I would think you can add onclick= to the button that is submitting the form since the textboxes are running at server. After the equal sign if you press tab twice it will automatically generate an event in the code behind. Then you can do something like this in the code behind to save it in session.
string firstName = txtUsername.Text;
string lastName = txtPassword.Text;
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
and to access it later
string firstName = Session["FirstName"];
First of all to checking if it is a post you can check with this:
if (IsPostback) { //do something here }
and you will see that if it is done on the same page will not work coz it post to itself.When you click a button in the same page data are generally saved in viewstate.So before give you more info we need to have more info about it work to better help you.

Page_Load firing twice regardless of what i do

I am working on a simple asp.net login page. Yesterday, the code was working fine. Today, it isn't.
The only thing that changed between yesterday and today is that I shut down my pc and started it again today.
The problem is that Page_Load is firing twice (I checked all the answers/solutions, and none worked (image with empty src, handling the page_load manually, setting autoEventWireUp to false...)) none of these seemed to do the trick.
PLEASE can someone help me figure out why this is happening?
Here is the code for the page and its code behind:
<%# Page Title="Login" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="MatchingWebsite.Login" MaintainScrollPositionOnPostback="true" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1><%: Title %></h1>
</hgroup>
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2 align="center">Looking for someone to spend your time with? Want to have fun with someone you like?<br />You've come to the right place!!</h2><br /><br /><br />
<img alt="Cupid" src="Images/images.jpg" align="left" />
<img alt="Couple" align="right" src="Images/matchmaking.jpg" /><br /><br />
Username:<br />
<asp:TextBox ID="UserLogIn" runat="server" Width="174px"></asp:TextBox><br /><br />
Password:<br />
<asp:TextBox ID="UserPass" runat="server" Width="175px" TextMode="Password"></asp:TextBox><br />
<asp:Label ID="LoginError" runat="server" Text="Wrong Username/Password Combination. Try again." Visible="False"></asp:Label><br /><br />
<asp:Button ID="LoginButton" runat="server" OnClick="Login_Click" Text="Login" Width="81px" />
<asp:Button ID="NotRegistered" runat="server" Text="Not Registered?" Width="150px" OnClick="Not_Registered" />
</asp:Content>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css">
.auto-style1
{
width: 300px;
height: 113px;
}
</style>
</asp:Content>
And here is the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MatchingWebsite
{
public partial class Login : System.Web.UI.Page
{
Service1 proxy = new Service1();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"]!=null && !IsPostBack)
Response.Redirect("~/EnterMyInfo.aspx");
}
protected void Login_Click(object sender, EventArgs e)
{
string user = UserLogIn.Text;
string pass = UserPass.Text;
if (user == "" || pass == "")
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Username/Password cannot be blank.\")</SCRIPT>");
else
{
if (proxy.login_service(user, pass))
{
Response.Redirect("~/EnterMyInfo.aspx");
}
else
LoginError.Visible = true;
}
}
protected void Not_Registered(object sender, EventArgs e)
{
Response.Redirect("~/SignUp.aspx");
}
}
}
Likely the webpage is really loaded twice. Check your javascripts (or any AJAX components?), or use the browser's debug console to monitor the network requests being sent. Try adding empty methods on the PreInit, PostInit (etc.) events and set breakpoints too, I bet those are hit twice as well.

Changing the text of a hyperlink in asp.net on click?

I am trying to change the text of my hyperlink after the user has clicked it. Here is the hyperlink:
<asp:hyperlink id="OpenClose" runat="server" onclick="OpenClose_Click" AutoPostBack="true">Close</asp:hyperlink>
And here is my code behind:
protected void Page_Load(object sender, EventArgs e)
{
OpenClose.Attributes.Add("onclick", "OpenClose_Click");
}
protected void OpenClose_Click(object sender, EventArgs e)
{
if (OpenClose.Text == "Close")
OpenClose.Text = "Open";
else
OpenClose.Text = "Close";
}
The problem is that it does not seem to see the function OpenClose_Click. I am not sure why. Is there another method to do this or am I missing something?
EDIT
Here is the entire aspx code
<%# Page Title="" Language="C#" MasterPageFile="../MasterPageLite.master" AutoEventWireup="true" CodeFile="testPageLoad2.aspx.cs" Inherits="BuilderPages_testPageLoad2" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="left_side">
<form id="form1" runat="server">
This is the second test page I am making. Practice collapse and expand panels!
<div class="msg_list">
<h3 class="msg_head">Header-1</h3>
<div class="msg_body">
Collapse this panel!!
<asp:button runat="server" text="Can you see me?" />
</div>
<h3 class="msg_head">Header-2</h3>
<div class="msg_body">
Congratulations you opened the panel!!
</div>
<h3 class="msg_head">Header-3</h3>
<div class="msg_body">
The third panel has been opened!!
</div>
</div>
</form>
</div>
<div class="right_side">
<div class="lBorder">
<asp:Panel ID="OpenClosePanel" runat="server"></asp:Panel>
<asp:HyperLink id="OpenClose" runat="server" AutoPostBack="true" style="cursor:pointer; text-decoration:underline;">Show/Hide</asp:HyperLink>
</div>
<div class="rscontent">
<p>
Lorem ipsum...
</p>
<p>
Nulla...
</p>
<p>
Vivamus...
</p>
<p>
Phasellus...
</p>
<p>
Aenean...
</p>
</div>
</div>
</asp:Content>
You should use a LinkButton instead of a HyperLink control, like this:
Markup:
<asp:LinkButton id="OpenClose"
runat="server"
OnClick="OpenClose_Click"
AutoPostBack="true"
Text="Close"></asp:LinkButton>
Code-Behind:
protected void OpenClose_Click(object sender, EventArgs e)
{
if (OpenClose.Text == "Close")
{
OpenClose.Text = "Open";
}
else
{
OpenClose.Text = "Close";
}
}
The LinkButton class derives from the Button class thus it has similar events to a button, which is the effect you want, but it renders like a hyperlink.
<asp:hyperlink ... is not a valid type of control since .NET is case sensitive. Try changing it to:
<asp:HyperLink ...
I would also get rid of the code in your page load event.

ASP/C#, adding text to textbox problem

For some reason, I cannot get text into any textbox or label!
I'm using Master pages and the code is going in the code behind view. I have created the textbox:
<asp:Textbox ID="whatever" runat="Server">
When I want to add some text I simply add the code in the code behind view like:
whatever.Text = "myText";
I get an error that says:
"System.NullReferenceException:Object reference not set to an instance of an object"
hightlighting this line in red: whatever.Text = "myText";
I guess its because it saying it not there but how can it let me reference the textbox?
Apologies if the answer is on the site, I have searched but found nothing. :)
This is my code in Basket.asp - I've changed the textbox to a label, it's called bskItems
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
<asp:Label ID="bskItems" runat="server"></asp:Label>
<div id="cart">
<asp:Button ID="btnCheckout" CssClass="BasketBtnAdd" runat="server" CommandName="checkout" Text="Checkout" />
</div>
</asp:Content>
This is my masterpage, where I'm using a loginView. ContentPlaceHolder3 is where the textbox should be. I only want it to display a count of items.
<asp:LoginView ID="loginView" runat="server">
<LoggedInTemplate>
<asp:LoginName ID="loginName" runat="server" FormatString="Hi, {0}!"/>
(<asp:LoginStatus ID="loginStatus" runat="server" />)
<%
if (HttpContext.Current.User.IsInRole("Admin"))
{
%>
<asp:SiteMapDataSource ID="admin" SiteMapProvider="admin" runat="server" ShowStartingNode="false" />
<asp:Menu ID="Menu" runat="server" DataSourceID="admin">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
<%
}
if (HttpContext.Current.User.IsInRole("Users"))
{
%>
<asp:SiteMapDataSource ID="user" runat="server" SiteMapProvider="user" ShowStartingNode="false" />
<asp:Menu ID="Menu1" runat="server" DataSourceID="user">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
<%
}
%>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server"></asp:ContentPlaceHolder>
</LoggedInTemplate>
<AnonymousTemplate>
<asp:LoginStatus ID="loginStatus" runat="server" />
<asp:SiteMapDataSource ID="anon" runat="server" SiteMapProvider="anon" ShowStartingNode="false" />
<asp:Menu ID="Menu2" runat="server" DataSourceID="anon">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
</AnonymousTemplate>
</asp:LoginView>
In addition to the other answers, if you're setting the value in Page.OnLoad, remember that the Master page controls haven't been created yet.
Here's a complete layout of the order in which things happen: Complete Lifecycle of an ASP Page
What I usualy do is to make the control visible as a property of my MasterPage.
On the master page (AMasterPage.master):
public TextBox MyTextBox { get { return this.theTextBoxControl; } }
So then, on a child using this masterPage (APage.aspx) :
((AMasterPage)this.Master).MyTextBox.Text = "myText";
When accessing Master Page members from Code-Behind in a Content Place Holder file, I believe you need to do:
this.Master.whatever.Text = "new Text";
Check this link on ASP.NET Master Pages, from MSDN.
You need to do get a reference to the textbox on the master page, then set the text
TextBox tb = Master.Page.FindControl("whatever") as TextBox;
if(tb != null)
{
tb.Text = "myText";
}
Set the ClientIDMode on the textbox to "Static". When the page is rendered it assigns the TextBox's ID to something random. By changing the ClientIDMode to "Static", you should be able to reference the ID because the ID will stay the same and not change.
Or try adding an OnDataBinding event handler and casting the "sender" as a (TextBox). For example:
protected void TextBox_OnDataBinding(object sender, EventArgs e)
{
var txt = (TextBox)sender;
txt.Text = "Something";
}
This should talk to the control directly.

Categories

Resources