Please See The Simple Example Below For Understanding My situation.
(Attention To Comments Inside Codes)
Master Page (ASPX) :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="NiceFileExplorer.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<span runat="server" id="SummaryContainer">
<asp:Label ID="lblDownload_Count_By_UserID_Today_Title" runat="server" Text="Count :"
ToolTip="Your Download Count-Today" CssClass="lblTitleInStatistics_Master"></asp:Label>
<asp:Label ID="lblDownload_Count_By_UserID_Today" runat="server" Text="<%# Download_Count_By_UserID_Today() %>"
CssClass="lblCountInStatistics_Master" ToolTip="Your Download Count-Today"></asp:Label>
<span style="color: white;"> | </span>
<asp:Label ID="lblDownload_Size_By_UserID_Today_Title" runat="server" Text="Size :"
ToolTip="Your Download Size-Today" CssClass="lblTitleInStatistics_Master"></asp:Label>
<asp:Label ID="lblDownload_Size_By_UserID_Today" runat="server" Text="<%# Download_Size_By_UserID_Today() %>"
CssClass="lblCountInStatistics_Master" ToolTip="Your Download Size-Today"></asp:Label>
</span>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" ViewStateMode="Inherit" ClientIDMode="Static">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
as you see i set ClientIDMode="Static".
Master Page (CodeBehind) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NiceFileExplorer
{
public partial class Site1 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
SummaryContainer.DataBind();
}
protected string Download_Count_By_UserID_Today()
{
//Read New Count From DataBase
//return Count;
return "Test";
}
protected string Download_Size_By_UserID_Today()
{
//Read New Size From DataBase
//return Size;
return "Test";
}
}
}
Content Page (ASPX) :
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NiceFileExplorer.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
Conntent Page
</asp:Content>
Content Page (CodeBehind) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NiceFileExplorer
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyMethod();
}
private void MyMethod()
{
//Add New Downloaded File Info To DataBase(); -> For Getting Count And Size Of Them Per Day
//Here I Wand To Access Master Page Controls And Update Count And Size Lables
//So, I Tried Codes Below Without Any Results -> How Can I Fix This ?
var SummaryContainer = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Master.FindControl("SummaryContainer");
SummaryContainer.DataBind();
SummaryContainer.InnerHtml = "<h2>Hello World</h2>";
//After Update Those Lables Failed, I test the codes Below With Null Execption Error -> How Can I Fix This ?
var lblDownload_Count_By_UserID_Today_Title = (Label)Page.Master.FindControl("lblDownload_Count_By_UserID_Today_Title");
lblDownload_Count_By_UserID_Today_Title.Text = "test";
DwonloadFile();
}
private void DwonloadFile()
{
//A Class (Method) That Shows Download Window To My Users, So Page_Load Of Master Will Never Fire...
//And This Is The Reason That I want to update count & size lables from content page
}
}
}
i want to DataBind SummaryContainer(a span) from content page's code-behind.
so i tried the codes below :
var SummaryContainer = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Master.FindControl("SummaryContainer");
SummaryContainer.DataBind():
but i can not see new results.
After That Fail I tried to find a label's text(that label is inside Master) from content page code behind for test like below : var
lblDownload_Count_By_UserID_Today_Title = (Label)Page.Master.FindControl("lblDownload_Count_By_UserID_Today_Title");
lblDownload_Count_By_UserID_Today_Title.Text = "test";
but i have System.NullReferenceException ERROR :
Object reference not set to an instance of an object.
how can i fix that error and force that span to show me new results?
thanks in advance
In a project I used an interface on the masterpage:
((IMasterPage)Page.Master).MyProperty = "test";
But in your case, personally instead of putting all that on the master page, I'd put your SummaryContainer into a UserControl, and have another ContentPlaceHolder.
Then the Page_Load method will be able to access the properties, and on future pages you could have different summary info by filling that first PlaceHolder with a different UserControl.
Also debugging stupid errors, is the Null exception being thrown at .Master.FindControl or at lbl.Text?
I'm unable to debug it for myself right now, but would it be due to the page life cycle, namely that Content Page Load comes before Master Page Load?
Related
I am new to stackoverflow and am needing help.
Last night I was working on an ASP.NET webpage using C# [I'm all very new to it] and after losing a lot of my progress after a flash drive failure, I had to rewrite from a backup I stored on google drive. After putting it onto my computer, I received problems with one of my webpages. At first it did not recognize objects "existing in the current context" but I rewrote the page from hand, because I thought it was some trouble caused by copy-pasting things back. Now I get these errors:
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends t he correct base class (e.g. Page or UserControl).
'ASP.index_aspx.GetTypeHashCode();: no suitable method found to override
'ASP.index_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override
'ASP.index_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable'
I did not have these problems at all when working with the version that I lost. Here is my code:
This is my "upload.aspx" page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BSHUpload</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"/>
</head>
<body>
<!-- Menu -->
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<ul>
<li>Home</li>
<li class="pure-menu-selected">Upload</li>
<li>Requests</li>
</ul>
</div>
<!-- Server-side Upload -->
<form id="form1" runat="server" style="padding-left: 2em">
<div>
<h1>Upload a File</h1>
</div>
<div>
<asp:Label ID="lblStatus1" runat="server" Text="---"></asp:Label>
<asp:FileUpload ID="fdFileDrop1" runat="server" />
<asp:Button ID="btnFileDrop1" runat="server" Text="upload" OnClick="btnFileDrop1_Click" />
</div>
</form>
</body>
</html>
And my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFileDrop1_Click(object sender, EventArgs e)
{
string strSavePath1 = "C:\\UploadBin\\";
if (fdFileDrop1.HasFile)
{
string strFileName = fdFileDrop1.FileName;
strSavePath1 += strFileName;
fdFileDrop1.SaveAs(strSavePath1);
lblStatus1.Text = "Your file was saved as " + strFileName;
}
else
{
lblStatus1.Text = "You did not specify a file to upload";
}
}
}
And because I think the bottom 3 errors are for the "index.aspx" page here is that also:
Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BSHUpload</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
</head>
<body style="height: 228px">
<!-- Menu -->
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<ul>
<li>Home</li>
<li class="pure-menu-selected">Upload</li>
<li>Requests</li>
</ul>
</div>
<form id="form1" runat="server" style="padding-left: 2em">
</form>
<div>
<button id="button1">Testing</button>
<p id="toggle1">
Wala
</p>
<script>
$( "#button1" ).click(function() {
$( "#toggle1" ).slideToggle( "slow" );
})
</script>
</div>
</body>
</html>
Code Behind for index.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I really hope you guys can help because I'm stumped here. I'm pretty sure that both the inherits and the "System.Web.UI.Page" is correct but it says it is not.
The problem is on index.aspx. Check this at the very top of the file
CodeFile="upload.aspx.cs" Inherits="index"
That's wrong. You're pointing to the incorrect code file. It should be
CodeFile="index.aspx.cs" Inherits="index"
I want this gridview to emulate a console, the text file I have is a server console log.
I want it to:
*Refresh when the file changes.(thus the timer)
*Scroll bar position maintained when updated, unless at bottom.
*When scroll bar position is at the bottom,stay at the bottom after databind update.
Current Problem: On timer, databind() moves the scrollbar position back to the top, and with a refresh so often you can't even scroll down at all.
Here is my current code. Thank you for your help!
asp.net
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TEST CONSOLE.aspx.cs" Inherits="WebApplication1.TEST_CONSOLE" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="30" ontick="Timer1_Tick"></asp:Timer>
<div style="width: 100%; height: 400px; overflow: scroll">
<asp:GridView ID="GridView1" runat="server" MaintainScrollPositionOnPostback="true">
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class TEST_CONSOLE : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StreamReader objStreamReader = new StreamReader(Server.MapPath("TextFile.txt"));
var arrText = new List<string>();
// ' Loop through the file and add each line to the ArrayList
while( objStreamReader.Peek() >= 0){
arrText.Add(objStreamReader.ReadLine());
}
//' Close the reader
objStreamReader.Close();
//' Bind the results to the GridView
GridView1.DataSource = arrText;
GridView1.DataBind();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1.DataBind();
}
}
}
Found most of answer here
http://forums.asp.net/t/1220070.aspx?how+to+maintain+scroll+position+in+gridview+or+datagrid+during+postback
using ajax and these two functions
function Func() {//document.getElementById("hdnScrollTop").value
document.getElementById("divScroll").scrollTop = document.getElementById("hdnScrollTop").value;
}
function Func2() {
var s = document.getElementById("divScroll").scrollTop;
document.getElementById('hdnScrollTop').value = s;
the aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MyWebApplication.MyWebForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 334px">
<form id="form1" runat="server">
<div style="height: 338px">
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="True"></asp:TextBox>
<br />
<asp:CustomValidator ID="MyCustomValidator" runat="server"
ControlToValidate="MyTextBox" ErrorMessage="My error message"
onservervalidate="Foo2"></asp:CustomValidator>
</div>
</form>
</body>
</html>
the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWebApplication
{
public partial class MyWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
bool Foo1(string bar)
{
if (bar == "bar")
return true;
return false;
}
protected void Foo2(object source, ServerValidateEventArgs args)
{
args.IsValid = this.Foo1(this.MyTextBox.Text);
} // breakpoint
}
}
I set breakpoint in the Foo2 function (comment), but debugger hasn't visited it. I tried writing diffrent text, not writting anything and writting "bar" and pushing enter and tab after that, but it still doesn't work. I mean text changing action. Is it possible?
There's no submit button. You need to add a button to submit the form, which will then trigger page validation.
<asp:Button runat="server" id="myButton" Text="Submit" />
I am using C# ASP.NET , i did a crosspage postback and it is working fine, without master page.
But while using Master page , same logic fails and get the error described above. I am new to ASP.NET, please tell me in little detail.
My code is
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="View_Information.aspx.cs" Inherits="View_Information" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
Module 3: Assignment 1</p>
<div>
Total Data You Have Entered
<br />
<br />
Name:
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />
Address:
<asp:Label ID="Label2" runat="server"></asp:Label>
<br />
<br />
Thanks for submitting your data.<br />
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Placehodler2" Runat="Server">
</asp:Content>
And code behind is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class View_Information : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsPostBack)
{
TextBox nametextpb = (TextBox)PreviousPage.FindControl("TextBox1");
//Name of controls should be good to identify in case application is big
TextBox addresspb = (TextBox)PreviousPage.FindControl("TextBox2");
Label1.Text = nametextpb.Text; //exception were thrown here
Label2.Text = addresspb.Text;
}
else
{
Response.Redirect("Personal_Information.aspx");
}
}
}
The problem is that, with the master page, your controls are now required to be placed in the ContentPlaceHolder controls.
The FindControl method can be used to access a control whose ID is not
available at design time. The method searches only the page's
immediate, or top-level, container; it does not recursively search for
controls in naming containers contained on the page. To access
controls in a subordinate naming container, call the FindControl
method of that container.
You now need to recursively search through the controls to find your TextBox controls from the PreviousPage. You can see an example of that here. Also noted on that site, you can get the control by its full UniqueID, which in your case will work via:
TextBox nametextpb = (TextBox)PreviousPage.FindControl("ctl00$ContentPlaceHolder1$TextBox1")
EDIT: Figured it couldn't hurt to include the code I used to locate the UniqueID of the target control.
In Page_Load:
var ids = new List<string>();
BuildControlIDListRecursive(PreviousPage.Controls, ids);
And the method definition:
private void BuildControlIDListRecursive(ControlCollection controls, List<string> ids)
{
foreach (Control c in controls)
{
ids.Add(string.Format("{0} : {2}", c.ID, c.UniqueID));
BuildControlIDListRecursive(c.Controls, ids);
}
}
Then just locate your control from the ids list.
(TextBox)PreviousPage.FindControl("TextBox1"); must have returned null, which means that the control was not found.
Try using Page.Master.FindControl() instead.
I have GUI for data acceptance.
I need to pass all the parameters of the form on click of a submit button to a function declared in C#.
Please help.
Using .Net us have too types of submit tags, one starting with <asp: and the other starting with <input. the <input html tag can call javascript and if you add the runat="server" attribute, you will enable it to also have C# code behind the button.
First of all, you will need to create an aspx page (say submission.aspx) that will receive the POST submission of your form. In that page, you can include your .cs file that contains the method/function you want to pass the data to.
Next, you want to submit you submit your data to submission.aspx. To do that, you will need to have a form which will submit its data to submission.aspx.
<form action='submission.aspx' method='POST' id='data-submission'>
<!-- stuff here -->
</form>
If you want to perform ajax submission, you can use jquery and use this code:
$('#data-submission').submit(function(evt){
var $form = $(this);
var url = $form.attr('action');
$.post(url, $form.serialize(), function(){alert('submission complete!);});
});
I wonder if all that helped you.
PS: I haven't used .NET for web programming in a long time now.. but what I've written here in this answer hold universally true for any web programming language/framework.
If your using asp.net you just need to double click the button(if it is an asp button) and it should make a click event.
In the click event you could get your other controls like
default.aspx code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
// you can declare it as a field variable so the entire code behind can use it
private Passengerdetails myClass;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// create an instance of the class.
myClass = new Passengerdetails ();
// stick textbox1 contents in the property called test.
myClass.PassengerName = TextBox1.Text;
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
int sum = Add(a, b);
// do something with it like return it to a lbl.
Label1.Text = sum.ToString();
}
private int Add(int a, int b)
{
return a + b;
}
}
Edit. You just make a class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for passengerdetails
/// </summary>
public class Passengerdetails
{
public Passengerdetails ()
{
public string PassengerName{ get; set; }
}
}