problem with viewstate? dll? namespace?
Hey there...I'm new to this c# and .net stuff, but I've picked up a little. I've got a basic .aspx page that runs with a code behind page. I'm also trying to wrap up my sorting stuff into a dll so I can use it in other files, too. Here is my .aspx page:
<html>
<head>
</head>
<body>
<form runat="server">
<klasUC:Header id="PageHeader" runat="Server"></klasUC:Header>
<asp:panel id="pnlListCustomer" runat="server">Select a Record to Edit <br />
<br />
<asp:Label id="lblLetters" runat="Server"></asp:Label>
<br />
<asp:datagrid id="myGrid" runat="server" AutoGenerateColumns="false" OnSortCommand="sortRecords" AllowSorting="True" itemstyle-CssClass="dataGridInterior" headerstyle-CssClass="dataGridHeader" alternatingitemstyle-backcolor="lightgray" backcolor="ghostwhite" borderwidth="1" bordercolor="black" gridlines="vertical" cellpadding="5" width="100%">
<columns>
<asp:boundcolumn headertext="CustomerID" datafield="CustomerID" SortExpression="CustomerID" />
<asp:hyperlinkcolumn headertext="Name" datanavigateurlfield="CustomerID" datanavigateurlformatstring="customer_edit_minimum .aspx?id={0}" datatextfield="Name" SortExpression="Name" />
<asp:boundcolumn headertext="Email" datafield="Email" SortExpression="Email" />
<asp:boundcolumn headertext="JobTitle" datafield="JobTitle" SortExpression="JobTitle" />
</columns>
</asp:datagrid>
<asp:repeater id="rptNumericPaging" runat="server">
<itemtemplate>
<asp:LinkButton id="pagingid" runat="server" commandname="Filter" commandargument='<%# DataBinder.Eval(Container, "DataItem.PagingNumber")%>' onCommand="OnLinkClick">
<%# DataBinder.Eval(Container, "DataItem.PagingNumber")%>
</asp:linkbutton>
</itemtemplate>
</asp:repeater>
<br />
<br />
Total Records <asp:Label id="lblRecordCount" runat="Server"></asp:Label>
<br />
Viewing Page <asp:Label id="lblPage" runat="Server"></asp:Label>- Records <asp:Label id="lblLowerBound" runat="Server"></asp:Label>to <asp:Label id="lblUpperBound" runat="Server"></asp:Label>
<br />
<br />
<asp:Label id="lblPaging" runat="Server"></asp:Label>
</asp:panel>
<asp:panel id="pnlEditCustomer" runat="server">
<asp:Label id="lblResults" runat="server"></asp:Label>
<br />
<br />
Edit Customer Record<br />
<br />
<asp:ValidationSummary id="valSum" runat="server" Font-Size="10" Font-Name="arial, verdana" HeaderText="You must enter a value in the following fields:"></asp:ValidationSummary>
<table cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr>
<td>
<asp:Label id="lblFirstName" runat="server">First Name:</asp:Label></td>
<td>
<asp:TextBox id="tbxFirstName" runat="server"></asp:TextBox>
</td>
<td>
<sup><strong>*</strong></sup>
<asp:RequiredFieldValidator id="reqFirstName" runat="server" align="left" InitialValue="" Display="Static" ErrorMessage="First Name " ControlToValidate="tbxFirstName"> </asp:RequiredFieldValidator>
</td>
<td>
</td>
<td>
<asp:Label id="lblPhone" runat="server">Phone:</asp:Label></td>
<td>
<asp:TextBox id="tbxPhone" runat="server"></asp:TextBox>
</td>
<td>
<sup><strong>*</strong></sup>
<asp:RequiredFieldValidator id="reqPhone" runat="server" align="left" InitialValue="" Display="Static" ErrorMessage="Phone " ControlToValidate="tbxPhone"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblLastName" runat="server">Last Name:</asp:Label></td>
<td>
<asp:TextBox id="tbxLastName" runat="server"></asp:TextBox>
</td>
<td>
<sup><strong>*</strong></sup>
<asp:RequiredFieldValidator id="reqLastName" runat="server" align="left" InitialValue="" Display="Static" ErrorMessage="Last Name " ControlToValidate="tbxLastName"> </asp:RequiredFieldValidator>
</td>
<td>
</td>
<td>
<asp:Label id="lblFax" runat="server">Fax:</asp:Label></td>
<td>
<asp:TextBox id="tbxFax" runat="server"></asp:TextBox>
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label id="lblPreferredName" runat="server">Preferred Name:</asp:Label></td>
<td>
<asp:TextBox id="tbxPreferredName" runat="server"></asp:TextBox>
</td>
<td></td>
<td>
</td>
<td>
<asp:Label id="lblEmail" runat="server">Email:</asp:Label></td>
<td>
<asp:TextBox id="tbxEmail" runat="server"></asp:TextBox>
</td>
<td>
<sup><strong>*</strong></sup>
<asp:RequiredFieldValidator id="reqEmail" runat="server" align="left" InitialValue="" ErrorMessage="Email " ControlToValidate="tbxEmail"> </asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="reqEmail2" runat="server" align="left" InitialValue="" ErrorMessage="Email is invalid" ControlToValidate="tbxEmail" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblJobTitle" runat="server">Job Title:</asp:Label></td>
<td>
<asp:TextBox id="tbxJobTitle" runat="server"></asp:TextBox>
</td>
<td>
<sup><strong>*</strong></sup>
<asp:RequiredFieldValidator id="reqJobTitle" runat="server" align="left" InitialValue="" Display="Static" ErrorMessage="Job Title " ControlToValidate="tbxJobTitle"> </asp:RequiredFieldValidator>
</td>
<td>
</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="7">
<p>
<asp:Button id="Button1" onclick="updateCustomer" runat="server" Text="Update"></asp:Button>
</p>
</td>
</tr>
</tbody>
</table>
</asp:panel>
<asp:panel id="pnlUpdateCustomer" runat="server">Customer Record has been successfully
updated <br />
<br />
<asp:HyperLink id="hyperlink1" runat="server" Text="Edit this Record" NavigateUrl=""></asp:HyperLink>
</asp:panel>
<klasUC:Footer id="PageFooter" runat="Server"></klasUC:Footer>
</form>
</body>
</html>
******* The code for my code behind page is as follows:
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Created By: Chris Wright
// Created On: 6/1/03
// Description: CodeBehind for Customer Minimum Page
// Change Log:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;
using sortpaging;
public class myCodeBehind : System.Web.UI.Page
{
//////////////////////////////////////////////////////
public TextBox tbxFirstName;
public TextBox tbxLastName;
public TextBox tbxPreferredName;
public TextBox tbxJobTitle;
public TextBox tbxPhone;
public TextBox tbxFax;
public TextBox tbxEmail;
public Panel pnlListCustomer;
public Panel pnlEditCustomer;
public Panel pnlUpdateCustomer;
public Label lblResults;
public Label lblRecordCount;
public Label lblPaging;
public Label lblLetters;
public Label lblPage;
public Label lblLowerBound;
public Label lblUpperBound;
public HyperLink hyperlink1;
public DataGrid myGrid;
public Repeater rptNumericPaging;
public int intPageCount = 10;
public char strLetter;
//////////////////////////////////////////////////////
private void Page_Load( Object s, EventArgs e ) {
String id = Request.QueryString["id"];
ViewState["intRecordCount"] = RecordCount();
ViewState["intPageCount"] = intPageCount;
if (!IsPostBack) {
if (id == null) {
pnlListCustomer.Visible = true;
pnlEditCustomer.Visible = false;
pnlUpdateCustomer.Visible = false;
BuildLettersLinkList();
BuildPagingLinkList();
bindGrid();
} else {
pnlListCustomer.Visible = true;
pnlEditCustomer.Visible = false;
pnlUpdateCustomer.Visible = false;
getCustomerRecord (id);
}
} else {
pnlListCustomer.Visible = true;
pnlEditCustomer.Visible = false;
pnlUpdateCustomer.Visible = false;
BuildLettersLinkList();
BuildPagingLinkList();
bindGrid();
}
}
public int RecordCount() {
return (int)SqlHelper.ExecuteScalar(ConfigurationSettings .AppSettings["ConnectionString"], "tb_Customers_Sel2_Count", strLetter);
}
public void OnLinkClick( object O, System.Web.UI.WebControls.CommandEventArgs E ) {
startRow = 1 + (intPageCount*(Convert.ToInt32(E.CommandArgument)-1));
curPage = Convert.ToInt32(E.CommandArgument);
bindGrid();
}
public void OnLinkClickLetters( object O, System.Web.UI.WebControls.CommandEventArgs E ) {
strLetter = E.CommandArgument.ToString();
startRow = 1;
curPage = 1;
BuildPagingLinkList();
bindGrid();
}
public void BuildLettersLinkList() {
string[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K","L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V","W", "X", "Y", "Z"};
for (int i = 0; i < letters.Length; i++) {
LinkButton _LB2 = new LinkButton();
_LB2.Text = letters[i].ToString();
_LB2.CommandName = "Page";
_LB2.CommandArgument = letters[i].ToString();
_LB2.Command += new System.Web.UI.WebControls.CommandEventHandler(OnLi nkClickLetters);
lblLetters.Controls.Add(_LB2);
lblLetters.Controls.Add(new LiteralControl(" "));
}
}
public void BuildPagingLinkList() {
DataTable dt;
dt = new DataTable();
dt.Columns.Add(new DataColumn("PagingNumber",typeof(int)));
int tmpPages;
int tmpPossiblePages = RecordCount()/intPageCount;
if ((tmpPossiblePages*intPageCount) < RecordCount()) {
tmpPages = tmpPossiblePages + 1;
} else {
tmpPages = tmpPossiblePages;
}
for (int i=1; i<=(tmpPages); i++) {
DataRow dr = dt.NewRow();
dr[0] = i;
dt.Rows.Add(dr);
}
rptNumericPaging.DataSource = dt.DefaultView;
rptNumericPaging.DataBind();
}
public void sortRecords( Object s, DataGridSortCommandEventArgs e ) {
SortField = e.SortExpression.ToString();
bindGrid();
}
public void bindGrid() {
myGrid.DataSource = SqlHelper.ExecuteReader(ConfigurationSettings.AppS ettings["ConnectionString"], "tb_Customers_Sel2", SortField, startRow, stopRow, strLetter, SortAscending);
myGrid.DataBind();
lblRecordCount.Text = RecordCount().ToString();
lblPage.Text = curPage.ToString();
lblLowerBound.Text = startRow.ToString();
lblUpperBound.Text = stopRow.ToString();
}
private void getCustomerRecord ( String id ) {
string sqlText = "tb_Customers_Sel";
SqlDataReader dr = SqlHelper.ExecuteReader(ConfigurationSettings.AppS ettings["ConnectionString"], sqlText, id);
dr.Read();
tbxFirstName.Text = dr["FirstName"].ToString();
tbxLastName.Text = dr["LastName"].ToString();
tbxPreferredName.Text = dr["NickName"].ToString();
tbxJobTitle.Text = dr["JobTitle"].ToString();
tbxPhone.Text = dr["WorkPhone"].ToString();
tbxFax.Text = dr["Fax"].ToString();
tbxEmail.Text = dr["Email"].ToString();
dr.Close();
}
public void updateCustomer ( Object src, EventArgs e ) {
String id = Request.QueryString["id"];
if (Page.IsValid) {
using (SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]) ) {
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction()) {
// Establish command parameters
SqlParameter paramCustomerID = new SqlParameter("@CustomerID", SqlDbType.Int, 4);
paramCustomerID.Value = id;
SqlParameter paramFirstName = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
paramFirstName.Value = tbxFirstName.Text;
SqlParameter paramLastName = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
paramLastName.Value = tbxLastName.Text;
SqlParameter paramNickName = new SqlParameter("@NickName", SqlDbType.VarChar, 50);
paramNickName.Value = tbxPreferredName.Text;
SqlParameter paramWorkPhone = new SqlParameter("@WorkPhone", SqlDbType.VarChar, 50);
paramWorkPhone.Value = tbxPhone.Text;
SqlParameter paramFax = new SqlParameter("@Fax", SqlDbType.VarChar, 50);
paramFax.Value = tbxFax.Text;
SqlParameter paramEmail = new SqlParameter("@Email", SqlDbType.VarChar, 50);
paramEmail.Value = tbxEmail.Text;
SqlParameter paramJobTitle = new SqlParameter("@JobTitle", SqlDbType.VarChar, 255);
paramJobTitle.Value = tbxJobTitle.Text;
SqlParameter paramLastUpdated = new SqlParameter("@LastUpdated", SqlDbType.DateTime);
paramLastUpdated.Value = DateTime.Now;
try
{
SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "tb_Customers_Upd_Minimum", paramCustomerID, paramFirstName, paramLastName, paramNickName, paramWorkPhone, paramFax, paramEmail, paramJobTitle, paramLastUpdated);
trans.Commit();
lblResults.Text = "Update Completed";
}
catch (Exception ex)
{
trans.Rollback();
lblResults.Text = "Update Error";
throw ex;
}
finally
{
conn.Close();
}
}
}
}
hyperlink1.NavigateUrl = "customer_edit_minimum.aspx?id=" + id;
}
} //End Class
**** And finally, my namespace that I'm trying to make into a dll...
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sortpaging {
public class pageformat : System.Web.UI.Page {
public static string SortField {
get {
//object o = ViewState["SortField"];
if (ViewState["SortField"] == null) {
//return String.Empty;
return "Name";
}
return (string)ViewState["SortField"];
}
set {
if (value == SortField) {
// same as current sort file, toggle sort direction
SortAscending = !SortAscending;
}
ViewState["SortField"] = value;
}
}
public static bool SortAscending {
get {
object o = ViewState["SortAscending"];
if (o == null) {
return true;
}
return (bool)o;
}
set {
ViewState["SortAscending"] = value;
}
}
public static string strLetter {
get {
object o = ViewState["strLetter"];
if (o == null) {
return "A";
}
return (string)o;
}
set {
ViewState["strLetter"] = value;
}
}
public static int startRow {
get {
object o = ViewState["startRow"];
if (o == null) {
return 1;
}
return (int)o;
}
set {
ViewState["startRow"] = value;
}
}
public static int stopRow {
get {
if ((startRow+Convert.ToInt32(ViewState["intPageCount"]) )> Convert.ToInt32(ViewState["intRecordCount"])) {
return Convert.ToInt32(ViewState["intRecordCount"]);
} else {
return (int)startRow+Convert.ToInt32(ViewState["intPageCount"]);
}
}
set {
ViewState["stopRow"] = value;
}
}
public static int curPage {
get {
object o = ViewState["curPage"];
if (o == null) {
return 1;
}
return (int)o;
}
set {
ViewState["curPage"] = value;
}
}
}
}
*** When I try to compile it, I get the following errors:
sortpaging.cs<22,18>: error cs0120: An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Control.Viewstate
I also get this error about 15 more times, just with different line numbers. Help please! Thanks in advance.
|