【问题标题】:How to populate DropDownList in GridView EditItemTemplate depending on other column values如何根据其他列值在 GridView EditItemTemplate 中填充 DropDownList
【发布时间】:2013-05-17 17:16:49
【问题描述】:

在 GridView 中编辑一行时,我需要一个 DropDownList,其可用值列表取决于该行中的其他列(现在我们只说记录的“类型”);也就是说,将根据正在编辑的行列出不同的选项。

我通过在 GridView 中添加一个原本不需要的 DataKeyName 来使其正常工作,但这在其他地方引起了我的悲痛,无论如何它感觉太迂回了,所以我正在寻找更好的方法。以下是我目前的做法:

在 .aspx 文件中:

<asp:GridView ID="gvDct" ... DataKeyNames="dctId,dctType" ... >
  ...
  <asp:TemplateField>
    ...
    <EditItemTemplate>
      <asp:DropDownList ID="ddlDctParent" runat="server" 
             DataSourceId="sdsDctParent" 
             DataValueField="dctId" DataTextField="dctFullPath" 
             SelectedValue='<%# Bind("dctParentId") %>' 
             ... >
      </asp:DropDownList>
      <asp:SqlDataSource ID="sdsDctParent" runat="server" 
             ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
             OnSelecting="sdsDctParent_Selecting" 
             SelectCommand="SELECT dctId, dctFullPath FROM lkpDocCatAndType 
                            WHERE dctType=@dctType">
        <SelectParameters>
          <asp:Parameter Name="dctType" />
        </SelectParameters>
      </asp:SqlDataSource>
    </EditItemTemplate>
  </asp:TemplateField>
  ...
</asp:GridView>

我希望 SelectParameter 成为具有 ControlID="gvDct" 和 PropertyName="SelectedDataKey.Values[dctType]" 的 ControlParameter,但由于未知原因不起作用(参数值为 null),所以我添加了这个位到 .vb 代码隐藏文件以填充 DropDownList 的数据源的行特定参数:

Protected Sub sdsDctParent_Selecting(ByVal sender As Object, _
          ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
  e.Command.Parameters(0).Value = gvDct.DataKeys(gvDct.EditIndex).Item(1)
End Sub

所以,当我开始在 GridView 中编辑一行时,DropDownList 被绑定,这会导致 SqlDataSource SelectCommand 执行,这会触发 OnSelecting 代码,我在其中提供正在编辑的行 (EditIndex) 中的参数值,以便我在 DropDownList 中获取适当的值群。

有没有“更好”的方法?对我来说最重要的方面是避免添加 DataKeyName,因为这会导致我用于从 GridView 中删除一行的存储过程出现参数过多的问题。

【问题讨论】:

  • 上面的代码是不是容易被sql注入?

标签: asp.net gridview drop-down-menu edititemtemplate


【解决方案1】:

您应该使用 Gridview RowDataBound 稍后您可以在其中获取 valueID 您可以填充您的 Dropdownlist 还添加一个 label/hidden 字段,您想要填充其值的下拉列表
不好意思用c#写代码。

你可以试试这样的

protected void gvDct_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
  {
   if ((e.Row.RowState & DataControlRowState.Edit) > 0)
     {
      Label  lblValue = (Label)e.Row.FindControl("YourLableID")
      DropDownList ddList= (DropDownList)e.Row.FindControl("ddlDctParent");
      //bind dropdownlist
      DataTable dt = con.GetData("Select Query where YourColumn='"+lblValue.text+"'");
      ddList.DataSource = dt;
      ddList.DataTextField = "dctFullPath";
      ddList.DataValueField = "dctId";
      ddList.DataBind();

      DataRowView dr = e.Row.DataItem as DataRowView;
      ddList.SelectedValue = dr["dctId"].ToString();
     }
   }
 }

【讨论】:

  • GridViewEditEventArgs 不包含 Row 属性,而是使用 GridViewRowEventArgs
猜你喜欢
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
相关资源
最近更新 更多