protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//int test = Convert.ToInt32(ListBox1.SelectedValue.ToString());
//GetById(test);
foreach (ListItem listitem in ListBox1.Items)
{
if (listitem.Selected == true)
{
string email = listitem.Text;
GetByEmail(email);
}
}
}
This method should get selected item.I don't why but it show me that property selected is false for all items.
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();
SelectAllUsers();
}
public void SelectAllUsers()
{
SchoolService.SchoolServiceClient client = new SchoolService.SchoolServiceClient();
SchoolService.User[] userArray = client.SelectAll(0);
int i = 0;
while (i < userArray.Length)
{
ListItem listItem = new ListItem();
listItem.Text = userArray[i].Email;
listItem.Value = userArray[i].Id.ToString();
ListBox1.Items.Add(listItem);
i++;
}
}
public void GetByEmail(string email)
{
SchoolService.SchoolServiceClient client = new SchoolService.SchoolServiceClient();
SchoolClient.SchoolService.User user = client.GetUserByEmail(email);
if ((user.FirstName != null) || (user.LastName != null) || (user.Address != null) ||
(user.City != null) || (user.Email != null) || (user.Password != null))
{
txtId.Text = user.Id.ToString();
txtFirstName.Text = user.FirstName.ToString();
txtLastName.Text = user.LastName.ToString();
txtAddress.Text = user.Address.ToString();
txtCity.Text = user.City.ToString();
txtDateOfBirth.Text = user.DateOfBirth.ToShortDateString();
txtEmail.Text = user.Email.ToString();
txtPassword.Text = user.Password.ToString();
txtIsAdmin.Text = user.isAdmin.ToString();
}
else
{
MsgBox("None user was found", this.Page, this);
}
}
public void SelectAllUsers()
{
SchoolService.SchoolServiceClient client = new SchoolService.SchoolServiceClient();
SchoolService.User[] userArray = client.SelectAll(0);
int i = 0;
while (i < userArray.Length)
{
//if (userArray[i] != null)
//{
ListItem listItem = new ListItem();
listItem.Text = userArray[i].Email;
listItem.Value = userArray[i].Id.ToString();
ListBox1.Items.Add(listItem);
//}
i++;
}
}
Also sent methods which fill listBox with items (SelectAllUsers), and method which should take selected item from database (GetByEmail)
It doesn't find anything selected because on Page_Load you are rebinding the data again every time. You need to check if IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ListBox1.Items.Clear();
SelectAllUsers();
}
}
Related
Hey I was trying to create a method like...
private void btnSubmit_Click(object sender, EventArgs e)
{
FillIn();
}
private void FillIn()
{
if (txtName.Text == "")
{
txtName.Text = "Bob Frank";
}
if (txtAddress.Text == "")
{
txtAddress.Text = "4111 N Pensyvania Ave.";
}
if (txtCity.Text == "")
{
txtCity.Text = "Longbeach";
}
if (txtState.Text == "")
{
txtState.Text = "CA";
}
if(txtZip.Text == "")
{
txtZip = "90210";
}
}
this code works great but when I try to add parameters to it like this..
private void btnSubmit_Click(object sender, EventArgs e)
{
FillIn(txtName.Text, txtStreetAddress.Text, txtCity.Text, txtState.Text, txtZip.Text);
}
private void FillIn(string name, string address, string city, string state, string zip)
{
if (name == "")
{
name = "Bob Frank";
}
if (address == "")
{
address = "4111 N Pensyvania Ave.";
}
if (city == "")
{
city = "Longbeach";
}
if (state == "")
{
state = "CA";
}
if(zip == "")
{
zip = "99210";
}
}
it stops working and the text boxes won't fill back in and won't error out, how can I fix this?
You need to pass the actual controls. If you try and pass txtName.Text it just reads the value in the property and you can't update it.
private void btnSubmit_Click(object sender, EventArgs e)
{
FillIn(txtName, txtStreetAddress, txtCity, txtState, txtZip);
}
private void FillIn(TextBox name, TextBox address, TextBox city, TextBox state, TextBox zip)
{
if (name.Text == "")
{
name.Text = "Bob Frank";
}
if (address.Text == "")
{
address.Text = "4111 N Pensyvania Ave.";
}
if (city.Text == "")
{
city.Text = "Longbeach";
}
if (state.Text == "")
{
state.Text = "CA";
}
if(zip.Text == "")
{
zip.Text = "99210";
}
}
I have a Checkboxlist in which Items are binded from database.
Now what I want is, whenever user checks Registration / Conveyance Deed value from the list, Then the gridview should get disabled.
I tried with below code.
protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlStatus.SelectedValue == "30" && strMode == "M")
{
GridExpInfo.AllowAddingRecords = false;
}
else
{
GridExpInfo.AllowAddingRecords = true;
}
}
What happens is, it always shows the selected value of Agreement from the list, which is 20
Below is the screenshot of the list
update
ASPX:-
<asp:CheckBoxList ID="ddlStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged">
</asp:CheckBoxList>
C#
private void BindStatus()
{
DataTable dtstatus = new DataTable();
OracleDataAdapter dastatus = new OracleDataAdapter("SELECT lookup_code agr_type_code, meaning agr_type " +
"FROM apps.fnd_lookup_values_vl " +
"WHERE (NVL ('', territory_code) = territory_code OR territory_code IS NULL " +
") AND lookup_type = 'XXACL_FARM_AGR_TYPE' " +
"AND (lookup_type LIKE 'XXACL_FARM_AGR_TYPE') " +
"AND (view_application_id = 0) " +
"AND (security_group_id = 0) " +
"ORDER BY 1", ObjPriCon);
dastatus.Fill(dtstatus);
ddlStatus.DataTextField = "agr_type";
ddlStatus.DataValueField = "agr_type_code";
ddlStatus.DataSource = dtstatus;
ddlStatus.DataBind();
}
You can set property AllowUserToAddRows to false inside code as shown below.
this.yourGrid.AllowUserToAddRows = false;
this.BindDataGridView();
And use ForEach loop for checking your condition. Now you check all selected items not only first selected.
protected void ddlStatus_OnSelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem li in ddlStatus.Items)
{
if (li.Value == "30" && strMode == "M")
{
GridExpInfo.AllowUserToAddRows = false;
}
else
{
GridExpInfo.AllowUserToAddRows = true;
}
}
You can also use LINQ for checking it.
protected void ddlStatus_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (strMode == "M")
{
var disable = (from ListItem item in ddlStatus.Items.OfType<ListItem>()
where item.Selected
where item.Value == "30"
select int.Parse(item.Value)).Any();
if (disable)
{
GridExpInfo.AllowUserToAddRows = false;
}
else
{
GridExpInfo.AllowUserToAddRows = true;
}
}
}
Try this snippet
protected void myCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
{
bool enableGrid = true;
foreach (ListItem listItem in ddlStatus.Items)
{
if (listItem.Value == "30" && listItem.Selected == true)
{
enableGrid = false;
}
}
if (enableGrid == true)
{
//enable grid and/or row inserts here
Label1.Text = "enabled";
}
else
{
//disable grid and/or row inserts here
Label1.Text = "disabled";
}
}
I have created a paginated grid view which contains check boxes for multiple selection.But while looping through the data-row in the grid-view only last pages row values only I am able to get.I know it's because of the pagination.
My code is below
protected void btnSaveItemMapping_Click(object sender, EventArgs e)
{
grdCustomers.AllowPaging = false;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
if (myCheckBox.Checked == true)
{
Label lblCustomerCode = (Label)gvrow.FindControl("lblCustomerCode");
//INSERT TO DATABSE
}
}
grdCustomers.AllowPaging = true
}
Keeping check box checked after changing page index like below code.
protected void grdCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
savechkdvls_cst();
grdCustomers.PageIndex = e.NewPageIndex;
if (Session["AllCustomers"] != null)
{
lstAllCustomers = (List<CustomerMaster>)Session["AllCustomers"];
}
grdCustomers.DataSource = lstAllCustomers;
grdCustomers.DataBind();
chkdvaluesp_cst();
}
private void chkdvaluesp_cst()
{
ArrayList usercontent = (ArrayList)Session["chkditems_customers"];
if (usercontent != null && usercontent.Count > 0)
{
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
int index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
if (usercontent.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
myCheckBox.Checked = true;
}
}
}
}
private void savechkdvls_cst()
{
ArrayList usercontent = new ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
bool result = ((CheckBox)gvrow.FindControl("chkBoxBrandCustomers")).Checked;
// Check in the Session
if (Session["chkditems_customers"] != null)
usercontent = (ArrayList)Session["chkditems_customers"];
if (result)
{
if (!usercontent.Contains(index))
usercontent.Add(index);
}
else
usercontent.Remove(index);
}
if (usercontent != null && usercontent.Count > 0)
Session["chkditems_customers"] = usercontent;
}
Any help will be appreciated.
I am selecting a row from Gridview, working good except for checkbox, like I am assigning value of checkbox retreived from gridview to checkbox that is placed on web form but it isn't represented by checkbox on form, it shows empty checkbox in every case
if (gridviewDesignations.SelectedRow.Cells[5].Text == " ")
{
chkIsHead.Text = gridviewDesignations.SelectedRow.Cells[5].Text;
}
in short, checkbox is not picking value from gridview
Update:
tried this too:
CheckBox chkIsHead = (CheckBox) gridviewDesignations.SelectedRow.Cells[5].Controls[0];
if (chkIsHead.Checked == false)
{
chkIsHead.Checked = false;
}
else
{
chkIsHead.Checked = true;
}
Update:
my full code:
public partial class frmDesignations : System.Web.UI.Page
{
AccessibleVariables accessVariables = new AccessibleVariables(); //Used to access global variables
public void Clear(params TextBox[] txtBoxes)
{
foreach (TextBox txtbx in txtBoxes)
{
txtbx.Text = "";
}
}
public void fillddlDepartments()
{
ManageDepartmentsBizz mngDepBizz = new ManageDepartmentsBizz();
DataSet ds = (DataSet)mngDepBizz.SelectDepartments();
if (ds.Tables[0].Rows.Count != 0)
{
ddlDepartments.DataValueField = "DepID";
ddlDepartments.DataTextField = "DepName";
ddlDepartments.DataSource = ds.Tables[0];
// ddlDepartments.SelectedIndex = -1;
ddlDepartments.DataBind();
ddlDepartments.Items.Insert(0, "--Select--");
}
//else
// ddlDepartments.SelectedIndex = -1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count <= 0)
{
Response.Redirect("login.aspx");
}
lblMsgPopUp.Visible = false;
if (!IsPostBack)
{
fillddlDepartments();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
bool IsHead = Convert.ToBoolean(chkIsHead.Checked);
DesignationsBizz DesigBizz = new DesignationsBizz(-1, txtTitle.Text, DepartmentID, txtContactNo.Text, IsHead);
//-1 is bogus,used to fill parameters criteria i.e no of params
ManageDesignationsBizz mngDesigBizz = new ManageDesignationsBizz();
bool Result = mngDesigBizz.Insert(DesigBizz);
if (Result == true)
{
HiddenFieldSetMessage.Value = "Saved";
HiddenFieldShowMessage.Value = "True";
Clear(txtTitle, txtSelectedID, txtContactNo);
}
else
{
HiddenFieldSetMessage.Value = "RecordAlreadyExists";
HiddenFieldShowMessage.Value = "True";
}
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotSaved";
HiddenFieldShowMessage.Value = "True";
}
}
protected void btnSearchPopup_Click(object sender, EventArgs e)
{
string DesignationTitle = txtDesignationPopUp.Text;
ManageDesignationsBizz mngDepsBizz = new ManageDesignationsBizz();
DataSet ds = (DataSet)mngDepsBizz.Select(DesignationTitle);
if (ds.Tables[0].Rows.Count != 0)
{
lblMsgPopUp.Visible = false;
gridviewDesignations.DataSource = ds.Tables[0];
gridviewDesignations.DataBind();
gridviewDesignations.Visible = true;
}
else
{
lblMsgPopUp.Visible = true;
gridviewDesignations.Visible = false;
}
}
protected void gridviewDesignations_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string DesignationTitle = txtDesignationPopUp.Text;
ManageDesignationsBizz mngDepBizz = new ManageDesignationsBizz();
DataSet ds = (DataSet)mngDepBizz.Select(DesignationTitle);
if (ds.Tables[0].Rows.Count != 0)
{
gridviewDesignations.PageIndex = e.NewPageIndex;
gridviewDesignations.DataSource = ds.Tables[0];
gridviewDesignations.DataBind();
gridviewDesignations.Visible = true;
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
if (gridviewDesignations.SelectedRow != null)
{
if (gridviewDesignations.SelectedRow.Cells[1].Text == " ")
{
txtSelectedID.Text = string.Empty;
}
else
{
txtSelectedID.Text = gridviewDesignations.SelectedRow.Cells[1].Text;
}
if (gridviewDesignations.SelectedRow.Cells[2].Text == " ")
{
txtTitle.Text = string.Empty;
}
else
{
txtTitle.Text = gridviewDesignations.SelectedRow.Cells[2].Text;
}
if (gridviewDesignations.SelectedRow.Cells[3].Text == " ")
{
ddlDepartments.SelectedValue = string.Empty;
}
else
{
ddlDepartments.SelectedValue = gridviewDesignations.SelectedRow.Cells[3].Text;
accessVariables.DepID = Convert.ToInt32(gridviewDesignations.SelectedRow.Cells[3].Text);
ViewState["depID"] = accessVariables.DepID;
}
if (gridviewDesignations.SelectedRow.Cells[4].Text == " ")
{
txtContactNo.Text = string.Empty;
}
else
{
txtContactNo.Text = gridviewDesignations.SelectedRow.Cells[4].Text;
}
CheckBox chkIsHead = (CheckBox) gridviewDesignations.SelectedRow.Cells[5].Controls[0];
if (chkIsHead.Checked == false)
{
chkIsHead.Checked = false;
}
else
{
chkIsHead.Checked = true;
}
gridviewDesignations.DataBind();
gridviewDesignations.SelectedIndex = -1;
HiddenFieldShowHideButtons.Value = "True";
}
}
protected void btnUpdatePopUp_Click(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(txtSelectedID.Text);
int DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
bool IsHead = Convert.ToBoolean(chkIsHead.Checked);
DesignationsBizz DesigBizz = new DesignationsBizz(id, txtTitle.Text, DepartmentID, txtContactNo.Text, IsHead );
ManageDesignationsBizz mngDesigBizz = new ManageDesignationsBizz();
bool Result = mngDesigBizz.Update(DesigBizz);
if (Result == true)
{
HiddenFieldSetMessage.Value = "Updated";
HiddenFieldShowMessage.Value = "True";
Clear(txtSelectedID, txtTitle, txtContactNo);
}
else
{
HiddenFieldSetMessage.Value = "NotUpdated";
HiddenFieldShowMessage.Value = "True";
}
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotUpdated";
HiddenFieldShowMessage.Value = "True";
}
}
protected void btnDeletePopUp_Click(object sender, EventArgs e)
{
try
{
int ID = Convert.ToInt32(txtSelectedID.Text.Trim());
ManageDesignationsBizz mngDepBizz = new ManageDesignationsBizz();
mngDepBizz.Delete(ID);
Clear(txtTitle, txtSelectedID, txtContactNo);
HiddenFieldSetMessage.Value = "Deleted";
HiddenFieldShowMessage.Value = "True";
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotDeleted";
HiddenFieldShowMessage.Value = "True";
}
}
protected void btnClosePopup_Click(object sender, EventArgs e)
{
Clear(txtTitle, txtSelectedID, txtContactNo);
}
protected void ddlDepartments_SelectedIndexChanged(object sender, EventArgs e)
{
if (txtSelectedID.Text != "")
{
accessVariables.DepID = Convert.ToInt32(ddlDepartments.SelectedValue);
ViewState["depID"] = accessVariables.DepID;
}
else
{
accessVariables.DepID = Convert.ToInt32(ddlDepartments.SelectedValue);
ViewState["depID"] = accessVariables.DepID;
}
}
protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
{
if (txtSelectedID.Text != "")
{
int DepID = Convert.ToInt32(ViewState["depID"]);
ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);
if (isHead == true)
{
HiddenFieldSetMessage.Value = "HeadExists";
HiddenFieldShowMessage.Value = "True";
chkIsHead.Checked = false;
}
}
else
{
int DepID = Convert.ToInt32(ViewState["depID"]);
ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);
if (isHead == true)
{
HiddenFieldSetMessage.Value = "HeadExists";
HiddenFieldShowMessage.Value = "True";
chkIsHead.Checked = false;
}
}
}
}
I believe the following is what you need to do:
Then select EditProgrammatically.
I'm not sure if yoou'll need to manually put data into the grid though but that won't be too hard. Examples can be seen here : http://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx
PS : You can set the DataSource in the grid view to the DataTable.
I am creating a web site but anytime I run this code, an exception is raised:
#Exception Details: System.IndexOutOfRangeException: There is no row at position 0.
How do I solve this problem?
public partial class Employee_frmSearchClaims : System.Web.UI.Page
{
int empno;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["empno"] != null)
empno = int.Parse(Session["empno"].ToString());
if (!IsPostBack)
{
DataRow r = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno).Tables[0].Rows[0];
Label7.Text = r["policyname"].ToString();
Label8.Text = r["policyid"].ToString();
Label9.Text = r["policyamount"].ToString();
Label10.Text = r["TotalAmount"].ToString();
Label11.Text = r["pstartdate"].ToString();
}
}
protected void btnapplicy_Click(object sender, EventArgs e)
{
if (ddlReason.SelectedItem.ToString() != "--Select--")
{
if (ddlReason.SelectedItem.Text == "Death")
{
Session["Reason"] = ddlReason.SelectedItem.Text;
Response.Redirect("frmDeathCliam.aspx");
}
else if (ddlReason.SelectedItem.Text == "Accident")
{
Session["Reason"] = ddlReason.SelectedItem.Text;
Response.Redirect("frmAccidentClaim.aspx");
}
else
{
Session["Reason"] = ddlReason.SelectedItem.Text;
Response.Redirect("frmCompleteClaim.aspx");
}
}
else
lblmsg.Text = "You have to select the reason";
}
}
You need to make sure your table contains rows first:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["empno"] != null)
empno = int.Parse(Session["empno"].ToString());
if (!IsPostBack)
{
var ds = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno);
if (ds.Tables[0].Rows.Count > 0)
{
DataRow r = ds.Tables[0].Rows[0];
Label7.Text = r["policyname"].ToString();
Label8.Text = r["policyid"].ToString();
Label9.Text = r["policyamount"].ToString();
Label10.Text = r["TotalAmount"].ToString();
Label11.Text = r["pstartdate"].ToString();
}
}
}