I’m building 2 cascading dropdownlists in a gridview edit mode following this example but than in C#:
http://mikepope.com/blog/DisplayBlog.aspx?permalink=1708
But my @AttriFk never gets a value, how can I set this in this event?
<asp:TemplateField HeaderText="Sub Attribute">
<EditItemTemplate>
<asp:DropDownList ID="ddlListSubAttributes" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourceSubAttributes" DataTextField="Title" DataValueField="ID" />
<asp:SqlDataSource ID="SqlDataSourceSubAttributes" runat="server" ConnectionString="Data Source="//empty for this post" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [ID],[Title],[AttriFk] FROM [LocalGTPDatabase].[dbo].[AttributeSubTypes] WHERE ([AttriFk] = @AttriFk)">
<SelectParameters>
<asp:Parameter Name="@AttriFk" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label66" runat="server" Text='<%# Bind("subAttribute") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is a part of my code behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
System.Data.DataRowView dv = (System.Data.DataRowView)e.Row.DataItem;
DropDownList ddlListAttributes = (DropDownList)(e.Row.FindControl("ddlListAttributes"));
String nest = Convert.ToString(dv["AttributeFk"]);
ddlListAttributes.SelectedValue = nest;
//Databind list of taskCode in dependent drop-down list, preselect value
DropDownList ddlListSubAttributes = (DropDownList)(e.Row.FindControl("ddlListSubAttributes"));
SqlDataSource dsc = (SqlDataSource)(e.Row.FindControl("SqlDataSourceSubAttributes"));
String testing = Convert.ToString(dv["AttributeFk"]);
//This is not working
dsc.SelectParameters["@AttriFk"].DefaultValue = Convert.ToString(dv["AttributeFk"]);
//Than I tried this but still it isn't working:
dsc.SelectParameters.Add("@AttriFk", Convert.ToString(dv["AttributeFk"]));
ddlListSubAttributes.DataBind();
ddlListSubAttributes.SelectedValue = Convert.ToString(dv["SubAttributeFk"]);
}
}
I’m pretty sure the @ is not allowed in the parameter Name (leave it in the SQL select command though.
So change
to