I am trying to extend the asp.net DataPager control into a custom control with predefined templates. This is the desired output
Problems:
When I load the page containing the custom DataPager for the first time, the SelectMethod of my ObjectDataSource is called 3 times.
When I try to load another page of data using the DataPager, the SelectMethod is called twice.
When I try to load another page of data, either by using the Next/Previous buttons or the DropDownList, the page does not change. I ran some debugging and found that it was not passing the correct value to the StartRowIndexParameter of the SelectMethod (it just passed 0 everytime it called the method).
Here's the code for my custom control.
public class DataPagerDDL : DataPager
{
protected override void RenderContents(HtmlTextWriter writer)
{
//add custom template
TemplatePagerField templateField = new TemplatePagerField();
templateField.PagerTemplate = new CustomTemplate();
Fields.Add(templateField);
//add previous/next page template
NextPreviousPagerField nextPreviousField = new NextPreviousPagerField();
nextPreviousField.ShowFirstPageButton = false;
nextPreviousField.ShowLastPageButton = false;
nextPreviousField.PreviousPageText = "<<";
nextPreviousField.NextPageText = ">>";
Fields.Add(nextPreviousField);
base.RenderContents(writer);
}
public void cmbPage_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList cmbPage = (DropDownList)sender;
this.SetPageProperties(cmbPage.SelectedIndex * MaximumRows, MaximumRows, true);
}
}
public class CustomTemplate : ITemplate
{
/// <summary>
/// Insert an instance of text and controls into the specified container.
/// </summary>
public void InstantiateIn(Control container)
{
DataPagerFieldItem caller = (DataPagerFieldItem)container;
DataPagerDDL pager = (DataPagerDDL)caller.Parent;
int totalPages = pager.TotalRowCount / pager.MaximumRows;
if (pager.TotalRowCount % pager.MaximumRows > 0) totalPages += 1;
int currentPage = (pager.StartRowIndex / pager.MaximumRows) + 1;
DropDownList cmbPage = new DropDownList();
cmbPage.ID = "cmbPage";
cmbPage.AutoPostBack = true;
cmbPage.SelectedIndexChanged += new EventHandler(pager.cmbPage_SelectedIndexChanged);
for (int i = 1; i <= totalPages; i++)
{
ListItem item = new ListItem(i.ToString(), i.ToString());
if (i == currentPage) item.Selected = true;
cmbPage.Items.Add(item);
}
pager.Controls.Add(new LiteralControl("Page "));
pager.Controls.Add(cmbPage);
pager.Controls.Add(new LiteralControl(" of " + totalPages.ToString() + " pages | "));
}
}
And this is what my page looks like:
<asp:ListView ID="ListView1" DataSourceID="ods1" ... >
...
</asp:ListView>
<custom:DataPagerDDL ID="CustomDataPager" runat="server" PagedControlID="ListView1"
PageSize="25">
</custom:DataPagerDDL>
<asp:ObjectDataSource ID="ods1" ... >
</asp:ObjectDataSource>
What should I do to make my custom DataPager work as intended? Thanks in advance! :)
I suspect you're creating the pager fields too late in the page lifecycle. Try creating them from the Init event of the DataPagerDDL class.
Also, your CustomTemplate should be adding the controls to the container, not the pager.
public class DataPagerDDL : DataPager
{
protected override void OnInit(EventArgs e)
{
CreateDefaultPagerFields();
base.OnInit(e);
}
protected virtual void CreateDefaultPagerFields()
{
//add custom template
TemplatePagerField templateField = new TemplatePagerField();
templateField.PagerTemplate = new CustomTemplate();
Fields.Add(templateField);
//add previous/next page template
NextPreviousPagerField nextPreviousField = new NextPreviousPagerField();
nextPreviousField.ShowFirstPageButton = false;
nextPreviousField.ShowLastPageButton = false;
nextPreviousField.PreviousPageText = "<<";
nextPreviousField.NextPageText = ">>";
Fields.Add(nextPreviousField);
}
public void cmbPage_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList cmbPage = (DropDownList)sender;
SetPageProperties(cmbPage.SelectedIndex * MaximumRows, MaximumRows, true);
}
}
public class CustomTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
DataPagerFieldItem caller = (DataPagerFieldItem)container;
DataPagerDDL pager = (DataPagerDDL)caller.Parent;
int totalPages = pager.TotalRowCount / pager.MaximumRows;
if (pager.TotalRowCount % pager.MaximumRows > 0) totalPages += 1;
int currentPage = (pager.StartRowIndex / pager.MaximumRows) + 1;
DropDownList cmbPage = new DropDownList();
cmbPage.ID = "cmbPage";
cmbPage.AutoPostBack = true;
cmbPage.SelectedIndexChanged += pager.cmbPage_SelectedIndexChanged;
for (int i = 1; i <= totalPages; i++)
{
ListItem item = new ListItem(i.ToString(), i.ToString());
if (i == currentPage) item.Selected = true;
cmbPage.Items.Add(item);
}
container.Controls.Add(new LiteralControl("Page "));
container.Controls.Add(cmbPage);
container.Controls.Add(new LiteralControl(" of " + totalPages + " pages | "));
}
}
Related
I want to create a webform with 4 types of phones like: LG, xiaomi, samsung and iphone. They will be in an Array and I will insert them into dynamic radiobuttonList in the page init.
Also, the user will have a textbox where he will put an amount of money he has. The user will also have a button that will calc if he have the budget for the selected phone from the list.
After the user selects the phone, write in the budget and press the button
he will get "in budget" or "not enough budget".
The class have property of the array to insert the all the array from the page init and will have 2 functions:
One function will take the budget number > will go to the 2nd function that will see the selected phone and the budget > do its calcs and return the result into the 1st func that will give the feedback.
Now where I am stuck:
if the class isnt made global - and i put it in init or in button click - it wont work, so im looking for a way to make it work but without putting it global
so far i managed to inject the selected value into a class property and than compare - but i want to know if there is a way that this can happen with array inside class property
maybe if anyone can help me out and refer me to a guide where i can learn more about this subject (how inject selected value into function of a class and etc) ill be glad! as everything i see is C# with console but i work with ASP.NET WEB APPLICATION (.netframework)
enter code here
namespace gfjsr{
public partial class WebForm1 : System.Web.UI.Page{
phone InsertUserInfo = new phone();
protected void Page_init(object sender, EventArgs e){
string[] myArr = new string[] { "samsung", "IPHONE", "XIAOMI","LG"};
RadioButtonList phoneList = new RadioButtonList();
phoneList.ID = "radioList";
for (int i = 0; i< myArr.Length; i++)
{
ListItem li = new ListItem();
li.Text = myArr[i];
li.Value = i.ToString();
phoneList.Items.Add(li);
}
Panel1.Controls.Add(phoneList);
Label budgetLb = new Label();
budgetLb.ID = "budglb";
budgetLb.Text = "write your budget";
Panel1.Controls.Add(budgetLb);
TextBox insertBudg = new TextBox();
insertBudg.ID = "budgTxt";
Panel1.Controls.Add(insertBudg);
Button myBtn = new Button();
myBtn.ID = "btn1";
myBtn.Click += new EventHandler(btn1_click);
myBtn.Text = "result";
Panel1.Controls.Add(myBtn);
Label Labelfeedback = new Label();
Labelfeedback.ID = "feedback";
Labelfeedback.Text = "";
Panel1.Controls.Add(Labelfeedback);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_click(object sender, EventArgs e)
{
InsertUserInfo.phoneChosen = ((RadioButtonList)FindControl("radioList")).SelectedItem.Text;
double UserBudget =
Convert.ToDouble(((TextBox)FindControl("budgTxt")).Text);
InsertUserInfo.BudgetYN(UserBudget);
((Label)FindControl("feedback")).Text = InsertUserInfo.feedback; }}}
namespace gfjsr{
public class phone{
private string _phoneChosen;
public string phoneChosen
{
get { return _phoneChosen; }
set { _phoneChosen = value; }
}
private string _feedback;
public string feedback
{
get { return _feedback; }
set { _feedback = value; }
}
public double Func1(string x)
{
double phonePrice = 0;
if( x == "samsung")
{
phonePrice = 4000;
}
if (x == "IPHONE")
{
phonePrice = 3500;
}
if (x == "XIAOMI")
{
phonePrice = 3000;
}
if (x == "LG")
{
phonePrice = 2000;
}
return phonePrice;
}
public void BudgetYN(double y)
{
if(y >= Func1(_phoneChosen))
{
_feedback = "positive";
}
else
{
_feedback = "no";
}
}
}
}
I'm trying to use an ajax panel to add keep multiple images added to table cells dynamically. Thing is when I add the second image, the first one dissapears.
Its really just a silly example to try and get ajax controls working for another project.
I'm putting an image of Bill Gates in row 3, column 3 and an image of Steve Jobs in row 1, column 5. I have a button to place each image.
I can't seem to get both to display at the same time.
I have written a function to generate the cell id (GenerateTableCellID), as I've been told I would need to to this. Also there is a function to extract the cell and row in a tuple (GetColumnAndRow).
I'm not sure how to use a Session object to save the data. I thought using AJAX would be the answer, though I think I'm missing a major aspect of it.
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div id="tablePlaceHolder" runat="server"></div>
<asp:Button ID="tblButton2" runat="server" Text="Add Steve Jobs" OnClick="tblButton_Click_Jobs" />
<asp:Button ID="tblButton" runat="server" Text="Add Bill Gates" OnClick="tblButton_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
int tableSize = 5;
var t = new HtmlTable();
t.ID = "myTable";
var placeHolderURL = "http://wiki.tripwireinteractive.com/images/4/47/Placeholder.png";
for (int r = 0; r < tableSize; r++)
{
var tableRow = new HtmlTableRow();
tableRow.ID = "row_" + r.ToString();
for (int c = 0; c < tableSize; c++)
{
var tableCell = new HtmlTableCell();
var id = GenerateTableCellID(r, c);
tableCell.ID = id;
tableCell.Height = "20";
tableCell.Width = "20";
tableCell.InnerHtml = string.Format("<img src='{0}' width='20' height='20' />", placeHolderURL);
tableRow.Controls.Add(tableCell);
}
t.Controls.Add(tableRow);
}
tablePlaceHolder.Controls.Add(t);
}
protected void tblButton_Click(object sender, EventArgs e)
{
int c =2;
int r = 2;
var id = GenerateTableCellID(c, r);
var image = GenerateImage("http://www.mnn.com/sites/default/files/billgates.jpg");
var cell = (HtmlTableCell)UpdatePanel2.FindControl(id);
cell.InnerHtml = "";
cell.Controls.Add(image);
}
protected void tblButton_Click_Jobs(object sender, EventArgs e)
{
int c = 4;
int r = 0;
var id = GenerateTableCellID(c, r);
var image = GenerateImage("http://images.boomsbeat.com/data/images/full/209/jobs-jpg.jpg");
var cell = (HtmlTableCell)UpdatePanel2.FindControl(id);
cell.InnerHtml = "";
cell.Controls.Add(image);
}
protected Image GenerateImage(string url)
{
var image = new Image();
image.ImageUrl = url;
image.Width = 20;
image.Height = 20;
return image;
}
protected string GenerateTableCellID(int c, int r)
{
return "column_" + c.ToString() + "_row_" + r.ToString();
}
protected Tuple<int, int> GetColumnAndRow(string tableCellID)
{
string[] splitString = tableCellID.Split('_');
int column, row;
if (Int32.TryParse(splitString[1], out column) && Int32.TryParse(splitString[3], out row))
{
return new Tuple<int, int>(column, row);
}
else
{
return null;
}
}
It is because at every update you clear the html present before by cell.InnerHtml = ""; remove this and try
protected void tblButton_Click_Jobs(object sender, EventArgs e)
{
int c = 4;
int r = 0;
var id = GenerateTableCellID(c, r);
var image = GenerateImage("http://images.boomsbeat.com/data/images/full/209/jobs-jpg.jpg");
var cell = (HtmlTableCell)UpdatePanel2.FindControl(id);
//cell.InnerHtml = "";
cell.Controls.Add(image);
}
As mentioned on the Page LifyCycle your table is created everytime when you do reload page (does not matter is this postback or not). Also you could read this post. In other words, it is not proper way store your data in the dynamic generated controls, because you lose your data on page load.
But if it is necessary for you could use AJAX methods ($.get and $.post, not UpdatePanel) to get data from the backend and add this to generated control on the client side
I'm trying to get a list of strings from the database.
For each string in the list i want to add a label & textbox to the page.
On button submit I want to collect the textbox value as well as the corresponding label value then save it to the database.
I need help retrieving the values from the textboxes.
What I have so far:
Panel1 is on the aspx page
protected List<string> items = MyClass.GetItems();
protected void Page_Load(object sender, EventArgs e)
{
GenerateItemsTable();
}
private void GenerateItemsTable()
{
Table table = new Table();
table.ID = "Table1";
//PlaceHolder1.Controls.Add(table);
Panel1.Controls.Add(table);
foreach (var x in items)
{
TableRow row = new TableRow();
for (int y = 0; y < 1; y++)
{
TableCell labelCell = new TableCell();
labelCell.Controls.Add(CreateLabel(x));
labelCell.CssClass = "tdLabel";
row.Cells.Add(labelCell);
TableCell txbCell = new TableCell();
txbCell.Controls.Add(CreateRadNumericTextBox(x));
txbCell.Width = 30;
row.Cells.Add(txbCell);
TableCell dataTypeCell = new TableCell();
dataTypeCell.Text = "<span style='font-size: 10px; color: #777'>(student count)</span>";
dataTypeCell.Width = 100;
row.Cells.Add(dataTypeCell);
TableCell fourthCell = new TableCell();
if (x == items[items.Count - 1])
{
RadButton rb = new RadButton();
rb.ID = "submit";
rb.Text = "Submit Guidance";
rb.Skin = "Forest";
rb.Click += new EventHandler(submit_Click);
rb.AutoPostBack = true;
fourthCell.Controls.Add(rb);
row.Cells.Add(fourthCell);
}
else
{
row.Cells.Add(fourthCell);
}
}
table.Rows.Add(row);
}
}
private RadNumericTextBox CreateRadNumericTextBox(string x)
{
RadNumericTextBox rntb = new RadNumericTextBox();
rntb.ID = x;
rntb.Width = 40;
return rntb;
}
private Label CreateLabel(string x)
{
Label l = new Label();
l.ID = "label_" + x;
l.Text = "<label>" + x + "</label>";
return l;
}
protected void submit_Click(object sender, EventArgs e)
{
foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
{
if (x is RadNumericTextBox)
{
//how to get the data??????/
}
}
}
(thanks to those that actually read the whole post)
-----------------updated solution--------------------------------------------
I decided to change it and store the list from the db at page_load. With the list stored i loop through the list and use FindControl() to access the textboxes. Something like this..
//a couple containers
protected class ItemVal
{
public int Value { get; set; }
public string Name { get; set; }
}
protected List<ItemVal> items = new List<ItemVal>();
//get the list from that database
protected void GetItems()
{
foreach (var x in MyClass.GetItems())
{
ItemVal i = new ItemVal();
i.Name = x;
items.Add(i);
}
}
//submit
protected void submit_Click(object sender, EventArgs e)
{
foreach (var x in items)
{
RadNumericTextBox rntb = FindControl(x.Name) as RadNumericTextBox;
x.Value = (int)rntb.Value;
}
}
You need to cast x to a RadNumericTextBox and then pull out the property values you want, like this:
RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;
Then for the other controls you want, you will need to put if conditions for their types, like this:
if (x is Label)
{
Label theLabel = x as Label;
string valLabel = theLabel.Text;
}
Here is the full code for the method:
protected void submit_Click(object sender, EventArgs e)
{
foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
{
Label theLabel;
RadNumericTextBox theRadNumericTextBox;
if (x is RadNumericTextBox)
{
RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;
}
if (x is Label)
{
Label theLabel = x as Label;
string valLabel = theLabel.Text;
}
// Either store up in a list or save to the database on each loop; it is recommended to store a list and send all the changes at once for a database save, but that is your choice
}
}
i imported my data from Excel sheet to grid view.after that i want to put the column header text of my grid view into drop down,but i can't access them
help me plz :-( ;
this is my code but it dose not work :
List<string> lst = new List<string>();
* for (int i = 0; i < dg_excel.Columns.Count; i++)
{
lst.Add(dg_excel.Columns[i].HeaderText);
}
ddl_prd_count.DataSource;
ddl_prd_count.DataBind();
for this * line it says to me there are no columns (column.count = 0)
Check answer on another SO thread Why column count is 0 for GridView to know that why are you not able to access the columns from your grid's Columns collection.
In my opinion, if you want to use auto generated columns then follow this ASP.net forum link - Customizing Auto Generated Columns (GridView) and SO thread How to hide columns in an ASP.NET GridView with auto-generated columns?.
e.g.
have an idea from this code.. i have not checked the proper syntax etc in it.
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (IsFillCombo)
{
//fill your list here.
for (int i = 0; i < datasourcetable.Columns.Count; i++)
{
lst.Add(e.Row.Cells[i].Text);
}
IsFillCombo = false;
}
}
}
// Another simplest way to implement this as below: From Question Text
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Abc> listofAbc = new List<Abc>();
for (int i = 0; i < 10; i++)
{
listofAbc.Add(new Abc
{
ID = i + 1,
Name = "Abc" + (i + 1).ToString()
});
}
GridView1.DataSource = listofAbc;
GridView1.DataBind();
List<string> lst = new List<string>();
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
lst.Add(GridView1.HeaderRow.Cells[i].Text);
System.Diagnostics.Debug.WriteLine(GridView1.HeaderRow.Cells[i].Text);
}
}
}
}
public class Abc
{
public int ID { get; set; }
public string Name { get; set; }
}
you can use following code
List<string> lst = new List<string>();
for (int i = 0; i < dg_excel.HeaderRow.Cells.Count; i++)
{
lst.Add(dg_excel.HeaderRow.Cells[i].Text);
}
ddl_prd_count.DataSource=lst ;
ddl_prd_count.DataBind();
Access gridView1_CellValueChanged event of your grid
and then accesse.Column.FieldName.ToString()
Suppose I have the below class
public partial class invoice
{
public list<lineitem> lineitem;
}
public partial class lineitem
{
private Quantity quantity = new Quantity();
public Quantity Quantity
{
get { return quantity; }
set { quantity = value; }
}
}
How do I bind the value to the gridview.
public void lbluom_OnDataBinding(object sender, System.EventArgs e)
{
for (int i = 0; i < invoicetransmit.Invoice.Count; i++)
{
Label lbl = (Label)sender;
lbl.Text =
invoicetransmit.Invoice[0].LineItem[i].Quantity.Value.ToString();
}
If I do this the values in the gridview are being overwritten with the latest values...and this one below
public void lbluom_OnDataBinding(object sender, System.EventArgs e)
{
for (int i = 0; i < invoicetransmit.Invoice.Count; i++)
{
Label lbl = (Label)sender;
lbl.Text = Eval("Value")
} property does not exist.
}
}
This is how i added values to the grid
public void Addtogrid()
{
//var lineItems = (Session["BillXML"] as InvoiceTransmission).Invoice[0].LineItem;
invoicetransmit.Invoice[0].LineItem.Add(new LineItem {MaterialCode = MaterialCode.Text, ChargeCode = ChargeCode.Text, CostCenter = CostCenter.Text, GLAccount = GLAccount.Text });
//lineItems.Add(new LineItem { MaterialCode=MaterialCode.Text,ChargeCode=ChargeCode.Text,CostCenter=CostCenter.Text,GLAccount=GLAccount.Text});
for (int i = 0; i < invoicetransmit.Invoice[0].LineItem.Count; i++)
{
invoicetransmit.Invoice[0].LineItem[i].Quantity.UOMCode = UOM.Text;
invoicetransmit.Invoice[0].LineItem[i].Quantity.Value = Convert.ToDecimal(Quantity.Text);
invoicetransmit.Invoice[0].LineItem[i].UnitPrice.Value = Convert.ToDecimal(Price.Text);
//invoicetransmit.Invoice[0].LineItem[i].TotalNetAmount = (invoicetransmit.Invoice[0].LineItem[i].Quantity.Value) * (invoicetransmit.Invoice[0].LineItem[i].UnitPrice.Value);
invoicetransmit.Invoice[0].LineItem[i].TotalNetAmount = ( Convert.ToDecimal(Quantity.Text) )* (Convert.ToDecimal(Price.Text));
GridView1.DataSource = invoicetransmit.Invoice[0].LineItem;
// GridView1.DataSource = lineItems;
GridView1.DataBind();
//}
Please help me resolve this question
Try the RowDataBound event
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label) e.Row.FindControl("Label1");
if(lbl!=null)
{
lbl.Text = invoicetransmit.Invoice[0].LineItem[e.Row.RowIndex].Quantity.Value.ToString();
}
}
}