2017-02-27 20 views
0

グリッドビューのドロップダウンリストで選択した値を、現在のテキストボックスのように現在のレコードに設定する方法を教えてください。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の現在のレコードを表示します。

関連する問題