Checkbox state in itemupdating event of listview - asp.net

I have a listview displaying our current projects.
In the itemediting event handler of the listview, I have a number of checkboxes that are being rendered using nested repeaters. After rendering, I loop through all checkboxes and set the correct state based on data retrieved from the DB.
The idea is that I can check or uncheck any of the checkboxes, and the changes are saved in the db.
My problem lies with the itemupdating event handler: I am unable to retain the changed checkbox states. I rebind the nested repeaters, but this seems to overwrite the checkbox states that were set during editing.
Any pointers on how to retain checkbox states generated by a repeater in the edititemtemplate of a listview would be greatly appreciated!
Thanks
Stijn

First I bind the rptDepts repeater at itemediting
Public Sub lvProjects_OnItemEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewEditEventArgs)
Dim rptDepts As Repeater = lvProjects.EditItem.FindControl("rptDepts")
rptDepts.DataSource = bllDept.getServices()
rptDepts.DataBind()
'get tasks for projectID
Dim hdnprojectID As HiddenField = lvProjects.EditItem.FindControl("hdnStudyID")
getTasks(hdnProjectID.Value, rptDepts)
End Sub
Then when rptDepts is databound, I bind the rptTasks repeater
Protected Sub lvDepts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
'get tasks for service
Dim rptTasks As Repeater = e.Item.FindControl("rptTasks")
rptTasks.DataSource = bllDept.getTasksForService(e.Item.DataItem("pk_dept_id"))
rptTasks.DataBind()
End Sub
Then, at itemupdating, I rebind rptDepts (which you said I shouldn't do
Public Sub lvProjects_OnItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs)
'this item
Dim itmProject As ListViewItem = lvProjects.Items(e.ItemIndex)
'rebind depts
'Dim rptDepts As Repeater = itmProject.FindControl("rptDepts")
'rptDepts.DataSource = bllDept.getServices()
'rptDepts.DataBind()
'update project
bllProject.updateProject(itmProject, lblTest)
'unset edit status
lvProjects.EditIndex = -1
'success message
pnlFeedback.CssClass = "success"
ltlFeedback.Text = "Project <b>" & txtName.Text & "</b> was successfully updated."
'rebind
bindProjects()
End Sub
But in the bllProject.updateProject method, I need to be able to reference the checkboxes to save the changes to the DB

If you rebind the nested repeaters, they will be updated from the original datasource (overwriting your changes). Try not rebinding.

Checked='<%# Eval("PreAcqClaim") ==DBNull.Value?false:true %>' MARKUP
------------------CODE BEHIND--------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Claim : System.Web.UI.Page
{
CheckBox OurFaultCheckBox = new CheckBox();
CheckBox PicturesCheckBox = new CheckBox();
CheckBox ReportedInsCheckBox = new CheckBox();
CheckBox ReportLateCheckBox = new CheckBox();
CheckBox AssistRepairCheckBox = new CheckBox();
CheckBox LitigationCheckBox = new CheckBox();
CheckBox PreAcqClaimCheckBox = new CheckBox();
DetailsDataTableAdapters.tblClaimsTableAdapter _adapter = new DetailsDataTableAdapters.tblClaimsTableAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvDetails.DataSource = _adapter.GetDataByPK_Claim_ID(Convert.ToInt32(Request.QueryString["PK_Claim_ID"]));
lvDetails.DataBind();
}
}
protected void objDetails_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
}
protected void lvDetails_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//CheckBox OurFaultCheckBox = (CheckBox)lvDetails.FindControl("OurFaultCheckBox");
//OurFaultCheckBox.Checked = true;
//OurFaultCheckBox = (CheckBox)e.Item.FindControl("OurFaultCheckBox");
//PicturesCheckBox = (CheckBox)e.Item.FindControl("PicturesCheckBox");
//ReportedInsCheckBox = (CheckBox)e.Item.FindControl("ReportedInsCheckBox");
//ReportLateCheckBox = (CheckBox)e.Item.FindControl("ReportLateCheckBox");
//AssistRepairCheckBox = (CheckBox)e.Item.FindControl("AssistRepairCheckBox");
//LitigationCheckBox = (CheckBox)e.Item.FindControl("LitigationCheckBox");
//PreAcqClaimCheckBox = (CheckBox)e.Item.FindControl("PreAcqClaimCheckBox");
}
protected void objDetails_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
CheckBox OurFaultCheckBox = (CheckBox)lvDetails.FindControl("OurFaultCheckBox");
e.InputParameters.Add("OurFaultCheckBox", OurFaultCheckBox.Checked);
}
protected void objDetails_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
e.InputParameters.Add("OurFault", OurFaultCheckBox.Checked);
e.InputParameters.Add("Pictures", PicturesCheckBox.Checked);
e.InputParameters.Add("ReportedIns", ReportedInsCheckBox.Checked);
e.InputParameters.Add("ReportLate", ReportLateCheckBox.Checked);
e.InputParameters.Add("AssistRepair", AssistRepairCheckBox.Checked);
e.InputParameters.Add("Litigation", LitigationCheckBox.Checked);
e.InputParameters.Add("PreAcqClaim", PreAcqClaimCheckBox.Checked);
}
protected void lvDetails_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
DetailsDataTableAdapters.tblClaimsTableAdapter _adapter = new DetailsDataTableAdapters.tblClaimsTableAdapter();
OurFaultCheckBox = (CheckBox)lvDetails.EditItem.FindControl("OurFaultCheckBox");
PicturesCheckBox = (CheckBox)lvDetails.EditItem.FindControl("PicturesCheckBox");
ReportedInsCheckBox = (CheckBox)lvDetails.EditItem.FindControl("ReportedInsCheckBox");
ReportLateCheckBox = (CheckBox)lvDetails.EditItem.FindControl("ReportLateCheckBox");
AssistRepairCheckBox = (CheckBox)lvDetails.EditItem.FindControl("AssistRepairCheckBox");
LitigationCheckBox = (CheckBox)lvDetails.EditItem.FindControl("LitigationCheckBox");
PreAcqClaimCheckBox = (CheckBox)lvDetails.EditItem.FindControl("PreAcqClaimCheckBox");
try
{
_adapter.Update("eventNum", "jobNum","test", "1", DateTime.Now, "", "", "",
"", "", "", DateTime.Now, "", "", "", "54143", "", "", "",
OurFaultCheckBox.Checked, PicturesCheckBox.Checked,
ReportedInsCheckBox.Checked, ReportLateCheckBox.Checked,
AssistRepairCheckBox.Checked, LitigationCheckBox.Checked,
PreAcqClaimCheckBox.Checked,
Convert.ToInt32(Request.QueryString["PK_Claim_ID"]));
}
catch (Exception ex)
{
}
lvDetails.EditIndex = -1;
}
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
}
protected void lvDetails_ItemEditing(object sender, ListViewEditEventArgs e)
{
lvDetails.EditIndex = e.NewEditIndex;
}
}

Related

DevExpress XtraReport HtmlItemCreated event not raised

I'm using DevExpress 2012 Vol 2.4 ASP.NET WebForms controls. I've created a new object of XtraReport class and subscribed to its HtmlItemCreated event. When I'm calling the ExportToHtml method of the XtraReport object the event is not getting raised.
I've not posted the markup but there is just a button. When I click the button the event is not triggered. I've put a breakpoint and tested that.
public partial class WebForm1 : System.Web.UI.Page
{
XtraReport rep = null;
protected void Page_Load(object sender, EventArgs e)
{
rep = new XtraReport();
rep.CreateDocument();
rep.HtmlItemCreated += rep_HtmlItemCreated;
PageHeaderBand h = new PageHeaderBand();
XRLabel l = new XRLabel();
l.Text = "asdasd";
h.Controls.Add(l);
rep.Bands.Add(h);
}
protected void rep_HtmlItemCreated(object sender, HtmlEventArgs e)
{
if (e.ScriptContainer != null)
{
string x = "asdasd";
}
}
protected void btnTest_Click(object sender, EventArgs e)
{
string sPDFFilePath = System.IO.Path.GetTempPath() + "Test.html";
HtmlExportOptions objPDFExpOpt = rep.ExportOptions.Html;
objPDFExpOpt.EmbedImagesInHTML = true;
objPDFExpOpt.ExportMode = HtmlExportMode.DifferentFiles;
if (!string.IsNullOrEmpty("Test"))
objPDFExpOpt.Title = "Test";
rep.ExportToHtml(sPDFFilePath, objPDFExpOpt);
}
}

why my function doesn't execute after clicking dynamically created button

here is my scenarioton : if !page.Ispostback i fill a dropdown with data from database!
Also in the page there is a butron and onclick it gets one id from database and crates one panel IN WHIH THERE IS DYNAMICALLY CREATED BUTTON. The PROBLEM IS WHEN I CLICK THIS DYNAMICALLY CREATED BUTTON _ JUST NOTHING HAPPENS AND I CANT EXPLAIN WHY.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlLanguages.DataSource = this.catRep.GetAllAvailableLanguages();
ddlLanguages.DataBind();
}
//IEnumerable<CatgoriesLanguages> allcategories = this.catRep.GetAllCategoriesByID(1)
}
protected void btnAddNew_Click(object sender, EventArgs e)
{
inseredID = this.catRep.AddCategory();
Label mylab = new Label();
mylab.Text = "Yeeee" + inseredID;
Page.FindControl("form1").Controls.Add(mylab);
Panel myFieldSet = new Panel();
myFieldSet.GroupingText= "Add New Category";
Label lblTitle = new Label();
lblTitle.Text="Title: ";
myFieldSet.Controls.Add(lblTitle);
TextBox txbTitle = new TextBox();
txbTitle.ID = "txbTitle";
myFieldSet.Controls.Add(txbTitle);
myFieldSet.Controls.Add(new LiteralControl("<br />"));
Label lblShrtDescrpt = new Label();
lblShrtDescrpt.Text = "Short Description: ";
myFieldSet.Controls.Add(lblShrtDescrpt);
TextBox txbShrtDescrpt = new TextBox();
txbShrtDescrpt.ID = "txbShrtDescrpt";
myFieldSet.Controls.Add(txbShrtDescrpt);
myFieldSet.Controls.Add(new LiteralControl("<br />"));
Label lblDescrpt = new Label();
lblDescrpt.Text = "Description: ";
myFieldSet.Controls.Add(lblDescrpt);
TextBox txbDescrpt = new TextBox();
txbDescrpt.ID = "txbDescrpt";
myFieldSet.Controls.Add(txbDescrpt);
Button btnAddcategorieslanguage = new Button();
btnAddcategorieslanguage.Click += new EventHandler(btnAddcategorieslanguage_Click);
myFieldSet.Controls.Add(btnAddcategorieslanguage);
Page.FindControl("form1").Controls.Add(myFieldSet);
}
public void btnAddcategorieslanguage_Click(object sender, EventArgs e)
{
TextBox txbTitle = (TextBox)FindControl("txbTitle");
TextBox txbShrtDescrpt = (TextBox)FindControl("txbShrtDescrpt");
TextBox txbDescrpt = (TextBox)FindControl("txbDescrpt");
this.catRep.AddCategoriesLanguages(11, 2, "malee", "tariiiiii", "liliiii");
}
You need to create all dynamically added controls in page Init or page load event too.
Something like this:
protected void Page_Load(object sender, EventArgs e)
{
if(ThereIsDynamicControl())
{
//You can set some session or viewState in the btnAddNew_Click to determine whether you need to add dynamic controls again here or not.
}
if (!IsPostBack)
{
ddlLanguages.DataSource = this.catRep.GetAllAvailableLanguages();
ddlLanguages.DataBind();
}
//IEnumerable<CatgoriesLanguages> allcategories = this.catRep.GetAllCategoriesByID(1);
}

findcontrol does not find dynamically created control in rowUpdating eventhandler

I implemten ITemplate to dynamically create a Template field
TemplateField isReqField = new TemplateField();
isReqField.HeaderText = "Lizenz anfordern";
isReqField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, DataControlRowState.Normal, "isRequested", "bool");
isReqField.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, DataControlRowState.Edit, "isRequested", "bool");
gvLicence.Columns.Add(isReqField);
I implement InstantiateIn
public void InstantiateIn(System.Web.UI.Control container)
{
...
CheckBox ckRequest = new CheckBox();
ckRequest.ID = "ckRequest";
ckRequest.DataBinding += new EventHandler(this.CkIsRequested_DataBinding);
container.Controls.Add(ckRequest);
...
}
with the DataBinding Handler
private void CkIsRequested_DataBinding(Object sender, EventArgs e)
{
CheckBox ckRequest = (CheckBox)sender;
GridViewRow row = (GridViewRow)ckRequest.NamingContainer;
ckRequest.Checked = (bool)DataBinder.Eval(row.DataItem, columnName);
}
But then in the RowUpdating Handler I cannot find my checkBox Control with the FindControl Method:
protected void gvLicence_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
CheckBox chb = (CheckBox)gvLicence.Rows[e.RowIndex].FindControl("ckRequest");
bool requestValue = chb.Checked;
It throws an Exeption because gvLicence.Rows[e.RowIndex].FindControl("ckRequest") is null.
Many thanks for your attention and help.

How to select Listbox value in asp.net

I have a ListBox in my webpage that is bound from database in this way:
ListBox1.DataTextField = "Text";
ListBox1.DataValueField = "MenuID";
ListBox1.DataSource = SqlHelper.ExecuteReader(DAL.DALBase.ConnectionString, "GetMenu");
ListBox1.DataBind();
I want to get selected item value and used this code but have a error and does not worked.
ListBox1.SelectedValue;
Forgive me if I have problems on how to write because my English is not good.
Can you be more specific on the error you are getting?
Using ListBox1.SelectedValue should work.
For example:
int mySelectedValue = int.Parse(ListBox1.SelectedValue);
or
string mySelectedValue = ListBox1.SelectedValue;
Edit
Added code to ensure the original poster was keeping values in ListBox databound.
protected void Page_Load( object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindListBox();
}
}
private BindListBox()
{
ListBox1.DataTextField = "Text";
ListBox1.DataValueField = "MenuID";
ListBox1.DataSource = SqlHelper.ExecuteReader(DAL.DALBase.ConnectionString, "GetMenu");
ListBox1.DataBind();
}
protected void SomeButton_Click( object sender, EventArgs e)
{
string mySelectedValue = ListBox1.SelectedValue;
}

itemdatabound event of a nested listview

I have a nested listview which I databind on the parent 'ItemDataBound' event, but how do i access/register the nested listview's itemdatabound event?
Thanks!
Edits
My parent listview itemdatabound now looks like so,
Protected Sub lvwManagePolicy_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvwManagePolicy.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim rv As DataRowView = CType(e.Item.DataItem, DataRowView)
Me.dsAccoutnTransactionHistory = Wrap.getWrapAccountTransactionHistory(rv!PLATFORM_ID, False)
Dim lvwTransactionHistory As ListView = DirectCast(e.Item.FindControl("lvwTransactionHistory"), ListView)
lvwTransactionHistory.ItemDataBound += New EventHandler(Of ListViewItemEventArgs)(lvwTransactionHistory_ItemDataBound)
lvwTransactionHistory.DataSource = dsAccoutnTransactionHistory
lvwTransactionHistory.DataBind()
End If
End Sub
but i get an error
BC32022: 'Public Event ItemDataBound(sender As Object, e As
System.Web.UI.WebControls.ListViewItemEventArgs)' is an event, and
cannot be called directly. Use a 'RaiseEvent' statement to raise an
event.
Before assigning the data to nested control in your parent control you can register the event like below under your parent ItemBoundData
ListView f = new ListView();
f.ItemDataBound += new EventHandler<ListViewItemEventArgs>(f_ItemDataBound);
protected void f_ItemDataBound(object sender, ListViewItemEventArgs e)
{
}
You can this :
<asp:ListView onitemcommand="inner_ItemCommand" ...
protected / public item command method need :
public void inner_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandArgument == "delete")
{
//do delete here
}
}

Resources