【问题标题】:How to pass value using ImageButton to update database?如何使用 ImageButton 传递值来更新数据库?
【发布时间】:2012-06-13 05:38:16
【问题描述】:

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

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

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

或者有什么建议可以传递状态值?

我的图片按钮:

<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"

我的控制参数

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

【问题讨论】:

    标签: asp.net vb.net controlparameter


    【解决方案1】:

    按钮在 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" />
    

    【讨论】:

      【解决方案2】:

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

      来自 MSDN:

      有时,多个 ImageButton 控件相互关联并共享相同的 CommandName 属性值,例如 Sort。使用此属性可使用有关要执行的命令的附加信息(例如升序)来补充 CommandName 属性。 CommandName 和 CommandArgument 属性的值通常用于 OnCommand 事件处理程序,以确定单击 ImageButton 控件时要执行的操作。

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

      【讨论】:

      • 我确实尝试过这个和其他解决方案,只是我总是收到错误:必须声明标量变量“@Status”似乎值只是没有通过
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      相关资源
      最近更新 更多