I thought this would be easy and straightforward, but I seem to be having an issue with it.
我认为这很容易和直截了当,但我似乎遇到了问题。
Basically, I have this in a formview:
基本上,我在formview中有这个:
<asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
And I want to replace it with this:
我想用这个替换它:
<asp:DropDownList ID="StatusDropdown" runat="server">
<asp:ListItem Selected="True" Value="A">Active</asp:ListItem>
<asp:ListItem Value="I">Inactive</asp:ListItem>
</asp:DropDownList>
I'm not exactly sure how to bind this like the Textbox is bound.
我不确定如何绑定这个绑定文本框。
Is there a straightforward answer?
有一个直截了当的答案吗?
2 个解决方案
#1
1
Codebehind is not a hack, actually it's best practise since you have full control and compiler support (shows errors at compile time and avoids careless mistakes)
Codebehind不是黑客,实际上它是最好的做法,因为你有完全的控制和编译器支持(在编译时显示错误并避免粗心的错误)
For example (assuming that the DropDownList
is in the EditItemTemplate
):
例如(假设DropDownList在EditItemTemplate中):
private void FormView1_DataBound(object sender, System.EventArgs e)
{
switch (FormView1.CurrentMode)
{
case FormViewMode.ReadOnly:
break;
case FormViewMode.Edit:
// note that the DataSource might be a different type
DataRowView drv = (DataRowView)FormView1.DataSource;
DropDownList StatusDropdown = (DropDownList)FormView1.FindControl("StatusDropdown");
// you can also add the ListItems programmatcially via Items.Add
StatusDropdown.DataSource = getAllStatus(); // a sample method that returns the datasource
StatusDropdown.DataTextField = "Status";
StatusDropdown.DataValueField = "StatusID";
StatusDropdown.DataBind();
StatusDropdown.SelectedValue = drv["StatusID"].ToString();
break;
case FormViewMode.Insert:
break;
}
}
#2
1
Bind it to the SelectedValue property as so:
将它绑定到SelectedValue属性,如下所示:
<asp:DropDownList ID="StatusDropdown" runat="server" SelectedValue='<%# Bind("Status") %>'>
The value in status has to match the value of a drop down list item.
状态中的值必须与下拉列表项的值匹配。
#1
1
Codebehind is not a hack, actually it's best practise since you have full control and compiler support (shows errors at compile time and avoids careless mistakes)
Codebehind不是黑客,实际上它是最好的做法,因为你有完全的控制和编译器支持(在编译时显示错误并避免粗心的错误)
For example (assuming that the DropDownList
is in the EditItemTemplate
):
例如(假设DropDownList在EditItemTemplate中):
private void FormView1_DataBound(object sender, System.EventArgs e)
{
switch (FormView1.CurrentMode)
{
case FormViewMode.ReadOnly:
break;
case FormViewMode.Edit:
// note that the DataSource might be a different type
DataRowView drv = (DataRowView)FormView1.DataSource;
DropDownList StatusDropdown = (DropDownList)FormView1.FindControl("StatusDropdown");
// you can also add the ListItems programmatcially via Items.Add
StatusDropdown.DataSource = getAllStatus(); // a sample method that returns the datasource
StatusDropdown.DataTextField = "Status";
StatusDropdown.DataValueField = "StatusID";
StatusDropdown.DataBind();
StatusDropdown.SelectedValue = drv["StatusID"].ToString();
break;
case FormViewMode.Insert:
break;
}
}
#2
1
Bind it to the SelectedValue property as so:
将它绑定到SelectedValue属性,如下所示:
<asp:DropDownList ID="StatusDropdown" runat="server" SelectedValue='<%# Bind("Status") %>'>
The value in status has to match the value of a drop down list item.
状态中的值必须与下拉列表项的值匹配。