问题描述
我在 EditItemTemplate 的 gridview 中有一个 radiobuttonlist。当我单击一行中的更新时,RBL 将正确显示为他选择的值 1 。但是,如果我单击下一个 ROW,例如 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();
}
我做错了什么..?提前谢谢。
解决方法
解决方案在这篇文章中
How to check for combined RowState 'Altering | Edit' in RowDataBound?
e.Row.RowState 可以取
- 替代
- 编辑
- 插入
- 正常
- 已选择
也可以与其他状态交替使用。