Cấp bậc tác giả:

DOTNET

Làm thế nào để lựa chọn radiobutton trong GridView phụ thuộc vào giá trị trả về?

Được viết bởi QuangIT ngày 13/05/2013 lúc 06:13 PM
Tôi có ứng dụng web sử dụng GridView. Trong GridView tôi sử dụng RadioButtonList, RadioButtonList này có hai mục Yes và No. Tôi muốn chọn một mục từ hai phụ thuộc vào giá trị tôi nhận được từ bộ dữ liệu tại thời điểm binding.
  • 0
  • 8353

Làm thế nào để lựa chọn radiobutton trong GridView phụ thuộc vào giá trị trả về?

Tôi có ứng dụng web sử dụng GridView. Trong GridView tôi sử dụng RadioButtonList, RadioButtonList này có hai mục Yes và No. Tôi muốn chọn một mục từ hai phụ thuộc vào giá trị tôi nhận được từ bộ dữ liệu tại thời điểm binding. Tên cột của tôi là is_selected và nó trả về True thì trong RadioButtonList Yes sẽ được Check. Đây là mã

<asp:RadioButtonList ID="rbd_join" AutoPostBack="true" runat="server" 
                            RepeatDirection="Horizontal" BorderStyle="None" BorderWidth="0px" BorderColor="Transparent"
                            DataValueField="is_selected">
                        <asp:ListItem Text="Yes" Value="1" ></asp:ListItem>
                        <asp:ListItem Text="No" Value="0"></asp:ListItem>
                        </asp:RadioButtonList>

Bạn cần sử dụng sự kiện RowDataBound trên GridView:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var radioList = e.Row.FindControl("rbd_join") as RadioButtonList;

        var myObject = e.Row.DataItem as DataRowView;
        bool isSelected = bool.Parse(myObject["is_selected"].ToString());

        radioList.SelectedValue = isSelected ? "1" : "0";
    }
}

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML