单击按钮

时间:2017-07-28 15:41:56

标签: c# asp.net listview

我有一个使用listview的aspx页面。删除按钮我需要使用列表视图中的ID执行sql删除。

ASPX:

 <asp:ListView runat="server" ID="ListView2" OnItemDataBound="ListView2_ItemDataBound" OnItemCommand="ListView2_ItemCommand">
                <LayoutTemplate>
                    ...
                    <asp:DataPager runat="server" ID="DataPager" PageSize="10" OnPreRender="DataPager_PreRender">
                        <Fields>
                           ...
                        </Fields>
                    </asp:DataPager>
                </LayoutTemplate>
                <ItemTemplate>
                    ...
                      <td runat="server" align="right" colspan="4"><asp:Button ID="deleteRButton" CssClass="Button" runat="server" Text="Delete"  CommandName="deleteRButton" OnClientClick="return confirm('You are about to delete this forum response.  Are you sure you want to proceed?');" /></td>
                    ...
                </ItemTemplate>
         <ItemSeparatorTemplate>
         </ItemSeparatorTemplate>
 </asp:ListView>

代码背后:

protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
            if (e.CommandName == "deleteRButton") //Never makes it here with breakpoints
        {
            ...
            //Find label value and execute SQL
        }

    }

如何点击按钮执行项目命令代码?我也尝试过使用OnClick方法(触发)但我没有那种方式访问​​标签ID值。

修改

分享页面加载事件:

protected void Page_Load(object sender, EventArgs e)
    {
        GetQuestions(); //builds data table and binds to listview 1
        GetAnswers(); //builds data table and binds to listview 2
        questionIDBreadCrumb.Text = grabID(); //grabs id from url
        //loads the current userID
        getUserData();
     }

1 个答案:

答案 0 :(得分:0)

解决方案:

将页面加载事件放入if(!ispostback)解决问题。

if (!IsPostBack)
        {
            GetQuestions();
            GetAnswers();
            questionIDBreadCrumb.Text = grabID();

            //loads the current userID
            getUserData();
            //validates questionOwner
        }