Hi,
i have a similar problem to Acchetri.I dont know if I will be able to explain this clearly. Anywayz here I go. I am having 3 dropdown lists inside a datagrid , am using VS.NET 2005. I have defined 2 SQL datasources for the first 2 drop down lists and they are populated on page load. The 3rd drop down list is also associated with a SQl data source where the query expects an input parameter which is nothing but the 2nd drop down list value.
I have this set up by calling the method to pass the SQL Data source Select parameter on the 2nd dropdown list's selectedindexchanged method. But when the 2nd drop down list changes, all the rows of the 3rd drop down changes to the same values. ie., the select parameter that I am passing to populate the 3rd dropdown applies across all the rows. So if I change a value on the 2nd row drop down , then both the first row and second row changes
datagrid definition
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="5" AllowPaging="True" OnItemDataBound="DataGrid1_ItemDataBound" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnItemCommand="DataGrid1_ItemCommand">
<Columns>
<asp:TemplateColumn HeaderText="Sales Org">
<ItemTemplate>
<asp:dropdownlist id="ddlSalesOrg" runat="server" DataSourceID="SqlDataSourceSalesOrg" DataTextField="salesorg_desc" DataValueField="salesorg_code"></asp:dropdownlist>
<asp:RequiredFieldValidator ID="vldSalesOrg" runat="server" ControlToValidate="ddlSalesOrg"
ErrorMessage="Sales Org cannot be blank">!</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="District">
<ItemTemplate>
<asp:dropdownlist id="ddlDistrict" runat="server" DataSourceID="SqlDataSourceDistrict" DataTextField="district_name" DataValueField="district_code" AutoPostBack="True" OnSelectedIndexChanged="ddlDistrict_SelectedIndexC hanged"></asp:dropdownlist>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sales Rep">
<ItemTemplate>
<asp:dropdownlist id="ddlSalesRep" runat="server" DataSourceID="SqlDataSourceSalesRep" DataTextField="SalesRep_Name" DataValueField="SalesRep_Code"></asp:dropdownlist>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right"></FooterStyle>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton id="DeleteRow" runat="server">X</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle NextPageText="Next" PrevPageText="Previous" HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Code Behind
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
dsRole.RoleRecordRow dataRow;
dataRow = (dsRole.RoleRecordRow)(((DataRowView)e.Item.DataIt em).Row);
DropDownList SalesOrg = (DropDownList)(e.Item.FindControl("ddlSalesOrg"));
dataRow.SalesOrg = SalesOrg.SelectedItem.Value.Trim();
DropDownList ddlDistrict = (DropDownList)e.Item.FindControl("ddlDistrict");
dataRow.District = ddlDistrict.SelectedItem.Value.Trim();
DropDownList ddlSalesRep = (DropDownList)e.Item.FindControl("ddlSalesRep");
// check if the dropdown is empty, else show the value;
if (ddlSalesRep != null && ddlSalesRep.Items.Count != 0 && ddlSalesRep.SelectedItem != null)
{
dataRow.SalesRep = ddlSalesRep.SelectedItem.Value.Trim();
}
}
protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
{
string District_code = "";
foreach (DataGridItem gridRow in DataGrid1.Items)
{
DropDownList ddlDistrict = (DropDownList)(gridRow.FindControl("ddlDistrict")) ;
DropDownList ddlSalesRep = (DropDownList)(gridRow.FindControl("ddlSalesRep")) ;
District_code = ((DropDownList)gridRow.Cells[0].Controls[1]).SelectedItem.Value;
SqlDataSourceSalesRep.SelectParameters.Clear();
SqlDataSourceSalesRep.SelectParameters.Add("distri ct_code", District_code);
}
}
So on the first row,if the district ddl changes to say "a" then the salesrep ddl should show only "a" district items.
On the second row, if district ddl is set to "b" then the salesrep ddl should show only "b" district items. but the way I do, when I change the second row to "b", then both first and second row shows only the "b" items for the sales rep dropdown list.
I understand that the data_itembound should be used along with the dropdown even changed to achieve what I want, but I dont know how I would do that. Tried the hidden field but again the same issue.
Can anyone please help me? I am stuck and running out of options.
|