dotnet22
asked on
selectedindex change not working in datagrid
If I dynamically populate my dropdownlist in ItemDataBound of a datagrid, the selectedindex change even does not work.. However, If I populate the dropdown through the properties and not dynamically, it works? Does anyone have any ideas? In both cases it postback but the event is not triggered for the dynamic case...?
Can you post the selectedindex change event?
I see no reason this shouldn't work if you dynamically populate from ItemDataBound, can you post your code?
ASKER
this is the item databound
private void grdOrderSheets_ItemDataBou nd(object sender, System.Web.UI.WebControls. DataGridIt emEventArg s e)
{
if ( (e.Item.ItemType == ListItemType.AlternatingIt em) || (e.Item.ItemType == ListItemType.Item))
{
DropDownList pageNums = (DropDownList)e.Item.FindC ontrol("dd lPages");
pageNums.DataTextField = "documentSheetOrder";
pageNums.DataValueField = "documentInputID";
pageNums.DataSource = pages;
pageNums.DataBind();
}
}
the selectedindex change
protected void grdOrderSheets_SelectedInd exChanged( object sender, System.EventArgs e)
{
// suppose to come int here but it dosn't
}
the control
<asp:dropdownlist id="ddlPages" runat="server" onselectedindexchanged="gr dOrderShee ts_Selecte dIndexChan ged" autopostback="True">
private void grdOrderSheets_ItemDataBou
{
if ( (e.Item.ItemType == ListItemType.AlternatingIt
{
DropDownList pageNums = (DropDownList)e.Item.FindC
pageNums.DataTextField = "documentSheetOrder";
pageNums.DataValueField = "documentInputID";
pageNums.DataSource = pages;
pageNums.DataBind();
}
}
the selectedindex change
protected void grdOrderSheets_SelectedInd
{
// suppose to come int here but it dosn't
}
the control
<asp:dropdownlist id="ddlPages" runat="server" onselectedindexchanged="gr
protected void grdOrderSheets_SelectedInd exChanged( object sender, System.EventArgs e)
{
// suppose to come int here but it dosn't
}
This is the code we need. The logic in there is probably not correct.
{
// suppose to come int here but it dosn't
}
This is the code we need. The logic in there is probably not correct.
ASKER
That's the problem... On a postback grdOrderSheets_SelectedInd exChanged is never triggered???
I believe it is being fired, but due to some logic errors, it doesn't appear to be firing. But if you are so sure that it's not a logic error and it is indeed never firing then I'm sorry for wasting your time.
the only way I can see why it is not firing is if you are rebinding the dropdownlist on a postback (erasing any event handler information). It doesn't appear you are doing this, and I don't know why you would. Can you paste full code or the entire page process?
ASKER
I know it's proably stemming from somewhere else...
ASKER
private void Page_Load(object sender, System.EventArgs e)
{
if ( !Page.IsPostBack)
{
if (User.IsInRole(clPermissio ns.AccessR ights.Admi n.ToString ())||(User .IsInRole( clPermissi ons.Access Rights.Lib rary.ToStr ing())))
{
if (Request.QueryString["id"] != null)
{
try
{
int docID = Convert.ToInt32(Request.Qu eryString[ "id"]);
pages = clDocument.GetDocumentInpu tSheets(do cID);
grdOrderSheets.DataSource = clDocument.GetDocumentShee tOrderDisp lay(docID) ;
grdOrderSheets.DataBind();
}
catch(Exception ee)
{
lblMessage.Text = ee.Message;
}
}
}
else
{
Response.Redirect("../logi n.aspx");
}
}//if ( !Page.IsPostBack)
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grdOrderSheets.ItemDa taBound += new System.Web.UI.WebControls. DataGridIt emEventHan dler(this. grdOrderSh eets_ItemD ataBound);
this.grdOrderSheets.Select edIndexCha nged += new System.EventHandler(this.g rdOrderShe ets_Select edIndexCha nged);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
private void grdOrderSheets_ItemDataBou nd(object sender, System.Web.UI.WebControls. DataGridIt emEventArg s e)
{
if ( (e.Item.ItemType == ListItemType.AlternatingIt em) || (e.Item.ItemType == ListItemType.Item))
{
DropDownList pageNums = (DropDownList)e.Item.FindC ontrol("dd lPages");
pageNums.DataTextField = "documentSheetOrder";
pageNums.DataValueField = "documentInputID";
pageNums.DataSource = pages;
pageNums.DataBind();
}
}
protected void grdOrderSheets_SelectedInd exChanged( object sender, System.EventArgs e)
{
// suppose to come int here but it doewn't
}
{
if ( !Page.IsPostBack)
{
if (User.IsInRole(clPermissio
{
if (Request.QueryString["id"]
{
try
{
int docID = Convert.ToInt32(Request.Qu
pages = clDocument.GetDocumentInpu
grdOrderSheets.DataSource = clDocument.GetDocumentShee
grdOrderSheets.DataBind();
}
catch(Exception ee)
{
lblMessage.Text = ee.Message;
}
}
}
else
{
Response.Redirect("../logi
}
}//if ( !Page.IsPostBack)
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grdOrderSheets.ItemDa
this.grdOrderSheets.Select
this.Load += new System.EventHandler(this.P
}
#endregion
private void grdOrderSheets_ItemDataBou
{
if ( (e.Item.ItemType == ListItemType.AlternatingIt
{
DropDownList pageNums = (DropDownList)e.Item.FindC
pageNums.DataTextField = "documentSheetOrder";
pageNums.DataValueField = "documentInputID";
pageNums.DataSource = pages;
pageNums.DataBind();
}
}
protected void grdOrderSheets_SelectedInd
{
// suppose to come int here but it doewn't
}
Everything looks ok to me, this is very weird.
Try what I do when I can't figure out something like this, create a new page that does absolutely nothing but what your question is about. Get it working, it should work in the most simplest form. Then start tacking on the extra code from your original page and see when it stops working, then you will have your problem.
Try what I do when I can't figure out something like this, create a new page that does absolutely nothing but what your question is about. Get it working, it should work in the most simplest form. Then start tacking on the extra code from your original page and see when it stops working, then you will have your problem.
ASKER
That's exactly what I did and when I comment out the dynamic population in item databound, it works... go figure... Note: I tried this on a brand new page...
If that was your new page, then you are still doing "extra" things that don't have to be done, for testing purposes anyway. Everything should be done bare bones, and I mean it, no role checks in page_load, no setting the dropdownlist DataSource=pages. nothing extra, only what you need!
ASKER
Sorry. I meant I did what you suggested in a test page. The code I have posted is from the original. So there was no role checks etc in the new test page... I've done this plenty of time before... I've never come across this...
ok, this is weird. I'm not a C# programmer, but just for kicks I created a test page to do exactly what you are after. Instead of assigning the eventhandler in the asp control on the aspx page, I did it in code, in ItemDataBound. I was experiencing the exact same problem you were, the eventhandler wasn't fired on a postback. I read through some examples, and someone has suggested to put it in ItemCreated...bam! worked like a champ. This is a very weird behavior to me, so if a C# developer would like to explain it, I'd even appreciate it!
here's what it looked like..
private void dgTest_ItemCreated(object sender, System.Web.UI.WebControls. DataGridIt emEventArg s e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
string[] blah = new string[3];
blah[0]="blahblah";
blah[1]="test";
blah[2]="yo";
DropDownList ddlTest = (DropDownList)e.Item.FindC ontrol("dd lTest");
ddlTest.DataSource = blah;
ddlTest.AutoPostBack = true;
ddlTest.DataBind();
ddlTest.SelectedIndexChang ed += new System.EventHandler(this.d dlTest_Sel ectedIndex Changed);
}
}
private void ddlTest_SelectedIndexChang ed(object sender, System.EventArgs e)
{
// suppose to come int here but it doewn't
Response.Write("fired event");
}
here's what it looked like..
private void dgTest_ItemCreated(object sender, System.Web.UI.WebControls.
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
string[] blah = new string[3];
blah[0]="blahblah";
blah[1]="test";
blah[2]="yo";
DropDownList ddlTest = (DropDownList)e.Item.FindC
ddlTest.DataSource = blah;
ddlTest.AutoPostBack = true;
ddlTest.DataBind();
ddlTest.SelectedIndexChang
}
}
private void ddlTest_SelectedIndexChang
{
// suppose to come int here but it doewn't
Response.Write("fired event");
}
ok .. y do u say the selectedindexchanged is not firing??
have a look at this link exapling everything about adding a dropdown to a datagrid ... uses the same approach as have here
http://odetocode.com/Articles/231.aspx
is it because u r searching for adropdown named ddlPages in itemdatabound
DropDownList pageNums = (DropDownList)e.Item.FindC ontrol("dd lPages");
and u do not have a event handler for the dropdown .. do u have it in ur aspx page .. something like
<ItemTemplate>
<asp:DropDownList
ID="ddlPages" Runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="dd lTest_Sele ctedIndexC hanged" />
</ItemTemplate>
within ur datagrid ....??
have a look at this link exapling everything about adding a dropdown to a datagrid ... uses the same approach as have here
http://odetocode.com/Articles/231.aspx
is it because u r searching for adropdown named ddlPages in itemdatabound
DropDownList pageNums = (DropDownList)e.Item.FindC
and u do not have a event handler for the dropdown .. do u have it in ur aspx page .. something like
<ItemTemplate>
<asp:DropDownList
ID="ddlPages" Runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="dd
</ItemTemplate>
within ur datagrid ....??
also confirm that the autopostback is set to true for the dropdown .. ddlPages
ASKER
The problem was due to the fact that values were all the same in the dropdown... Meaning it looked as follows:
Text Value
1 66
2 66
3 66
Once the Values were different, it solved the problem... Go figure...
Text Value
1 66
2 66
3 66
Once the Values were different, it solved the problem... Go figure...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.