How to calculate the page index in grid view - asp.net

I am using a set of codes for all web pages
grvcity.PageIndex + = 1;
to get the next page in grid view
I want to caluclate the page index where the currently is on . How to do it

GridView.PageIndex property both gets and sets the index of currently displayed page:
[BrowsableAttribute(true)]
public virtual int PageIndex { get; set; }
Therefore, the answer in your case should simply be:
int currentPage = grvcity.PageIndex;
Update
If you are talking about the PageIndexChanging event, here is a sample:
protected void grvcity_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvcity.PageIndex = e.NewPageIndex;
grvcity.DataBind();
}

Related

How to assign the value of user control property to aspx or codebehind in VB?

I have a user control with a public property which updates each time when a date from my calender(part of user control) is selected. Now I need to bring this value to page on which this user control is kept. How to do this.
I tried bringing the property value on page load event of aspx.vb(master page on which user control is present) but couldn't do it as page load is happening first and user property is loading next(null reference exception).
i tried this on page load of aspx hdnPPSeq.Value = PPCalender1.test1.ToString
Please share ideas to bring this value to aspx or codebehind in vb.
Create a method in usercontrol, which will return property value.
public partial class PassPropertyToPage : System.Web.UI.UserControl
{
string strSelectDateTime;
public string SelectedDateTime
{
set { strSelectDateTime = value; }
get { return strSelectDateTime; }
}
protected void Page_Load(object sender, EventArgs e){}
public string GetDateTime()
{
strSelectDateTime = <calendar value>;
return strSelectDateTime;
}
}
And in page, call the method to get the value:
public partial class AccessUserControlProperty : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("DateTime selected in page: " + PassPropertyToPage1.GetDateTime() + "<br/>");
}
}

Asp.net property value set to zero on button click

On my ASP.net page I am assigning a session variable to a private property on Page_Load
private int userid{ get; set;}
if (!IsPostBack)
{
if (Session["userid"] != null)
{
userid= int.Parse(Session["userid"].ToString());
}
}
And in my Add button click event I am using userid
protected void btnAdd_Click(object sender, EventArgs e)
{
doSomething(userid);
}
Initially the value is retrieved and stored in userid property but in btnAdd_Click, the userid becomes 0. I moved the code out of if(!IsPostBack) block, that fixed the problem but left me wondering what set my userid property to zero once it is already set on page load. Not clear whether on button click a new instance of the page is returned or what? Any guidance?
You could just remove the stuff from the page load, since the data is in the session, and just change the property to the following:
private int userid{ get{ return int.Parse(Session["userid"].ToString()); } }

how to insert data within a calendar cell into a database

i have a calender and i was able to add text-box inside the cell using the day-render event but what i am trying to do is allow the user to add data to the text-box and then press add and the content is added to a database and showed inside that same text-box:
here is what i did:
protected void Page_Load(object sender, EventArgs e)
{
Calendar1.SelectedDate = DateTime.Now;
}
protected void update(object sender, DayRenderEventArgs e)
{
TextBox tb = new TextBox();
Button1.click += new EventHandler(insert);
e.Cell.Controls.Add(Button1);
e.Cell.Controls.Add(textBox2);
}
protected void insert(object sender, EventArgs e)
{
}
and i know how to insert the data but i am lost on how to identify it and output it back to the same text box
thanks
Well I'm not sure exactly what part you are lost on based on your question, so short of providing a complete working example I'll hit some of the main points:
1) To identify the data you are inserting, attach a date (and time if applicable) to the record. (Edit: are you looking for a mechanism to accomplish this? If so post your current DayRender handler code and Calendar markup).
2) To populate individual day data in a calendar, use Calendar.VisibleDate (to filter DB results) in a Page.Load handler to load a data structure (such as a List<T>) with day data for the entire month. Then in a Calendar.DayRender handler, add appropriate records from the structure to the e.Cell.
3) To cause the new results to show up on the first page refresh after insertion, you should be able to get away with using a Response.Redirect to the current page after insertion. This will cause the page generation process to restart, but you will lose ViewState.
Edit: Here is a basic prototype of what I tried to describe above. Note that you could pre-fetch into any enumerable data type, I use a List<T> here.
//Page code-behind
public partial class Default2 : System.Web.UI.Page
{
List<DataObject> liCurrentMonth = new List<DataObject>();
protected void Page_Load(object sender, EventArgs e)
{
liCurrentMonth = DataObject.GetCurrentMonth(Calendar1.VisibleDate);
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
foreach (DataObject item in liCurrentMonth)
{
if (item.date == e.Day.Date)
{
Literal lit = new Literal();
lit.Text = item.text;
e.Cell.Controls.Add(lit);
}
}
}
}
//Data layer object
public class DataObject
{
public DateTime date { get; set; }
public string text { get; set; }
public static List<DataObject> GetCurrentMonth(DateTime currentdate)
{
//Get items from the db here, based on currentdate parameter
//and populate the List<DataObject>.
return new List<DataObject>();
}
}
I'm not sure you'll get this to work. Its the page viewstate that remembers the value entered into textboxes etc. Viewstate is created just before the page renders. The Calendar DayRender event is called during the calendars rendering, and hence after viewstate has done its thing. Therefore the viewstate mechanism is not aware of the existance of the textboxes and so will not track their content.
Perhaps you could assign each textbox an ID based on the month and day number, on postback you could check for this control ID in the Request.Form collection and do something with the data?

asp.net data entry form

I want to build a form where users can enter some data in a few text boxes and click the "Add" button and have the the data appear in a grid or something like it. They need to be able to enter multiple rows.
Then when they are done they can click the "Save" button to save the data to the database.
How do I get the data from from the text boxes into the "grid"?
EDIT
Here's what I have so far
protected void Page_Load(object sender, EventArgs e)
{
DataTable myDataTable = new DataTable();
DataColumn dc1 = new DataColumn("Employee");
myDataTable.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("Category");
myDataTable.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("Description");
myDataTable.Columns.Add(dc3);
DataColumn dc4 = new DataColumn("P/S/N");
myDataTable.Columns.Add(dc4);
DataColumn dc5 = new DataColumn("Hours");
myDataTable.Columns.Add(dc5);
DataColumn dc6 = new DataColumn("WeekEnding");
myDataTable.Columns.Add(dc6);
}
protected void btnAddToGrid_Click(object sender, EventArgs e)
{
DataRow row = myDataTable.NewRow();// i'm getting error here sayind myDataTable does not exist
row["Employee"] = LoginName1.ToString();
row["Category"] = ddlCategory.SelectedValue;
row["Description"] = txtDescription.Text;
row["P/S/N"] = ddlPSN.SelectedValue;
row["Hours"] = ddlHours.SelectedValue;
row["WeekEnding"] = txtWeekEnding.Text;
myDataTable.Rows.Add(row);
Ok your first problem from your comment:
i'm getting error here sayind myDataTable does not exist
Is because you defined your table in the Page_Load and then it goes out of scope at the end of the function. It sounds like you don't understand the basic concepts of ASP.NET and what you are trying to do.
Here is a quick and dirty untested solution to your problem but I must stress that you should try and understand why this solution works before trying to extend it or do more in ASP.NET. I hope my 10 minutes of my time helps you get a good start into understanding C# and ASP.NET.
[Serializable]
public class YourData
{
public string Employee { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public string P_S_N { get; set; }
public string Hours { get; set; }
public string WeekEnding { get; set; }
}
// Used to add your list of data to the viewstate cache
// (There are other ways to store data between posts but I am trying to keep it simple)
public void SetYourCachedData(List<YourData> data)
{
ViewState["YourDataCache"] = data;
}
// Used to get your save data so far
public List<YourData> GetYourCachedData()
{
return ViewState["YourDataCache"] == null ?
new List<YourData>() : (List<YourData>)(ViewState["YourDataCache"]);
}
protected void Page_Load(object sender, EventArgs e)
{
// Do nothing
}
protected void btnAddToGrid_Click(object sender, EventArgs e)
{
// Get the data and store it in the ViewState to cache it between post backs
// Assuming one record added with each click.
List<YourData> data = GetYourCachedData();
data.Add(new YourData()
{
Employee = LoginName1.ToString(),
Category = ddlCategory.SelectedValue,
Description = txtDescription.Text,
P_S_N = ddlPSN.SelectedValue,
Hours = ddlHours.SelectedValue,
WeekEnding = txtWeekEnding.Text
});
// You can bind to any type of collection easily.
// You don't need to use a DataTable with a GridView
yourGrid.DataSource = data;
yourGrid.DataBind();
SetYourCachedData(data);
}
protected void btnSaveData_Click(object sender, EventArgs e)
{
// Pull data from the ViewState cache
List<YourData> data = GetYourCachedData();
// Now iterate through data to save it to your database...
// look this up if you don't know how as this is a lot more work.
}
looks like you just need to set the output object's datasource and bind it.
I.e.:
myGrid.datasource = myDataTable
myGrid.databind()
And as Kelsey says -- keep your datatable in scope.
a public dim right above your page_load would be easier, if you just want to try it out. Otherwise, the separate class approach is a good way to go.

UserControl: How to set Output Cache duration programmatically?

UserControl: How to set Output Cache duration programmatically?
Make a public property on the UserControl which you could use to change the duration of the cache.
public Double CacheDuration { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetExpires(DateTime.Now + TimeSpan.FromSeconds(CacheDuration));
Response.Write(DateTime.Now.ToString());
}
Then when you add the UserControl, mine is named Cached, you could do this.
var cachedUserControl = new Cached {CacheDuration = 5};
Page.Controls.Add(cachedUserControl);
You can sue Response.Cache.SetExpires(DateTime DateTime); for that. Some useful infor here as well How to enable ASP.NET output caching programmatically.

Resources