我有一个单选按钮列表,应该以Web表单的视图和编辑模式显示。当页面加载时,单选按钮应该根据从数据库中获取的值(类型为位的IsAnimal)选择值。
当用户切换到编辑模式时,单选按钮默认选择的值也应与查看模式相同。当用户切换时,编辑模式中所选的单选按钮值将会改变。但我在这里得到的是,在selectedindex_changed之后,pre_render将保持触发。如果我在prerender中执行if(!Page.IsPostBack),那么在编辑模式下默认选择值为null。
protected void Form_PreRender(object sender, EventArgs e) {
RadioButtonList rbViewIsAnimal = (RadioButtonList) fvForm.FindControl("rbViewIsAnimal");
RadioButtonList rbEditIsAnimal = (RadioButtonList) fvForm.FindControl("rbEditIsAnimal");
if (txtIsAnimal.Value == "True") {
if (rbViewIsAnimal != null) {
rbViewIsAnimal.Items.FindByValue("1").Selected = true;
}
if (rbEditIsAnimal != null) {
rbEditIsAnimal.Items.FindByValue("1").Selected = true;
}
} else {
if (rbViewIsAnimal != null) {
rbViewIsAnimal.Items.FindByValue("0").Selected = true;
}
if (rbEditIsAnimal != null) {
rbEditIsAnimal.Items.FindByValue("0").Selected = true;
}
}
}
protected void rbEditIsAnimal_SelectedIndexChanged(object sender, EventArgs e) {
RadioButtonList rbEditIsAnimal = (RadioButtonList) fvForm.FindControl("rbEditIsAnimal");
TextBox txtEditAnimalDescription = (TextBox) fvForm.FindControl("txtEditAnimalDescription");
if (txtIsAnimal.Value == "True") {
txtEditAnimalDescription.Enabled = false;
txtEditAnimalDescription.Text = string.Empty;
} else {
txtEditAnimalDescription.Enabled = true;
}
}
<ItemTemplate>
<asp:HiddenField runat="server" ID="txtIsAnimal" Value='<%# Bind("IsAnimal") %>' />
<asp:RadioButtonList runat="server" ID="rbViewIsAnimal" Enabled="false">
<asp:ListItem Text="Not Animal" Value="0" />
<asp:ListItem Text="Animal" Value="1" />
</asp:RadioButtonList>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField runat="server" ID="txtIsAnimal" />
<asp:RadioButtonList runat="server" ID="rbEditIsAnimal" Value='<%# Bind("IsAnimal") %>' AutoPostBack="true">
<asp:ListItem Text="Not Animal" Value="1" />
<asp:ListItem Text="Animale" Value="0" />
</asp:RadioButtonList>
</EditItemTemplate>
答案 0 :(得分:1)
我找到了一个看起来像你需要的样本here。样本数据有四列,并且第三列和第四列中有一个radiobuttonlist和一个下拉列表,因此下面的代码使用数据行单元[2]和[3]。我添加了一些评论。
// i believe this fires on load *and* when editing.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// editing:
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
// find the controls.
RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl("rbGenderEdit");
DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatusEdit");
// set the values.
rblGender.SelectedValue = dRowView[2].ToString();
ddlStatus.SelectedValue = dRowView[3].ToString();
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// find the controls.
RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
// set the values.
SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
}