【问题标题】:Set the current value of gridview DDL on EditItemTemplate在 EditItemTemplate 上设置 gridview DDL 的当前值
【发布时间】:2017-02-27 14:02:51
【问题描述】:

如何将 gridview 下拉列表中的选定值设置为当前记录中的值,就像我目前对文本框所做的那样。

我希望用户可以查看当前值并决定是否需要更改它们——这就是我用于文本框的内容:

<asp:TemplateField HeaderText="Other Notes" SortExpression="Other Notes" HeaderStyle-Wrap="false">
    <edititemtemplate>
        <asp:TextBox ID="txtOtherNotes" runat="server" Text='<%# Eval("[Other Notes]") %>' DataTextField="Other Notes" DataValueField="Other Notes"></asp:TextBox>
    </edititemtemplate>
    <itemtemplate>
        <asp:Label runat="server" Text='<%# Bind("[Other Notes]") %>' ID="lblOtherNotes"></asp:Label>
    </itemtemplate>
</asp:TemplateField>

【问题讨论】:

  • 您的 sn-p 中没有 DropDownList。
  • @VDWWD 我试图在下拉列表中显示我想要实现的目标,我在文本框中同样实现了这一目标。

标签: c# asp.net gridview


【解决方案1】:

您需要为此使用OnRowDataBound 事件。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //check if the row is in edit mode
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //find the dropdownlist in the row using findcontrol
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList");

            //fill the dropdownlist from code behind if needed
            ddl.DataSource = source;
            ddl.DataTextField = "key";
            ddl.DataValueField = "valee";
            ddl.DataBind();

            //cast the dataitem back to a datarowview
            DataRowView row = e.Row.DataItem as DataRowView;

            //set the correct listitem as selected
            ddl.SelectedValue = row["myValue"].ToString();
        }
    }
}

【讨论】:

    【解决方案2】:

    我最终使用了:

     SelectedValue='<%# Bind("myID") %>' 
    

    在 ddl 中显示当前记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多