I am trying to create a dynamic Data List bind with database. I can easily create this but I am not able to make the item Command of this Data List. Please help me. Here is my code below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
categorybinding();
}
}
public void categorybinding()
{
int totalcate = (from x in ebooks.books_category select x).Count();
var ra = (from x in ebooks.books_category select x);
DataList dl = new DataList();
dl.ItemTemplate = new DatalistLabelColumnBind();
dl.DataSource = ra;
dl.DataBind();
form1.Controls.Add(dl);
dl.ItemCommand += new DataListCommandEventHandler(this.ItemCommandHandler);
}
public void ItemCommandHandler(object sender, DataListCommandEventArgs e)
{
Response.Redirect("NewPage.aspx?"+e.CommandArgument.ToString());
}
//Create a new class implementing ITemplate
public class DatalistLabelColumnBind : ITemplate
{
public DatalistLabelColumnBind()
{
//Add constructor
}
public void InstantiateIn(Control container)
{
LinkButton label1 = new LinkButton();
label1.DataBinding += new EventHandler(this.BindLabelColumn);
container.Controls.Add(label1);
}
public void BindLabelColumn(object sender, EventArgs e)
{
LinkButton lbl = (LinkButton)sender;
DataListItem container = (DataListItem)lbl.NamingContainer ;
String strVals = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "books_category1"));
lbl.CommandArgument = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "id_books"));
lbl.Text = strVals;
}
}
My Problem :
My Data List easily added on the page but when I click on the Link Button which is added in the Data List is does not Redirect to the NewPage.aspx
Help me out..
I think your Response.Redirect is not resolving to the intended page.
Try:
Response.Redirect("~/NewPage.aspx?"+e.CommandArgument.ToString());
Write categorybinding() function after the if condition in Page_Load. And ItemCommandHandler will definitely work.
Related
When I GridView data Update in other forms then data update but when close this form then gridview data refresh not response automatically in C# win-forms application
Here my Code:
private void button1_Click(object sender, EventArgs e)
{
aCatagory=new tbl_Catagory();
aContext = new RM_Inventory_DataContext();
var product = (from p in aContext.tbl_Catagories
where p.CatagoriesID == Convert.ToDecimal(textBox1.Text)
select p).Single();
product.CatagoriesName = textBox2.Text;
aContext.SubmitChanges();
this.Close();
AddCatagories addCatagories=new AddCatagories();
addCatagories.GridView();
}
public void GridView()
{
gridControl1.DataSource = new RM_PM_Inverntory_Management_System.RM_Inventory_DataContext().tbl_Catagories;
}
You are not binding gridview after assigning the datasource.
public void GridView()
{
gridControl1.DataSource = What ever Data Source You have
gridControl1.DataBind();
}
My problem is that I can get from page_load to show_books function
but I cant get from show_books to book_detail.
Every time I try to fire book_details it gets me to page_load.
my code is this:
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(book_show);
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(book_details);
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
I'm sorry if my question is annoyingly newbie
but I would like some help .
I just started to learn asp.net
well i don't understand why you need to do this,but code below works,hope it helps
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(this.book_show);
lnk_button.CommandName = "testClick";
lnk_button.CommandArgument = "1";
lnk_button.ID = "11";
lnk_button.Text = "blah";
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
public void book_show()
{
book_details() ;
}
public void book_details()
{
}
I'm new to ASP.Net, C#, and OOP in general, and I'm trying to get into the right paradigm about classes, objects, methods, etc.
I would like to access a DataTable from the Repeater.ItemDataBound event. Where would I create the DataTable so the method can access it? I don't want the DataTable created everytime ItemDataBound is called, just once. Would that be a separate class, or another method in the same class, or something else?
I want to use this to set values on controls in the HeaderTemplate of a Repeater. Here is my code:
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int numPages = 3, numItems = 10;
int[] parentRepeatCnt = Enumerable.Range(0, numPages).ToArray();
int[] childRepeatCnt = Enumerable.Range(0, numItems).ToArray();
ParentRepeater.DataSource = parentRepeatCnt;
ParentRepeater.DataBind();
foreach (int i in parentRepeatCnt)
{
Repeater ChildRepeater = ParentRepeater.Items[i].FindControl("ChildRepeater") as Repeater;
ChildRepeater.DataSource = childRepeatCnt;
ChildRepeater.DataBind();
}
}
public void ChildRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label Label1 = e.Item.FindControl("Label1") as Label;
// access DataTable here
Label1.Text = myDataTable.Rows[0]["item"].ToString();
}
}
}
Also, please feel free to critique my existing code. Thanks!
public partial class test : System.Web.UI.Page
{
public datatable mydatatable = new datatable();
protected void Page_Load(object sender, EventArgs e)
{
int numPages = 3, numItems = 10;
int[] parentRepeatCnt = Enumerable.Range(0, numPages).ToArray();
int[] childRepeatCnt = Enumerable.Range(0, numItems).ToArray();
ParentRepeater.DataSource = parentRepeatCnt;
ParentRepeater.DataBind();
foreach (int i in parentRepeatCnt)
{
Repeater ChildRepeater = ParentRepeater.Items[i].FindControl("ChildRepeater") as Repeater;
ChildRepeater.DataSource = childRepeatCnt;
ChildRepeater.DataBind();
}
}
public void ChildRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label Label1 = e.Item.FindControl("Label1") as Label;
// access DataTable here
Label1.Text = myDataTable.Rows[0]["item"].ToString();
}
}
}
I am using DevExpress in my winform application, I have a gridview, data entry form, datanavigator, all bound to dataset.
I want to add new record, if using datanavigator "Add" it works good, how to do the same using a "New Record" button?
BindingSource.AddNew()
is not working, it usually does, but with devexpress its not working.
If you want to use binding then use your objects with binding source..
and use the binding list .AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event to add new entity object ..
See the example of BindingList on MSDN.
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm Controls works so fast with binding sources as compare to typed datasources etc... YOu can implement bindingSources using these example..
set gridview and the associcated controls datasource to bindsouce that you have created...
process your form with the this MSDN example..
have a look on this code snippet.. may be you will get some idea from this..
private void BindingLIstDemo_Load(object sender, EventArgs e)
{
InitializeListOfEmployees();
BindlstEmp();
listofEmp.AddingNew += new AddingNewEventHandler(listOfEmp_AddingNew);
listofEmp.ListChanged += new ListChangedEventHandler(listofEmp_ListChanged);
}
private void BindlstEmp()
{
lstEmpList.Items.Clear();
lstEmpList.DataSource = listofEmp;
lstEmpList.DisplayMember = "Name";
}
void listofEmp_ListChanged(object sender, ListChangedEventArgs e)
{
MessageBox.Show(e.ListChangedType.ToString());
//throw new NotImplementedException();
}
//declare list of employees
BindingList<Emp> listofEmp;
private void InitializeListOfEmployees()
{
//throw new NotImplementedException();
// Create the new BindingList of Employees.
listofEmp = new BindingList<Emp>();
// Allow new Employee to be added, but not removed once committed.
listofEmp.AllowNew = true;
listofEmp.AllowRemove = true;
// Raise ListChanged events when new Employees are added.
listofEmp.RaiseListChangedEvents = true;
// Do not allow Employee to be edited.
listofEmp.AllowEdit = false;
listofEmp.Add(new Emp(1, "Niranjan", 10000));
listofEmp .Add (new Emp (2,"Jai", 8000));
}
// Create a new Employee from the text in the two text boxes.
void listOfEmp_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Emp (Convert.ToInt32(txtId.Text), txtName.Text,Convert.ToInt32(txtSalary.Text));
}
private void btnAdd_Click(object sender, EventArgs e)
{
Emp empItem = listofEmp.AddNew();
txtId.Text = txtName.Text = txtSalary.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 obj = new Form1();
obj.Show();
}
private void btnDelete_Click(object sender, EventArgs e)
{
var sg = (from sc in listofEmp.ToList<Emp>() where sc.Name == ((Emp)lstEmpList.SelectedValue).Name select sc);
}
private void lstEmpList_SelectedIndexChanged(object sender, EventArgs e)
{
Emp se = listofEmp[lstEmpList.SelectedIndex];
txtId.Text = se.Id.ToString();
txtName.Text = se.Name;
txtSalary.Text = se.Salary.ToString();
}
Here I am using BindingList as datasouce BindingList<Emp> listofEmp; and on the place of grid listing of records are shown in a listbox control.. but all same...try this with your gridview..
I've created a simple ASPX page that lists records in a GridView. The records are a list of incidents and one of the columns is the ID of the person who reported the incident.
The initial page shows all records but I would like to provide a filter for the ReportedBy column. I've gotten this working by allowing the user to type in the ReportedByID in a textbox and then clicking on the submit button. This refreshes the page as expected with the filtered view.
The code for this page is as follows:
public MyPage()
{
this.Load += new EventHandler(Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
DataAccessObj daObj = new DataAccessObj();
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
int reportedById = 0;
if (int.TryParse(txtReportedById.Text, out reportedById) == false)
{
reportedById = 0;
}
DataAccessObj daObj = new DataAccessObj();
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(reportedById);
IncidentGrid.DataBind();
}
To make it more user friendly, I decided to add a dropdown box populated with the ReportedBy names for the user to select which would then be used to filter on upon clicking the submit button. The dropdown box has names as the display items but the values should still be set to the IDs.
The problem I have is that the ID number I get from the dropdown box always comes up as the first element of the list rather than the one the user selected at the time they clicked on the submit button.
The code for this page with this implementation is as follows:
public MyPage()
{
this.Load += new EventHandler(Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
DataAccessObj daObj = new DataAccessObj();
foreach (ReportedByItem repByItem in daObj.GetAllReportedBy())
{
ListItem listItem = new ListItem(repByItem.Name, repByItem.Id.ToString());
combobox.Items.Add(listItem);
}
if (IsPostBack == false)
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
int reportedById = 0;
if (combobox.SelectedItem != null)
{
if (int.TryParse(combobox.SelectedItem.Value, out reportedById) == false)
{
reportedById = 0;
}
}
DataAccessObj daObj = new DataAccessObj();
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(reportedById);
IncidentGrid.DataBind();
}
Any help would be gratefully appreciated. TIA
Keep in mind that with WebForms the Page_Load code is executed before the event handler code for the control which created the postback.
You have to populate the list in the section where postbacks flags are checked, just like you do with the grid.
if (IsPostBack == false){
//bind the combobox
}
Otherwise, on a postback, the list will re-populate and the selection will be gone.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataAccessObj daObj = new DataAccessObj();
foreach (ReportedByItem repByItem in daObj.GetAllReportedBy())
{
ListItem listItem = new ListItem(repByItem.Name, repByItem.Id.ToString());
combobox.Items.Add(listItem);
}
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}