0

我正在按照此示例在 gridview 编辑模式下构建 2 个级联下拉列表,但在 C# 中: http ://mikepope.com/blog/DisplayBlog.aspx?permalink=1708

但是我的@AttriFk 从来没有得到一个值,我该如何在这个事件中设置它?

<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>

这是我背后代码的一部分:

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"]);
            }
        }
4

1 回答 1

2

我很确定参数名称中不允许使用 @ (尽管将其保留在 SQL 选择命令中。

所以改变

<asp:Parameter Name="@AttriFk" />

 <asp:Parameter Name="AttriFk" />
于 2013-01-24T14:31:42.180 回答