1

当我写这样的代码时:

<asp:DetailsView ID="DetailsView1" Runat="server" DataSourceID="Vote" DefaultMode="Insert"
            AutoGenerateRows="False" DataKeyNames="id" Width="352px" Height="50px" 
            HorizontalAlign="Left"  >
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:RadioButton ID="RadioButtonVote" runat="server" Text="111" 
                            GroupName="A"  /><br/>
                        <asp:RadioButton ID="RadioButtonName" runat="server" Text="222" 
                            GroupName="A"/>
                    </ItemTemplate>
                </asp:TemplateField>
   <asp:CommandField CancelText="取消" InsertText="添加" ShowInsertButton="True"    ShowCancelButton="False"></asp:CommandField>
</asp:DetailsView>

然后在 .aspx.cs 中:

private string Home
{
    get
    {
        if (Request.QueryString["home"] != null)
        {
            return Request.QueryString["home"].ToString();
        }
        return "1";
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindDatailsView();
    }
}
private void BindDatailsView()
{
    RadioButton radioButtonVote = this.DetailsView1.FindControl("RadioButtonVote") as RadioButton;
    RadioButton radioButtonName = this.DetailsView1.FindControl("RadioButtonName") as RadioButton;
    if (Home.Equals("1"))
    {
        radioButtonName.Visible = true;
        radioButtonVote.Visible = true;
    }
    else if (Home.Equals("2"))
    {
        radioButtonVote.Visible = true;
        radioButtonName.Visible = false;
    }
}

当 Home 为“2”时,我添加 click to insert ,两个单选按钮都可见。为什么?

4

1 回答 1

1

你需要使用 detailsview 数据绑定事件而不是你在页面加载中做的,试试这个代码......它肯定会工作

 protected void DetailsView1_DataBound(object sender, EventArgs e)
{
    if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
    {
        RadioButton radioButtonVote = this.DetailsView1.FindControl("RadioButtonVote") as RadioButton;
        RadioButton radioButtonName = this.DetailsView1.FindControl("RadioButtonName") as RadioButton;
        if (Home.Equals("1"))
        {
            radioButtonName.Visible = true; radioButtonVote.Visible = true;
        }
        else if (Home.Equals("2"))
        {
            radioButtonVote.Visible = true; radioButtonName.Visible = false;
        }
    }
}
于 2009-11-17T05:50:11.967 回答