In an ASP.NET project i have 2 textboxes and a submit button.
In the event handler of button clicked i want to save the values of the textboxes to a datatable and then bind the datatable to a Gridview.
This has to happen multiple times. But every time the datatable has one row, like being overwritten every time the event handler fires. It's like the datatable is being created from the beginning every time the event handler fires. Code below. Thanks for your time.
EDIT: Thanks for all the answers you gave.
protected void BtnInsertCustomerLegalRelationsInsert_Click(object sender, EventArgs e)
{
string FullCompanyName = TbxFullCompanyName.Text.Trim();
object LegalRelationAfm = TbxLegalRelationAfm.Text.Trim();
if (dtCustomersLegalRelations.Columns.Count == 0)
{
dtCustomersLegalRelations.Columns.Add("FullCompanyName",typeof(string));
dtCustomersLegalRelations.Columns.Add("LegalRelationAfm",typeof(string));
}
DataRow dr = dtCustomersLegalRelations.NewRow();
dr["FullCompanyName"] = FullCompanyName;
dr["LegalRelationAfm"] = LegalRelationAfm;
dtCustomersLegalRelations.Rows.Add(dr);
GvCustomerRegalRelations.DataSource = dtCustomersLegalRelations;
GvCustomerRegalRelations.DataBind();
}
The whole code behind here
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ConnectedBorrowers_CustomerBasicInput : System.Web.UI.UserControl
{
DataTable dtCustomersLegalRelations = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
}
}
protected void CbMandatoryAFM_CheckedChanged(object sender, EventArgs e)
{
if (CbMandatoryAFM.Checked == true)
{
TbxCustAfm.ReadOnly = true;
TbxCustAfm.BackColor = Color.LightGray;
}
else
{
TbxCustAfm.ReadOnly = false;
TbxCustAfm.BackColor = Color.White;
}
}
protected void CbLegalRelationsMandatoryAFM_CheckedChanged(object sender, EventArgs e)
{
if (CbLegalRelationsMandatoryAFM.Checked == true)
{
TbxLegalRelationAfm.ReadOnly = true;
TbxLegalRelationAfm.BackColor = Color.LightGray;
}
else
{
TbxLegalRelationAfm.ReadOnly = false;
TbxLegalRelationAfm.BackColor = Color.White;
}
}
protected void CbLegalRelations_CheckedChanged(object sender, EventArgs e)
{
if (CbLegalRelations.Checked == true)
{
TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = true;
}
else
{
TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
}
}
protected void BtnInsertCustomerLegalRelationsInsert_Click(object sender, EventArgs e)
{
try
{
string FullCompanyName = TbxFullCompanyName.Text.Trim();
object LegalRelationAfm = TbxLegalRelationAfm.Text.Trim();
if (dtCustomersLegalRelations.Columns.Count == 0)
{
dtCustomersLegalRelations.Columns.Add("FullCompanyName",typeof(string));
dtCustomersLegalRelations.Columns.Add("LegalRelationAfm",typeof(string));
}
DataRow dr = dtCustomersLegalRelations.NewRow();
dr["FullCompanyName"] = FullCompanyName;
dr["LegalRelationAfm"] = LegalRelationAfm;
dtCustomersLegalRelations.Rows.Add(dr);
GvCustomerRegalRelations.DataSource = dtCustomersLegalRelations;
GvCustomerRegalRelations.DataBind();
}
catch (Exception ex)
{
((Label)this.Page.Master.FindControl("LblError")).Text = ex.Message;
}
}
Try such approach:
public partial class ConnectedBorrowers_CustomerBasicInput : System.Web.UI.UserControl
{
DataTable dtCustomersLegalRelations;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dtCustomersLegalRelations = new DataTable();
Session["table"] = dtCustomersLegalRelations;
TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
} else {
dtCustomersLegalRelations = Session["table"] as DataTable;
}
}
...
}
The value of 'dtCustomersLegalRelations' will not persist through PostBack events - so when you add the row on the end of it, you're doing so on a new instance.
You need to fetch all the data back out of the GridView before you add the new row and re-bind it.
st4hoo's answer above should sort it for you.
When u click the button postback occurs. During each postback your datatable is created from the beginning. So u will lose ur old data.
So one option is that u can keep the datatable with data in a session and retrieve the datatable from the session during the next button click and add the new row.
At what point do you create the datatable (dtCustomersLegalRelations)? Could you paste the complete code including the cration of the datatable and the button_click?
Related
I've watched a tutorial on youtube that explain how to search in an excel file but when I search for a specific value in a row I must type at the search box column name = 'value at the row' and the column name must be a single word, not 2 words split with space or I must write column name like '%value%' to get similar results.
when I search for a specific value in a row
to search for similar results
column name must be a single word
First: How I can search by writing a specific keyword at any row.
Second: How I can load column names at combo box and search by column name and make it optional to select column.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnOpen_Click(object sender, EventArgs e)
{
using(OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xlsx",Multiselect = false})
{
if (ofd.ShowDialog() == DialogResult.OK)
{
Cursor.Current = Cursors.WaitCursor;
DataTable dt = new DataTable();
using(XLWorkbook workbook = new XLWorkbook(ofd.FileName))
{
bool isFirstRow = true;
var rows = workbook.Worksheet(1).RowsUsed();
foreach(var row in rows)
{
if (isFirstRow)
{
foreach (IXLCell cell in row.Cells())
dt.Columns.Add(cell.Value.ToString());
isFirstRow = false;
}
else
{
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
dt.Rows[dt.Rows.Count - 1][i++] = cell.Value.ToString();
}
}
dataGridView1.DataSource = dt.DefaultView;
lblTotal.Text = $"Total Records:{dataGridView1.RowCount}";
Cursor.Current = Cursors.Default;
}
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
DataView dv = dataGridView1.DataSource as DataView;
if (dv != null)
dv.RowFilter = txtSearch.Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
btnSearch.PerformClick();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
static string ColumnNumberToColumnLetter(int colIndex)
{
int colnum = colIndex;
string columnLetter = null;
int mod = 0;
while (colnum > 0)
{
mod = (colnum - 1) % 26;
columnLetter = (char)(65 + mod) + columnLetter;
colnum = (int)((colnum - mod) / 26);
}
return columnLetter;
}
string columnLetter = ColumnNumberToColumnLetter(27); //return AA
I am trying to save retrieve and update data table from s single web form. Data is saved and retrieve correctly but is not updated.
Here is the code
using System;
using System.Web.UI;
using System.Data;
public partial class Basic_Information : System.Web.UI.Page
{
DataTable user_dt, personal_dt = new DataTable();
string u_id;
Boolean check = false;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
if (Session["user"] != null)
load();
}
protected void btn_save_Click(object sender, EventArgs e)
{
if(check == false)
savedata();
else
editdata();
}
protected void load()
{
user_dt = (DataTable)Session["user"];
u_id = user_dt.Rows[0][0].ToString();
personal_dt = (DataTable)Session["personal"];
if (personal_dt.Rows.Count != 0)
{
retrivedata();
check = true;
}
}
protected void savedata()
{
personal_dt.Columns.Add("User_id");
personal_dt.Columns.Add("Name");
personal_dt.Columns.Add("Father_Name");
personal_dt.Columns.Add("CNIC");
personal_dt.Columns.Add("Date_of_Birth");
personal_dt.Columns.Add("Domicile");
personal_dt.Columns.Add("Gender");
personal_dt.Columns.Add("Religion");
personal_dt.Columns.Add("Nationality");
personal_dt.Columns.Add("Marital");
DataRow per_row = personal_dt.NewRow();
per_row["User_id"] = u_id;
per_row["Name"] = txt_name.Text;
per_row["Father_Name"] = txt_fname.Text;
per_row["CNIC"] = txt_cnic.Text;
per_row["Date_of_Birth"] = txt_dofb.Text;
per_row["Domicile"] = ddl_domicil.SelectedItem.Text;
per_row["Gender"] = ddl_gender.SelectedItem.Text;
per_row["Religion"] = ddl_religion.SelectedItem.Text;
per_row["Nationality"] = txt_nationality.Text;
per_row["Marital"] = ddl_marital.SelectedItem.Text;
personal_dt.Rows.Add(per_row);
string script = "alert('Profile Successfully Saved');";
ClientScript.RegisterStartupScript(this.GetType(), "saved", script, true);
}
}
protected void retrivedata()
{
txt_name.Text = personal_dt.Rows[0]["Name"].ToString();
txt_fname.Text = personal_dt.Rows[0]["Father_Name"].ToString();
txt_cnic.Text = personal_dt.Rows[0]["CNIC"].ToString();
txt_dofb.Text = personal_dt.Rows[0]["Date_of_Birth"].ToString();
ddl_domicil.Items.FindByText(personal_dt.Rows[0] ["Domicile"].ToString()).Selected = true;
ddl_gender.Items.FindByText(personal_dt.Rows[0]["Gender"].ToString()).Selected = true;
ddl_religion.Items.FindByText(personal_dt.Rows[0]["Religion"].ToString()).Selected = true;
txt_nationality.Text = personal_dt.Rows[0]["Nationality"].ToString();
ddl_marital.Items.FindByText(personal_dt.Rows[0]["Marital"].ToString()).Selected = true;
}
protected void editdata()
{
personal_dt.Rows[0]["Name"] = txt_name.Text;
personal_dt.Rows[0]["Father_Name"] = txt_fname.Text;
personal_dt.Rows[0]["CNIC"] = txt_cnic.Text;
personal_dt.Rows[0]["Date_of_Birth"] = txt_dofb.Text;
personal_dt.Rows[0]["Domicile"] = ddl_domicil.SelectedItem.Text;
personal_dt.Rows[0]["Gender"] = ddl_gender.SelectedItem.Text;
personal_dt.Rows[0]["Religion"] = ddl_religion.SelectedItem.Text;
personal_dt.Rows[0]["Nationality"] = txt_nationality.Text;
personal_dt.Rows[0]["Marital"] = ddl_marital.SelectedItem.Text;
}
}
Saving data correctly, retrieving data correctly.
But bug in editing data return the first values which are retrieved.
Also an error that Dropdown cannot select multiple items
Regarding dropdown binding issue, before
Dropdown.DataSource = YourDataSource
add
Dropdown.ClearSelection();
Try this.After filling data to datatable you need to pass that datatable, and update record.
protected void editdata()
{
DataRow dr = personal_dt.NewRow();
personal_dt.Rows.Add(dr);
dr["Name"] = txt_name.Text;
dr["Father_Name"] = txt_fname.Text;
dr["CNIC"] = txt_cnic.Text;
dr["Date_of_Birth"] = txt_dofb.Text;
dr["Domicile"] = ddl_domicil.SelectedItem.Text;
dr["Gender"] = ddl_gender.SelectedItem.Text;
dr["Religion"] = ddl_religion.SelectedItem.Text;
dr["Nationality"] = txt_nationality.Text;
dr["Marital"] = ddl_marital.SelectedItem.Text;
}
First time poster, long time lurker. I am having some trouble with my ASP.NET page, and I hope someone can help me resolve my issue.
Basically, I have a bunch of checkboxes in a gridview, and two buttons: a 'find' button, and a 'save' button. The 'find' can set the value of the checkbox, but if a user unchecks it, I want to capture that change when the user hits 'save'. Currently, it does not work.
Relevant ASPX:
<%# Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeBehind="FindTransactions.aspx.cs" Inherits="Basic.FindTransactions" MasterPageFile="~/Trans.Master" %>
Relevant Code Behind here:
Page:
public partial class FindTransactions : System.Web.UI.Page
{
GridView _gridview = new GridView() { ID = "_gridView" };
DataTable _datatable = new DataTable();
Int32 _buyerID = new Int32();
protected void Page_Load(object sender, EventArgs e)
{
}
"Find" button:
protected void Find_Click(object sender, EventArgs e)
{
//truncated
_datatable.Rows.Add(
//filled with other data from a custom object.
);
ViewState["_datatable"] = _datatable;
ViewState["_buyerID"] = _buyerID;
BuildGridView((DataTable)ViewState["_datatable"],(Int32)ViewState["buyerID"]);
}
BuildGridView function:
protected void BuildGridView(DataTable d, Int32 b)
{
_gridview.DataKeyNames = new String[] {"Transaction ID"};
_gridview.AutoGenerateColumns = false;
_gridview.RowDataBound += new GridViewRowEventHandler(OnRowDataBound);
for(Int32 i = 0; i < d.Columns.Count; i++)
{
Boundfield boundfield = new BoundField();
boundfield.DataField = d.Columns[i].ColumnName.ToString();
boundfield.HeaderText = d.Columns[i].ColumnName.ToString();
_gridview.Columns.Add(boundfield);
}
_gridview.DataSource = d;
_gridview.DataBind();
//truncated
Panel1.Controls.Add(_gridview);
}
Row Bound Event handler:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String controlID = "checkBox";
CheckBox c = new CheckBox() { ID = controlID};
c.Enabled = true;
Boolean success;
Boolean v;
success = Boolean.TryParse(e.Row.Cells[8].Text, out v);
e.Row.Cells[8].Controls.Add(c);
if (success)
{
c.Checked = v;
if (c.Checked)
{
//Will uncomment once other things work
//e.Row.Visible = false;
}
}
else
{
c.Checked = false;
}
}
}
All of that works. Here is where it starts to break down:
"Save" button:
protected void Save_Click(object sender, EventArgs e)
{
//Both for troubleshooting and both return 0. (Expected for datatable)
Label1.Text = _gridview.Rows.Count.ToString();
Label2.Text = _datatable.Rows.Count.ToString();
/*truncated
*/
if (grid.Rows.Count == 0)
{
BuildGridView((DataTable)ViewState["infoTable"], (Int32)ViewState["guestID"]);
}
foreach (GridViewRow r in grid.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)r.FindControl("checkBox");
if (cb != null && cb.Checked)
{
//This never seems to modify the label.
//Will put code to modify database here.
Label2.Text += "Hi " + r.RowIndex.ToString();
}
}
}
}
After I hit the save button, PostBack occurs and GridView is empty (Rows.Count is 0). ViewState appears to be lost before I get a chance to loop through the GridView rows to determine the checkbox values.
At the end of it all, I just want to capture the status of those checkboxes, changed by user interaction or not, by hitting the 'Save' button.
I found some other articles, but a lot of them haven't worked when I tried implementing the various fixes.
This one seems to be the closest that describes my issue, and the code is structured similarly, but I don't quite understand how to implement the fix: GridView doesn't remember state between postbacks
[New simplified code to illustrate problem:]
namespace GridViewIssue
{
public partial class GridViewNoMaster : System.Web.UI.Page
{
GridView _gridView = new GridView() { ID = "_gridView" };
DataTable _dataTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Find_Click(object sender, EventArgs e)
{
BuildDataTable();
List<String> list = new List<String>();
list.Add("1");
list.Add("User");
list.Add("10/12/2014");
foreach (String s in list)
{
_dataTable.Rows.Add(
list[0],
list[1],
list[2]
);
}
BuildGridView();
//Feedback.Text = _gridView.Rows.Count.ToString();
}
protected void Save_Click(object sender, EventArgs e)
{
Feedback.Text = "Save Clicked, PostBack: " + IsPostBack + ", GridView Row Count: " + _gridView.Rows.Count + ", GridView ViewState: " + _gridView.EnableViewState;
foreach (GridViewRow r in _gridView.Rows)
{
if(r.RowType == DataControlRowType.DataRow)
{
Feedback.Text = "In DataRow type" + _gridView.Rows.Count;
}
}
}
protected void BuildDataTable()
{
_dataTable.Columns.Add("Transaction ID", typeof(String));
_dataTable.Columns.Add("Name", typeof(String));
_dataTable.Columns.Add("Date", typeof(String));
}
protected void BuildGridView()
{
for (Int32 i = 0; i < _dataTable.Columns.Count; i++)
{
BoundField b = new BoundField();
b.DataField = _dataTable.Columns[i].ColumnName.ToString();
b.HeaderText = _dataTable.Columns[i].ColumnName.ToString();
_gridView.Columns.Add(b);
}
_gridView.DataKeyNames = new String[] { "Transaction ID" };
_gridView.AutoGenerateColumns = false;
_gridView.DataSource = _dataTable;
_gridView.DataBind();
Panel1.Controls.Add(_gridView);
}
}
}
Hi this is my aspx page loading some values to the user control
protected void Page_Load(object sender, EventArgs e)
{
}
this is the usercontrol where i am loading and sending the values in find click event
protected void BtnFind_Click(object sender, EventArgs e)
{
Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.Date = txtDate.Text.Trim();
BPOP.DocNo = txtDocNo.Text.Trim();
BPOP.Code = txtCode.Text.Trim();
BPOP.Name = txtName.Text.Trim();
BPOP.Partcode = txtPartNo.Text.Trim();
if (chkReprint.Checked)
{
BPOP.BtnReprintVisible = true;
BPOP.BtnSaveVisible = false;
}
divControls.Controls.Clear();
PlaceHolder1.Controls.Add(BPOP);
}
this is my Usr_BPOP.ascx:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnReprint.Click += new EventHandler(btnReprint_Click);
}
btnReprint.Visible = false;
btnSave.Visible = BtnSaveVisible;
btnReprint.Visible = BtnReprintVisible;
if (btnReprint.Visible == false)
{
btnReprint.Text = "Print";
btnReprint.Visible = true;
}
table = new DataTable();
table.Columns.Add("DocNum", typeof(string));
table.Columns.Add("DocEntry", typeof(string));
table.Columns.Add("LineNum", typeof(string));
table.Columns.Add("PartNo", typeof(string));
table.Columns.Add("ItemDesc", typeof(string));
table.Columns.Add("QTR", typeof(string));
table.Columns.Add("QTP", typeof(string));
table.Columns.Add("Chk", typeof(bool));
table.Columns.Add("BarCode", typeof(string));
Datalayer dl = new Datalayer();
DataTable dttable = new DataTable();
if (!BtnSaveVisible && BtnReprintVisible)
BtnSaveVisible = true;
dttable = dl.GetPOItem(date, docNo, code, name, partcode, BtnReprintVisible, !BtnSaveVisible).Tables[0];
foreach (DataRow dr in dttable.Rows)
{
table.Rows.Add(dr["DocNum"].ToString(), dr["DocEntry"].ToString(), dr["LineNum"].ToString(), dr["PartNo"].ToString(),
dr["ItemDesc"].ToString(), dr["QTR"].ToString(), dr["QTP"].ToString(), Convert.ToBoolean(dr["Chk"]), dr["Barcode"].ToString());
}
if (table != null && table.Rows.Count > 0)
{
grdlistofitems.DataSource = table;
Session["Table"] = table;
grdlistofitems.DataBind();
}
else
{
}
}
this is the reprint button click event when i cilck this event it is not firing:
void btnReprint_Click(object sender, EventArgs e)
{
}
Since you are not setting the ID of the control, it is generated anew every time the control added to the page. The generated ID might not be the same, and therefore the sender of the event cannot be recognized. So first thing you should do is assign an ID explicitly:
Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.ID = "SomeID";
Secondly, assignment of the event handler should be done very time the control is created - that is, on every request, does not matter whether this is a postback or not - otherwise ASP.NET will not be able to determine what method should be called when the event is fired:
protected void Page_Load(object sender, EventArgs e)
{
// No check for postback here
btnReprint.Click += new EventHandler(btnReprint_Click);
Update. There is one more reason why this code does not behave as expected. The BPOP control is added to the page only on btnFind click. When the postback is caused by anything else, including btnReprint, on the response page generation BPOP control is not added to the page at all. If there is no control on the page - obviously its methods, including event handlers, cannot be triggered.
Here is quick and dirty fix for this situation. It should be applied to the page code where BPOP control is added:
protected void Page_Load(object sender, EventArgs e)
{
bool? addBPOP = ViewState["AddBPOP"] as bool?;
if (addBPOP.HasValue && addBPOP.Value)
{
AddBPOP();
}
}
protected void BtnFind_Click(object sender, EventArgs e)
{
AddBPOP();
ViewState["AddBPOP"] = true;
}
protected void AddBPOP()
{
Usr_BPOP BPOP = (Usr_BPOP)Page.LoadControl("~/Usr_BPOP.ascx");
BPOP.ID = "BPOPID";
BPOP.Date = txtDate.Text.Trim();
BPOP.DocNo = txtDocNo.Text.Trim();
BPOP.Code = txtCode.Text.Trim();
BPOP.Name = txtName.Text.Trim();
BPOP.Partcode = txtPartNo.Text.Trim();
if (chkReprint.Checked)
{
BPOP.BtnReprintVisible = true;
BPOP.BtnSaveVisible = false;
}
divControls.Controls.Clear();
PlaceHolder1.Controls.Add(BPOP);
}
Change:
void btnReprint_Click(object sender, EventArgs e)
{
}
To
protected void btnReprint_Click(object sender, EventArgs e)
{
}
I need to dynamically add CheckBoxList on the SelectedIndexChanged event of DropDownList. I have achieved this but I cannot retain its value on postback.
Here’s what I have done so far:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
loadTracks();//Needs to generated dynamically
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadDegrees();
}
loadTracks();
}
public void loadTracks()
{
try
{
ConfigurationDB objConfig = new ConfigurationDB();
DataSet ds = objConfig.GetTracksByDegreeID(
Convert.ToInt32(ddlDegree.SelectedValue.ToString()));
CheckBoxList CbxList = new CheckBoxList();
CbxList.ID = "Cbx";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CbxList.Items.Add(new ListItem(ds.Tables[0].Rows[i]["Track_Name"]
.ToString(), ds.Tables[0].Rows[i]["ID"].ToString()));
}
ph.Controls.Add(CbxList);
ViewState["tracks"] = true;
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
//For testing, I added a button and on its click I have added this code
protected void btnDetails_Click(object sender, EventArgs e)
{
CheckBoxList Cbx = (CheckBoxList)ph.FindControl("chk");
foreach (ListItem ex in Cbx.Items)
{
if (ex.Selected)
{
Response.Write(String.Format("You selected: <i>{0}</i> <br>", ex.Value));
}
}
}
Might be a typo:
CbxList.ID = "Cbx";
v.s.
CheckBoxList Cbx = (CheckBoxList)ph.FindControl("chk");
You can try it without changing the code and use pre PreRender
just run you loadTracks()