简体   繁体   English

回发后,GridView中RadioButtons的检查状态不会更改

[英]Checked state of RadioButtons in GridView don't change after postback

I have a Gridview control and I have placed a RadioButton in the itemtemplate. 我有一个Gridview控件,并且已经在itemtemplate中放置了一个RadioButton。 When I click a button I'm trying to get the checked property of the radio button. 当我单击按钮时,我试图获取单选按钮的checked属性。 But When I click the checked property is always returning false. 但是,当我单击选中的属性时,总是返回false。 Please look into the below code and let me know where I'm making mistake. 请查看下面的代码,让我知道我在哪里出错。

aspx Code aspx代码

<asp:GridView ID="gvDepartments" runat="server" AutoGenerateColumns="False" GridLines="None"AllowPaging="true" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
      <Columns> 
       <asp:TemplateField>
         <ItemTemplate>
                 <asp:RadioButton ID="rdbtn" runat="server" />
         </ItemTemplate>
       </asp:TemplateField>
      </Columns>
    </asp:GridView>
<asp:Label ID="lbl" runat="server"></asp:Label>
<asp:Button ID="btn" runat="server" Text="Save" OnClick="btn_Click" CssClass="button small"/>

Code Behind on button click 单击按钮后的代码

foreach (GridViewRow row in gvDepartments.Rows)
{
   if (row.RowType == DataControlRowType.DataRow)
    {
        RadioButton rd = (RadioButton)row.FindControl("rdbtn");
        lbl.Text += rd.Checked.ToString();// +rd1.Checked.ToString();
    }
}

Result will be False always in the label always. 结果始终在标签中始终为False。 I'm using Ajaxcontroltoolkit in the application, Above controls are present in the update panel and I even tried to placing button and event in the triggers but the result is same. 我在应用程序中使用Ajaxcontroltoolkit,上面的控件出现在更新面板中,我什至尝试将按钮和事件放在触发器中,但结果是相同的。 Please help. 请帮忙。

Regards, 问候,
Nuthan AR Nuthan AR

When do you assign and load the DataSource of the GridView ? 什么时候分配和加载GridViewDataSource Note that you should do that only if(!IsPostBack) and not on every postback. 请注意,仅应在if(!IsPostBack)上执行此操作,而不应在每个回发上执行此操作。 So use the Page.IsPostBack property . 因此,请使用Page.IsPostBack属性

So assuming that you're using Page_Load for this: 因此,假设您为此使用Page_Load

protected void Page_Load(Object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        DataBindGridView(); // method that assigns DataSource and calls DataBind
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM