【问题标题】:Setting selectedvalue for a dropdownlist in GridView为 GridView 中的下拉列表设置 selectedvalue
【发布时间】:2009-12-08 14:55:57
【问题描述】:

我的 Gridview 中有一个下拉列表,我正在将一个数据源绑定到该 gridview。

虽然所有记录都正确显示,但未选择下拉值。

如何设置类似

<%# Bind("Country") %> 用于 ASP.net 中 Gridview 中的下拉列表。

谢谢

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    您可以挂钩网格视图的 RowDataBound 事件,找到控件并设置值。

    protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
         var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
         dropdownList .SelectedIndex = SET_VALUE_HERE;
    
    }
    

    【讨论】:

    • Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound Dim dl As DropDownList = CType(e.Row.FindControl("ddlDecision" ), DropDownList) Dim atviolPK As TextBox = CType(e.Row.FindControl("txtViolationPK"), TextBox) dl.SelectedValue = GetDecisionCode(atviolPK.Text) End Sub 由于某种原因,dl 和 actviolPK 都显示为空。请帮忙
    【解决方案2】:

    从数据源设置 DropDownList 值应如下所示:

        protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        if (e.Row.RowType == DataControlRowType.DataRow)
         {
            DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
            ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
         }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多