findcontrol does not find dynamically created control in rowUpdating eventhandler - asp.net

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.

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);
}
}

Can't dynamically add controlvalidation to textbox

After several searches on the Internet finally decided to drop a question.
At runtime I want the following to happen.
If you push a button a textbox will be added to the controls and a custom validator must be attached to the textbox and fire.
It does not fire. Why?
Here is my code
Thank you for looking into this.
public partial class WebUserControl1 : System.Web.UI.UserControl
{
CustomValidator rv2 = new CustomValidator();
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
rv2.ID = "rev2";
rv2.ErrorMessage = "Not numeric input";
rv2.ClientValidationFunction = "";
rv2.ServerValidate += new ServerValidateEventHandler(GetalValidator);
Controls.Add(rv2);
}
}
protected void ButtonClick(object sender, EventArgs args)
{
TextBox tb1 = new TextBox();
tb1.ID = "tb2";
tb1.Visible = true;
tb1.Width = 30;
this.Controls.Add(tb1);`enter code here`
rv2.EnableClientScript = false;
rv2.ControlToValidate = "tb2";
}
private bool IsNumber(string someText)
{
int number;
bool result = Int32.TryParse(someText, out number);
return result;
}
protected void GetalValidator(object source, ServerValidateEventArgs args)
{
args.IsValid = IsNumber(args.Value);
}
}
here is part of code thorugh which you can add validation on your dynamically created control.
TextBox tb1 = new TextBox();
tb1.ID = "tb2";
tb1.Visible = true;
tb1.Width = 30;
RequiredFieldValidator regfv = new RequiredFieldValidator();
regfv.ID = "regfv";
regfv.ControlToValidate = tb1.ID;
regfv.ErrorMessage = "My Error";
this.Controls.Add(tb1);
this.Controls.Add(regfv);

linkButton Click Event in Grid View not firing when adding though Bound Fields in a loop in GridView

Hi,Here i am trying to implement the linkButton click event in a
gridview through code behind using the BoundField Class in gridview.
when i trying to add the individual BoundField values Directly to grid
as a column in page_Load and binding the linkbutton in
RowDataBoundEvent of the row of cell with linkbutton click event, it
is firing the linkButton Click event well with the following code.
protected void Page_Load(object sender,EventArgs e)
{
BoundField bfield = new BoundField();
bfield.HeaderText = "EmpName";
bfield.DataField = "EmpName";
gridView.Columns.Add(bfield);
BoundField bfield1 = new BoundField();
bfield1.HeaderText = "Monday";
bfield1.DataField = "Monday";
gridView.Columns.Add(bfield1);
}
and in on RowDataBound Event i have wrote
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkViews = new LinkButton();
lnkViews.ID = "lnkViews";
lnkViews.Text = (e.Row.DataItem as DataRowView).Row["Monday"].ToString();
lnkViews.Click += new EventArgs(linkButton_ClickAction);
e.Row.Cells[2].Controls.Add(lnkViews);
}
}
protected void linkButton_ClickAction(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string name = row.Cells[0].Text; this.ModalPopupExtender1.Show();
}
But when i trying to add the above BoundFields by looping based on the
table columns count like this, in page_Load event, the event is not
firing.
protected void Page_Load(object sender,EventArgs e)
{
DataTable dtTable = new DataTable();
dtTable= BindGrid();
BoundField boundField;
for (int i = 0; i < dtTable.Columns.Count; i++)
{
string ColumnValue = dtTable.Columns[i].ColumnName;
boundField = new BoundField();
boundField.HeaderText = ColumnValue;
boundField.DataField = ColumnValue;
gridView.Columns.Add(boundField);
}
}
when we creating the BoudnField event using the above code in a loop
based on dataSource columns count, it doesn't firing linkbutton event. why?
as per my understanding you are trying to form grid dynamically.
One thing is bound fields will not support for bubbled events. So better replace them with template fields that will meets your requirement.
You need to assign click event in InstantiateIn method by defining ITemplate interface.
OnRowDataBound event doesn't comes into picture.
below is example for your reference.
http://forums.asp.net/t/1001702.aspx

Capture DropDownList Index Change event inside of grid View

I am trying to capture the SelectedIndexChanged event for a drop down list I have put inside of a gridview control. It posts back fine, but does not go into my SelectedIndexChanged event handler. Here is my code
DropDownList myddl;
protected void Page_Load(object sender, EventArgs e)
{
this.myGridview.RowDataBound += new GridViewRowEventHandler(myGridview_RowDataBound);
myddl = new DropDownList();
myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
if (!Page.IsPostBack)
{
List<Team> teams = giveMeTeams();
this.myGridview.DataSource = teams;
this.myGridview.AutoGenerateColumns = false;
BoundField col1 = new BoundField();
col1.DataField = "Name";
this.myGridview.Columns.Add(col1);
BoundField col2 = new BoundField();
col2.DataField = "Sport";
this.myGridview.Columns.Add(col2);
BoundField col3 = new BoundField();
col3.DataField = "Status";
this.myGridview.Columns.Add(col3);
this.myGridview.DataBind();
}
}
void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
myddl = new DropDownList();
myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
List<string> items = new List<string>();
items.Add("good");
items.Add("bad");
myddl.DataSource = items;
myddl.AutoPostBack = true;
myddl.DataBind();
e.Row.Cells[2].Controls.Add(myddl);
}
void myddl_SelectedIndexChanged(object sender, EventArgs e)
{
string temp = "In Here"; //neve hits this code
}
private List<Team> giveMeTeams()
{
Teams teams = new Teams();
teams.Add(new Team("RedWings", "Hockey", "good"));
teams.Add(new Team("Lions", "Football", "bad"));
teams.Add(new Team("Packers", "Football", "good"));
return teams;
}
Any help is greatly appreciated.
Thanks,
Edited based on Comments
I have tried as you suggested...and am still not capturing the post back. here is my new code
void myGridview_RowCreated(object sender, GridViewRowEventArgs e)
{
DropDownList myddl = new DropDownList();
myddl = new DropDownList();
myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
myddl.ID = "MyID" + e.Row.RowIndex.ToString();
e.Row.Cells[2].Controls.Add(myddl);
}
void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList myddl = e.Row.FindControl("MyID" + e.Row.RowIndex.ToString()) as DropDownList;
//myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
List<string> items = new List<string>();
items.Add("good");
items.Add("bad");
myddl.DataSource = items;
myddl.DataMember = "Status";
myddl.AutoPostBack = true;
myddl.DataBind();
e.Row.Cells[2].Controls.Add(myddl);
}
it is still not going into my myddl_SelectedIndexChanged() eventhandler.
Create that Dropdownlist in RowCreated of the Grid and assign an ID to it. Get the refrence to these Dropdowns in RowDataBound via e.Row.FindControl("MyDropdownlistID") and bound them to the Datasource. Create distinct Dropdownlist instances instead of referencing always the same

CheckedChanged event for Dynamically generated Checkbox column in DataGrid(Asp.Net)

I have a datagrid (Asp.Net) with dynamically generated checkbox column..I am not able to generate the checkedChanged event for the checkbox..
Here is my code:
public class ItemTemplate : ITemplate
{
//Instantiates the checkbox
void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.CheckedChanged += new EventHandler(this.OnCheckChanged);
box.AutoPostBack = true;
box.EnableViewState = true;
box.Text = text;
box.ID = id;
container.Controls.Add(box);
}
public event EventHandler CheckedChanged;
private void OnCheckChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}
}
and Here is the event
private void OnCheckChanged(object sender, EventArgs e)
{
}
Thanks In advance
When do you add your custom column? If it is on load, then it is too late. Load it on init. I.e. following works with your code:
protected void Page_Init(object sender, EventArgs e)
{
ItemTemplate myTemplate = new ItemTemplate();
myTemplate.CheckedChanged += new EventHandler(myTemplate_CheckedChanged);
TemplateField col = new TemplateField();
col.ItemTemplate = myTemplate;
col.ItemStyle.Wrap = false;
grid.Columns.Add(col);
}
If your checkbox ID's are not being set the same way on every postback, then they can never be connected to the event handlers when it comes time to process the events. Where is your field "id" coming from?

Resources