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();
}
}
}
Related
This is the error, how can I correct it. Anyone ..help
I want to arrange column names in Gridview, I wrote the code as C#, but it didn't work.
namespace ip_web
{
public partial class login : System.Web.UI.Page
{
private void dgv1()
{
GridView1.Columns["no"].DisplayIndex = 0;
GridView1.Columns["batch"].DisplayIndex = 1;
GridView1.Columns["degree"].DisplayIndex = 2;
GridView1.Columns["module"].DisplayIndex = 3;
// dataGridView1.Columns["lecturer/instructor"].DisplayIndex = 4;
GridView1.Columns["date"].DisplayIndex = 5;
GridView1.Columns["time"].DisplayIndex = 6;
}
protected void Page_Load(object sender, EventArgs e)
{
Service1Client com = new Service1Client();
GridView1.DataSource = com.GetComTimeTable();
GridView1.DataBind();
}
}
}
According to your tag asp.net and your example source, you have used GridView. Unfortunately there is not any property DisplayIndex in GridView. Please check GridView Class.
The property DisplayIndex exists in DataGridView, it's a WinForms control. Please check DataGridView Class. And DataGridView allow to get a column by using name and index both. Please check below image;
private void dgv1()//show patient table
{
dataGridView1.Columns["id"].DisplayIndex = 0;
dataGridView1.Columns["name"].DisplayIndex = 1;
dataGridView1.Columns["gender"].DisplayIndex = 2;
dataGridView1.Columns["age"].DisplayIndex = 3;
dataGridView1.Columns["phone"].DisplayIndex = 4;
}
public void patientTableView()
{ //add to dataGridView
Service1Client ptn = new Service1Client();
dataGridView1.DataSource = ptn.GetpatientTable();
}
private void Form1_Load(object sender, EventArgs e)
{//call the method when form loading
patientTableView();
}
I have filled listBox with data and assigning it to gridview but it doesn't. I verified by setting count of list to variable and it shows 3, perfect but after assigning it to gridview, the count of gridview shows 0. Why ?
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count; //returns 3
grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count; //return 0
}
}
Solved
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count;
//grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
//grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count;
ListItemCollection lstTempFeatures = ListBoxFeatures.Items;
DataTable dTempFeatures = new DataTable();
dTempFeatures.Columns.Add("ID");
dTempFeatures.Columns.Add("FeatureName");
foreach (ListItem lstItem in lstTempFeatures)
{
DataRow dr = dTempFeatures.NewRow();
dr["ID"]= lstItem.Value;
dr["FeatureName"] = lstItem.Text;
dTempFeatures.Rows.Add(dr);
}
grdViewTemporaryFeatures.DataSource = dTempFeatures;
grdViewTemporaryFeatures.DataBind();
mdlTemporaryFeatures.Show();
}
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.
I have a dropdown which is bound to a database. On its index change there is a function that add some button in a panel based upon selected value.
I am reading those button in page_init event but still I get null values, i.e. event bound with the button never fires.
Here is my code and dropdownlist1 is the dropdown that is adding dynamic button.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
colorgroupsTableAdapters.master_color_groupTableAdapter ta
= new colorgroupsTableAdapters.master_color_groupTableAdapter();
DataTable dt = ta.GetData();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = dt.Columns[1].ToString();
DropDownList1.DataValueField = dt.Columns[0].ToString();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select One", "0"));
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
bindcolors();
}
}
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
}
protected void DropDownList2_DataBound(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex < 1)
{
DropDownList2.Items.Clear();
}
DropDownList2.Items.Insert(0, new ListItem("Select One", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["dd"] = DropDownList1.SelectedIndex;
bindcolors();
}
void bindcolors()
{
if (DropDownList1.SelectedIndex > 0)
{
addcolorgroupsTableAdapters.groupavailablecolorTableAdapter ta
= new addcolorgroupsTableAdapters.groupavailablecolorTableAdapter();
DataTable dt = ta.GetData(int.Parse(DropDownList1.SelectedValue));
HtmlTable ht = new HtmlTable();
ht.Width = "90%";
ht.Border = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc1 = new HtmlTableCell();
HtmlTableCell tc2 = new HtmlTableCell();
HtmlTableCell tc3 = new HtmlTableCell();
object[] ob = dt.Rows[i].ItemArray;
tc1.InnerHtml = ob[0].ToString();
tc2.InnerHtml = ob[1].ToString();
tc2.BgColor = "#" + ob[1].ToString();
Button b = new Button();
b.Text = "Remove";
b.CommandArgument = ob[0].ToString();
AjaxControlToolkit.ConfirmButtonExtender cb
= new AjaxControlToolkit.ConfirmButtonExtender();
cb.ConfirmText = "Are You Sure To Delete This Color From The Group?";
b.ID = "Bo" + ob[0].ToString();
b.EnableViewState = true;
b.Click += new EventHandler(b_Click);
cb.TargetControlID = "Bo" + ob[0].ToString();
tc3.Controls.Add(b);
tc3.Controls.Add(cb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
ht.Rows.Add(tr);
}
Panel1.Controls.Add(ht);
}
}
void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
int grp = int.Parse(DropDownList1.SelectedValue);
int clid = int.Parse(b.CommandArgument);
addcolorgroupsTableAdapters.QueriesTableAdapter ta
= new addcolorgroupsTableAdapters.QueriesTableAdapter();
ta.DeleteQuery_group_color(grp, clid);
DropDownList2.DataBind();
bindcolors();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0 && DropDownList2.SelectedIndex > 0)
{
int grp = int.Parse(DropDownList1.SelectedValue);
int clid = int.Parse(DropDownList2.SelectedValue);
addcolorgroupsTableAdapters.QueriesTableAdapter ta
= new addcolorgroupsTableAdapters.QueriesTableAdapter();
ta.Insert_into_group_color(grp, clid);
DropDownList2.DataBind();
bindcolors();
}
}
Please tell what I am doing wrong?
I think the problem is the check for SelectedIndex > 0 in bindControls. The reason is that ViewState is evaluated between the Init and Load so the value for the SelectedIndex property is not set yet (and therefore = -1).
You could try a different approach: use a Repeater control that is databound to the results of the database query. This way, the general structure can be defined in the Repeater and its ItemTemplate. Also the EventHandlers are registered in the ItemTemplate.
You can reset the DataSource of the Repeater whenever the SelectedIndex of the Combobox changes and do not need to recreate the controls dynamically in init.
Your code should be much shorter and behave more deterministic.
I have a simple task. first one Dropdownlist control is there where country name is loaded. After selecting country name, dynamically Dropdownlist will be loaded with corresponding state, after selecting state, dynamically another Dropdownlist will be added with relevant district. the problem is that the dynamically selected-index event is not fired. I searched it so many pages, but not find any suitable answer. can any one answer it for written code.
This code worked fine in static controls. but not dynamic controls.
Can any one correct my code
namespace fireProgram
{
public partial class MindforeSystemTestingProgram : System.Web.UI.Page
{
BALayer objBALayer = new BALayer();
DropDownList ddlState=new DropDownList();
DropDownList ddlDistrict=new DropDownList();
protected void Page_Init(EventArgs e)
{
ddlState.ID = "ddlState";
ddlState.AutoPostBack = true;
ddlState.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
panel1.Controls.AddAt(2, ddlState);
ddlDistrict.ID = "ddlDistrict";
panel1.Controls.AddAt(3, ddlDistrict);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCountry.DataSource = objBALayer.GetCountry();
ddlCountry.DataTextField = "Text";
ddlCountry.DataValueField = "Value";
ddlCountry.DataBind();
}
//else
//{
// ddlState.ID = "ddlState";
// ddlState.AutoPostBack = true;
// ddlState.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
// panel1.Controls.AddAt(2, ddlState);
// //ddlDistrict.ID = "ddlDistrict";
// //panel1.Controls.AddAt(3, ddlDistrict);
//}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int value = Convert.ToInt32(ddlCountry.SelectedValue);
panel1.Controls.AddAt(2, ddlState);
//DropDownList ddlState = new DropDownList();
//ddlState.AutoPostBack = true;
if (value != 0)
{
ddlState.DataSource = objBALayer.GetState(value);
ddlState.DataTextField = "Text";
ddlState.DataValueField = "Value";
ddlState.DataBind();
}
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int value = Convert.ToInt32(ddlState.SelectedValue);
//DropDownList ddlDistrict = new DropDownList()
panel1.Controls.AddAt(3, ddlDistrict);
if (value != 0)
{
ddlDistrict.DataSource = objBALayer.GetDistrict(value);
ddlDistrict.DataTextField = "Text";
ddlDistrict.DataValueField = "Value";
ddlDistrict.DataBind();
}
}
}
}