【问题标题】:RadioButtonList in a GridViewGridView 中的 RadioButtonList
【发布时间】:2021-08-25 10:30:24
【问题描述】:

我在 EditItemTemplate 的网格视图中有一个单选按钮列表。当我单击一行中的更新时,RBL 会正确显示他选择的值 1 。但是,如果我单击下一行,例如 AlternatingRowStyle,则会显示 RBL 但没有他选择的值2

html:

<asp:GridView ID="GVDER" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True" DataKeyNames="DERINS,DERANA"
    OnRowCommand="GVDER_RowCommand"
    OnRowEditing="GVDER_RowEditing"
    OnRowUpdating="GVDER_RowUpdating"
    OnRowCancelingEdit="GVDER_RowCancelingEdit"
    OnRowDeleting="GVDER_RowDeleting"
    OnRowDataBound="GDVER_RowDataBound"
    AllowSorting="True" 
    CellPadding="4" 
    ForeColor="#333333" 
    GridLines="None" 
    AllowPaging="true"  
    PageSize="8">

<asp:TemplateField  >
    <HeaderTemplate>
         <asp:Label ID="lblHIVA"  style="text-align:center" runat="server" Text="IVA"  ></asp:Label>
     </HeaderTemplate>
    <ItemTemplate>
        <asp:Label ID="lblIva" runat="server" Text='<%# Eval("DERIVA") %>' width="50px" ></asp:Label>
    </ItemTemplate>
    <EditItemTemplate> 
        <asp:RadioButtonList ID="RBLIva" runat="server" RepeatDirection="Horizontal" width="250px">
            <asp:ListItem style="margin-right:20px;text-indent:5px" Value="0.0"> 0% </asp:ListItem>
            <asp:ListItem style="margin-right:20px;text-indent:5px" Value="10.5"> 10,5% </asp:ListItem>
            <asp:ListItem style="margin-right:20px;text-indent:5px" Value="21.0" > 21% </asp:ListItem>
        </asp:RadioButtonList>
    </EditItemTemplate>
    <ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>

在代码隐藏中:

public partial class derivacion_listado_actualizable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Request.IsAuthenticated)
        {
            Response.Redirect("~/Account/Login.aspx");
        }

        
        if (!IsPostBack)
        {
            CargarGridView();
        }
    }
    protected void CargarGridView()
    {
        
        
        DataTable dtbl = new DataTable();
        SqlDataAdapter dataAdapter = null;

        SqlConnection edo = new SqlConnection();
        edo.ConnectionString = ConfigurationManager.ConnectionStrings["JuncalCon"].ConnectionString;

        try
        {
            edo.Open();
            using (SqlCommand com = edo.CreateCommand())
            {
                com.CommandText = "Select * FROM DERAUX Where DERVAL ='N' ";

                dataAdapter = new SqlDataAdapter(com.CommandText, edo);

                dataAdapter.Fill(dtbl);

                if(dtbl.Rows.Count >0)
                {
                    GVDER.DataSource = dtbl;
                    GVDER.DataBind();
                }
               
            }
            
        }
        catch /*...*/
    }
    protected void GDVER_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Edit)
            {
                decimal IvaValue = (decimal)DataBinder.Eval(e.Row.DataItem, "DERIVA");
                RadioButtonList rb = (RadioButtonList)e.Row.FindControl("RBLIva");
                rb.Items.FindByValue(IvaValue.ToString(CultureInfo.CreateSpecificCulture("en-US"))).Selected = true;
            }
        }
    }
    protected void GVDER_RowEditing(object sender, GridViewEditEventArgs e)
    {
        lblSuccess.Text = "";
        GVDER.EditIndex = e.NewEditIndex;
        CargarGridView();
    }

我做错了什么..?提前谢谢。

【问题讨论】:

    标签: asp.net gridview bind radiobuttonlist


    【解决方案1】:

    解决方案在这篇文章中

    How to check for combined RowState 'Altering | Edit' in RowDataBound?

    e.Row.RowState 可以取

    • 备用
    • 编辑
    • 插入
    • 正常
    • 已选择

    Alternate con 也可以与其他状态结合使用。

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      相关资源
      最近更新 更多