Hi...
Can some one please please explain this to me cause i am finding it really weird and i have been on it for ages...
i am creating a dynamic gridview at runtime. i have a dropdownlist in my gridview when i select an item from the dropdownlist the selectedindexchanged is not firing for each item in the dropdownlist.
since i am creating a dynamic gridview everytime the page is loaded or postback the dynamic gridview is being created. so the dropdownlist is being populated everytime but for some weird reason as i said above the selectedindexchange of the dropdownlist is not firing all the time.
i have set the autopostback and enableviewstate for all controls and pages to be true.. but still it is not solving the problem..
what i have noticed is that in order for the selectedindexchange to work on an item that didnt fire the selectedindexchange , i would run the app, select the item obviously the selectedindexchanged event will not fire then i close the app, then run it again, then when i select t item from the dropdownlist then it fires the selectedindexchanged event...
weird yeah.........
thanks in advance for anyhelp...
my code is below
Code:
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
container.EnableViewState = true;
switch (_templateType)
{
case ListItemType.Header:
//Creates a new label control and add it to the container.
Label lbl = new Label(); //Allocates the new label object.
lbl.Text = _columnName; //Assigns the name of the column in the lable.
container.Controls.Add(lbl);//Adds the newly created label control to the container.
break;
case ListItemType.Item:
//Creates a new text box control and add it to the container.
if (_columnName == "ID")
{
Label lblID = new Label();//Allocates the new text box object.
lblID.DataBinding += new EventHandler(lblID_DataBinding);//Attaches the data binding event
lblID.Width = ((Unit)200);
lblID.Visible = true;
container.Controls.Add(lblID);
break;
}
else if (_columnName == "Guid")
{
Label lblGUID = new Label();
lblGUID.DataBinding += new EventHandler(lblGUID_DataBinding);
lblGUID.Width = ((Unit)200);
lblGUID.Visible = true;
container.Controls.Add(lblGUID);
}
else if (_columnName == "Fields")
{
DropDownList ddlFields = new DropDownList();
ddlFields.DataBinding += new EventHandler(ddlFields_DataBinding);
ddlFields.Width = ((Unit)200);
container.Controls.Add(ddlFields);
}
else if (_columnName == "OperatorType")
{
DropDownList ddlOperator = new DropDownList();
ddlOperator.DataBinding += new EventHandler(ddlOperator_DataBinding);
ddlOperator.Width = ((Unit)200);
container.Controls.Add(ddlOperator);
}
else if (_columnName == "EnterValue")
{
TextBox txtEnterValue = new TextBox();
txtEnterValue.DataBinding += new EventHandler(txtEnterValue_DataBinding);
txtEnterValue.Width = ((Unit)100);
container.Controls.Add(txtEnterValue);
}
else if (_columnName == "Add")
{
Button btnAdd = new Button();
btnAdd.Text = "Add";
btnAdd.DataBinding += new EventHandler(btnAdd_DataBinding);
btnAdd.Width = 50; ;
container.Controls.Add(btnAdd);
}
else if (_columnName == "Remove")
{
Button btnRemove = new Button();
btnRemove.Text = "Remove";
btnRemove.DataBinding += new EventHandler(btnRemove_DataBinding);
btnRemove.Width = 50;
container.Controls.Add(btnRemove);
}
break;
}
}
void txtEnterValue_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
TextBox txtEnterValue = (TextBox)sender;
txtEnterValue.ID = "txtEnterValue";
GridViewRow container = (GridViewRow)txtEnterValue.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != null)
{
txtEnterValue.Text = dataValue.ToString();
}
}
void ddlOperator_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
DropDownList ddlOperator = (DropDownList)sender;
ddlOperator.ID = "ddlOperator";
//ddlOperator.EnableViewState = true;
//ddlOperator.AutoPostBack = true;
GridViewRow container = (GridViewRow)ddlOperator.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != null)
{
ddlOperator.DataSource = dataValue;
ddlOperator.DataValueField = "ID";
ddlOperator.DataTextField = "Description";
}
}
void ddlFields_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
DropDownList ddlFields = (DropDownList)sender;
ddlFields.ID = "ddlFields";
ddlFields.SelectedIndexChanged += new EventHandler(ddlFields_SelectedIndexChanged);
ddlFields.EnableViewState = true;
ddlFields.AutoPostBack = true;
GridViewRow container = (GridViewRow)ddlFields.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != null)
{
ddlFields.DataSource = dataValue;
ddlFields.DataValueField = "SuperType";
ddlFields.DataTextField = "FieldName";
}
}
void ddlFields_SelectedIndexChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
void lblGUID_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
Label lblGUID = (Label)sender;
lblGUID.ID = "GUID";
lblGUID.Visible = true;
GridViewRow container = (GridViewRow)lblGUID.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
lblGUID.Text = dataValue.ToString();
}
}
void lblID_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
Label lblID = (Label)sender;
lblID.ID = "ID";
lblID.Visible = true;
GridViewRow container = (GridViewRow)lblID.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
lblID.Text = dataValue.ToString();
}
}
void btnAdd_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
Button btnAdd = (Button)sender;
GridViewRow container = (GridViewRow)btnAdd.NamingContainer;
btnAdd.ID = "btnAdd";
btnAdd.CommandName = "Add";
btnAdd.CommandArgument = container.DataItemIndex.ToString();
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
btnAdd.Text = dataValue.ToString();
}
}
void btnRemove_DataBinding(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
Button btnRemove = (Button)sender;
GridViewRow container = (GridViewRow)btnRemove.NamingContainer;
btnRemove.ID = "btnRemove";
btnRemove.CommandName = "Remove";
btnRemove.CommandArgument = container.DataItemIndex.ToString();
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != null)
{
btnRemove.Text = dataValue.ToString();
}
if (container.DataItemIndex > 0)
{
btnRemove.Enabled = true;
}
else
{
btnRemove.Enabled = false;
}
}