ASPX 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 控件不为空的行(正在更新的一行),但我确实希望有更好的方法来解决这个问题。
编辑
根据要求,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.
I've got an ASPX page with a GridView bound to a SqlDataSource, containing a RadioButtonList in each row:
<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>
....
The SqlDataSource has an UpdateCommand in which I have to pass the selected RadioButton, but unfortunately I cannot get this to work:
<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>
Now this approach doesn't work as the RadioButtonList does get a different ID for every row. I also couldn't find any other pages to point me in the right direction, so hopefully someone at stackoverflow might be able to help.
I do have a very ugly working solution with attaching to the OnUpdating event of the SqlDataSource and then looping over all GridView rows to find the row in which the RadioButtonList control is not null (the one row that is being updated), but I do hope that there is a better way to solve this issue.
Edit
As requested the ugly code for 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 );
}
}
}
Thanks in advance
G.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用以下内容在 SslDataSource 中添加 UpdateParameter 吗?
Can you try to add UpdateParameter in the SslDataSource with the following.
我刚刚发现你可以使用 gridview 的 RowUpdating 事件:
i just found out that you can use the gridview's RowUpdating event: