如何使用ImageButton传递值来更新数据库?

时间:2012-06-13 05:38:17

标签: asp.net vb.net controlparameter

当我尝试将值从ImageButton传递给控件参数时遇到问题,然后update命令可以从控制参数中检索值以执行更新语句。

我想在单击ImageButton APPROVE时传递值Status = 1,否则在单击ImageButton REJECT时传递值Status = 2。

我应该在何处以及如何分配值状态? 当我运行我的代码时,我收到此错误:必须声明标量变量“@Status”。

或者传递值Status的任何建议?

我的ImageButton:

<ItemTemplate>
<asp:ImageButton runat="server" ID="APPROVE" CommandName="update" 
ImageUrl="~/images/accept.png"
OnClientClick="if (!window.confirm('Are you sure you want to approve this booking?')) return false;" />
</ItemTemplate>


<ItemTemplate>
<asp:ImageButton runat="server" ID="REJECT" CommandName="update"
ImageUrl="~/images/reject.png"
OnClientClick="if (!window.confirm('Are you sure you want to reject this booking?')) return false;" />
</ItemTemplate>

我的更新声明

UpdateCommand="UPDATE [bookingschedule] SET status=@Status WHERE [bookingScheduleID] = @bookingScheduleID"

我的ControlParameter

<UpdateParameters>                            
<asp:Parameter Name="bookingScheduleID" Type="Int32" />
<asp:ControlParameter Name="Status" ControlID="APPROVE"  Type="Int32" />
<asp:ControlParameter Name="Status" ControlID="REJECT"  Type="Int32" />
</UpdateParameters>

2 个答案:

答案 0 :(得分:0)

按钮在html中没有值,因此您无法在ControlParameter声明中使用它。一种解决方案是在表单上创建一个隐藏的文本框。让我们称之为“StatusType”。

<input type="hidden" name="StatusType" value="0">

现在,在每个按钮的“OnClientClick”中;将StatusType的值分配给1或2(或任何您定义的状态代码)。

OnClientClick="if (!window.confirm('Are you sure you want to reject this booking?')) {
    return;
}
else {
    $('#StatusType').val(0);
}"

然后在ControlParameter中使用“StatusType”。

<asp:ControlParameter Name="Status" ControlID="StatusType"  Type="Int32" />

答案 1 :(得分:0)

您是否尝试过使用ImageButton的CommandArgument property

来自MSDN:

  

有时,多个ImageButton控件是相关的,并为CommandName属性共享相同的值,例如Sort。使用此属性可以补充CommandName属性以及有关要执行的命令的其他信息,例如Ascending。 CommandName和CommandArgument属性的值通常在OnCommand事件处理程序中使用,以确定单击ImageButton控件时要执行的操作。

<asp:ImageButton id="imagebutton1" runat="server"
       AlternateText="Sort Ascending"
       ImageUrl="images/pict.jpg"
       OnCommand="ImageButton_Command"
       CommandName="Sort"
       CommandArgument="Ascending"/>