使用RadioButtonList的ASPX GridView更新无法正常工作

时间:2011-07-04 09:10:18

标签: asp.net gridview radiobuttonlist

我有一个ASPX页面,其GridView绑定到SqlDataSource,每行包含一个RadioButtonList:

<asp:GridView ID="gvwSomeGridView" runat="server" DataSourceID="sdsSomeDataSource">
    <Columns>
        <asp:TemplateField HeaderText="SomeHeader">
            <EditItemTemplate>
                <asp:RadioButtonList ID="rblSomeRBL" RepeatDirection="Horizontal" runat="server" SelectedValue='<%#(bool)Eval("SomeColumn") ? "1" : "0"%>'>
                    <asp:ListItem Text="ABC" Value="1" />
                    <asp:ListItem Text="XYZ" Value="0" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Literal runat="server" ID="lblSomeLabel" Text='<%#Convert.ToBoolean(Eval("SomeFolumn")) ? "SomeString" : "SomeOtherString" />
            </ItemTemplate>
    ....   

SqlDataSource有一个UpdateCommand,我必须在其中传递所选的RadioButton,但不幸的是我无法使其工作:

<asp:SqlDataSource CancelSelectOnNullParameter="false" ConnectionString="..." ID="sdsSomeDataSource" runat="Server" UpdateCommand="UPDATE someTable SET someColumn = @SomeColumn" OnUpdating="OnSqlUpdating"

...

<UpdateParameters>
    <asp:ControlParameter Name="SomeColumn" ControlID="gvwSomeGridView$rblSomeRBL" PropertyName="SelectedValue" />
</UpdateParameters>

现在这种方法不起作用,因为RadioButtonList确实为每一行获得了不同的ID。我也找不到任何其他页面指向正确的方向,所以希望stackoverflow的某些人能够提供帮助。

我有一个非常难看的工作解决方案,附加到SqlDataSource的OnUpdating事件,然后循环遍历所有GridView行,以找到RadioButtonList控件不为null的行(正在更新的一行),但是我希望有更好的方法来解决这个问题。

修改

根据要求提供OnSqlUpdating的丑陋代码:

protected void OnSqlUpdating( object source, SqlDataSourceCommandEventArgs e )
{
    RadioButtonList radioButtonList = null;
    foreach( GridViewRow row in gvwSomeGridView.Rows )
    {
        if( row.FindControl( "rblSomeRBL" ) != null )
        {
            radioButtonList = row.FindControl( "rblSomeRBL" ) as RadioButtonList;
            break;
        }
    }

    if( radioButtonList != null )
    {
        int selectedValue = 0;
        if( Int32.TryParse( radioButtonList.SelectedValue, out selectedValue ) )
        {
            SqlParameter[] sqlParameters = new SqlParameter[ e.Command.Parameters.Count + 1 ];
            e.Command.Parameters.CopyTo( sqlParameter, 0 );
            sqlParameters[ sqlParameters.Length - 1 ] = new SqlParameter( "SomeColumn", SqlDbType.Bit )
            {
                Value = selectedValue
            }

            e.Command.Parameters.Clear();
            e.Command.Parameters.AddRange( sqlParameters );         
        }
    }
}

提前致谢

-G。

2 个答案:

答案 0 :(得分:0)

您是否可以尝试使用以下内容在SslDataSource中添加UpdateParameter。

<UpdateParameters>
    <asp:ControlParameter ControlID = "rblSomeRBL" Name="SomeColumn" Type="String" PropertyName="SelectedValue" />
</UpdateParameters>

答案 1 :(得分:0)

我刚刚发现你可以使用gridview的RowUpdating事件:

 <asp:Parameter Name="SomeColumn" Type="String" />

protected void gvwSomeGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

   GridViewRow row = gvwSomeGridView.Rows[e.RowIndex];

   RadioButtonList rblSomeRBL= row.Cells[0].Controls[0].FindControl("rblSomeRBL") as RadioButtonList;

   if (rblSomeRBL!= null)
   {
        sdsSomeDataSource.UpdateParameters["SomeColumn"].DefaultValue = rblSomeRBL.SelectedValue;
   }

    gvwSomeGridView.EditIndex = -1;
}
相关问题