О подробностях

Когда я пишу такой код:

<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;
    }
}

Когда домашняя страница равна «2», я «Добавить», нажмите «Вставить», обе радиокнопки видны. Почему?


person user189594    schedule 17.11.2009    source источник
comment
Вы отладили это? Попробуйте поставить точки останова после обоих операторов if. Возможно, в какой-то момент Дом изменится   -  person Nathan Koop    schedule 17.11.2009


Ответы (1)


вам нужно использовать событие detailsview Databound, а не то, что вы делаете при загрузке страницы, попробуйте этот код... он определенно сработает

 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;
        }
    }
}
person Muhammad Akhtar    schedule 17.11.2009