开发者

About Detailsview

开发者 https://www.devze.com 2022-12-11 20:35 出处:网络
When I write code like this: <asp:DetailsView ID=\"DetailsView1\" Runat=\"server\" DataSourceID=\"Vote\" DefaultMode=\"Insert\"

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开发者_StackOverflow中文版 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消