2017-02-27 38 views
0

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

我希望用户可以查看当前值,并决定是否需要改变 - 这是我用了一个文本框:

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

你的代码片段中没有DropDownList。 – VDWWD

+0

@VDWWD我试图在下拉列表中显示我想实现的目标,我在文本框中同样实现了这一目标。 – user2796515

回答

0

您将需要使用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(); 
     } 
    } 
} 
0

我最终使用:

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

显示在DDL当前记录。

相关问题