views:

64

answers:

1
+1  Q: 

About Detailsview

When I write code like this:

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

And then in .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;
    }
}

When Home is '2', I Add click to insert , both of the radiobuttons are visible.Why?

+1  A: 

you need to use detailsview Databound event rather you are doing in page load, try this code... it will definitly work

 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;
        }
    }
}
Muhammad Akhtar
Thank you very much