如果dropdownlist在 之內,如何設置dropdownlist的selectedvalue當EDIT 按鈕被點擊時Gridview和下拉列表由objectdatasource填充?使用ObjectDataSource將下拉列表的C#selectedvalue
我的應用程序會發生什麼變化dropdownlist得到填充 但第一項始終顯示爲selectedvalue。
如果dropdownlist在 之內,如何設置dropdownlist的selectedvalue當EDIT 按鈕被點擊時Gridview和下拉列表由objectdatasource填充?使用ObjectDataSource將下拉列表的C#selectedvalue
我的應用程序會發生什麼變化dropdownlist得到填充 但第一項始終顯示爲selectedvalue。
在數據網格視圖行數據綁定事件做到這一點
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dRowView1 = (DataRowView)e.Row.DataItem;
if ((e.Row.RowState= DataControlRowState.Edit) > 0)
{
DropDownList YourdropDown = (DropDownList)e.Row.FindControl("YourdropDown") as DropDownList;
if (YourdropDown!=null){
YourdropDown.SelectedValue = dRowView1["ID"].ToString();
}
}
}
標記:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="ObjectDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
<asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" />
<asp:BoundField DataField="Lastname" HeaderText="Lastname" SortExpression="Lastname" />
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<%#Eval("Age") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlAge">
<Items>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="20" Value="20"></asp:ListItem>
<asp:ListItem Text="30" Value="30"></asp:ListItem>
<asp:ListItem Text="40" Value="40"></asp:ListItem>
<asp:ListItem Text="50" Value="50"></asp:ListItem>
<asp:ListItem Text="60" Value="60"></asp:ListItem>
<asp:ListItem Text="70" Value="70"></asp:ListItem>
<asp:ListItem Text="80" Value="80"></asp:ListItem>
<asp:ListItem Text="90" Value="90"></asp:ListItem>
</Items>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAll" TypeName="OdsSelectedItem.App_Data.StudentsBll"></asp:ObjectDataSource>
代碼:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Student item = e.Row.DataItem as Student;
if (item != null)
{
var ddl = e.Row.FindControl("ddlAge") as DropDownList;
if (ddl == null) return;
ddl.SelectedValue = item.Age.ToString();
}
}
}
壞榜樣,但我認爲這顯示到正確的方向:)
就在今天我有類似的問題。我的問題是我需要將選定的值從名稱列表綁定到下拉列表。但我必須將「選定的值」傳遞給obj數據源才能填充下拉列表。
您不能綁定「selectedValue」並使用它將數據傳遞給將填充該下拉列表的函數。
這是我的解決方案:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource1" DataTextField="Text" DataValueField="Value" ToolTip='<%#Eval("ID") %>' SelectedValue='<%# Bind("ManagerID") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getallIDs" TypeName="MyClass" OldValuesParameterFormatString="original_{0}">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ID" PropertyName="ToolTip" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
發現下拉在GridView的RowBoundEvent並設置下拉的SeletedValue。或者,您可以使用SelectedValue ='EVAL(「ColumnName」)'來從GridView的DataSource綁定值。 – 2013-05-09 10:04:39