从gridview_rowdatabound中的代码后面设置一个sql selectparameter

时间:2013-01-24 14:24:15

标签: c# asp.net

我在网格视图编辑模式下构建了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"]);
            }
        }

1 个答案:

答案 0 :(得分:2)

我非常确定参数名称中不允许使用@(但是请将其保留在SQL select命令中。

所以改变

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

 <asp:Parameter Name="AttriFk" />