2012-02-23 119 views
0

我有一个GridView中有一个DropDownList麻烦的帖子NULL值的时候,其实一个值是使用内联编辑时,从列表中选择。ASP.NET GridView控件DropDownList的帖子空值

的问题是,我无法使用这种方法来将值更新命令结合在我的SqlDataSource:

的SelectedValue = '<%#绑定( “值”)%>'

的原因是因为该值可能不在列表中存在,所以它抛出一个异常。

有没有我可以值到更新命令绑定不使用的SelectedValue的方法吗?

谢谢先进。

回答

0

您可以使用的RowDataBound设置的SelectedValue;

中的.cs

文件

protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    GridView grid = (GridView)sender; 
    DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList); 
    HiddenField HiddenField1 = (e.Row.FindControl("HiddenField1") as HiddenField); 
    DropDownList1.SelectedValue = HiddenField1.Value; 

} 
在.aspx文件

;

<Columns> 

... 

       <asp:TemplateField HeaderText="Column Name"> 
        <ItemTemplate> 
         <asp:Label ID="Label1" runat="server" Text='<%# Bind("Value") %>'></asp:Label> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Value") %>'></asp:HiddenField> 
         <asp:DropDownList ID="DropDownList1" runat="server"> 
       ... 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 
    </Columns>