标签: itemcommand

ASP.Net转发器项目命令没有被触发

好吧,我已经使用了几十次中继器而没有任何问题,但今天出现了一些问题.我有一个转发器,我正在订阅itemCommand事件,但是当我的命令运行时,页面会回发但事件不会被触发.

为了解决这个问题,我必须在每个回发上进行数据绑定.

我的中继器看起来像这样:

<asp:Repeater id="MyRepeater" runat="server" onitemcommand="MyRepeater_ItemCommand">
<ItemTemplate>
    <li>
    <asp:Label id="Label" runat="server" />
    <asp:LinkButton id="LinkButton1" runat="server" commandname="Complete" commandargument='<%# Eval("MyID") %>' text='<%# Eval("Title") %>' />
    </li>
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)

和我的代码隐藏是这样的:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    SetupPage();
    }
}

private void SetupPage()
{
    // Do other stuff

    MyRepeater.DataSource = Repository.GetStuff()
    MyRepeater.DataBind();
}


protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{


// Do all my stuff here
}
Run Code Online (Sandbox Code Playgroud)

除非我注释掉if(!IsPostBack)行,否则不会调用MyRepeater_ItemCommand.一旦被注释掉并且转发器在每个回发上获得数据绑定,它就可以正常工作.我已经在很多其他页面中完成了这个,但就此而言似乎没有用.

其他人遇到这种行为或有解决方案吗?

asp.net repeater itemcommand

20
推荐指数
3
解决办法
3万
查看次数

我如何在ASP.NET应用程序中为ListView使用ItemCommand事件

我有一个带有ListView的ASP.NET应用程序.在ListView的每一行中,我都有一个LinkBut​​ton,它打开一个新的webform"Benutzer.aspx".我的问题是我没有得到这一行的索引.我使用ItemCommand事件,但它不起作用:(

在这里我的代码:

ASPX:

...

        <ItemTemplate>

            <tr runat="server"> 

                <td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
                <td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>

             <td align="left"><asp:LinkButton runat="server" Text="Anzeigen" CommandName="Anzeigen" OnCommand="ListView1_ItemCommand" CommandArgument="myArguments"></asp:LinkButton></td>

            </tr>

        </ItemTemplate>

...
Run Code Online (Sandbox Code Playgroud)

cs文件:

...

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Anzeigen")
            {
                Label lbText = (Label)e.Item.FindControl("Label2");

               string email = lbText.Text; …
Run Code Online (Sandbox Code Playgroud)

c# asp.net listview active-directory itemcommand

5
推荐指数
1
解决办法
2万
查看次数

asp.net中的repeater项目命令

我正在使用asp.net,我有一个转发器.我想从其中一个项目做出回应,这已经由repeater显示.这是我的代码:

<asp:Repeater ID="Repeatercart" runat="server" OnItemCommand ="RepeaterDeleteitemcommand" >
<ItemTemplate>                     
    <table> 
    <tr>
        <td><img id="Image1" src="PerfumeImages/<%#Eval("ProductImage") %>" width="90" /></td>
    </tr>                               
    <tr>
        <td><%#Eval("ProductName") %> x<%#Eval("Quantity") %></td>
    </tr>
    <tr>
        <td> <%#Eval("ProductGender") %></td>
    </tr>
    <tr>
        <td> <%#Eval("ProductSize") %> ml</td>
    </tr>                               
    <tr>
        <td><a class="buy-btn"><asp:Button ID="Button1" CommandName="Click" Text="Remove from cart" runat="server" CommandArgument='<%# Eval("ProductId") %>' /></a></td>
    </tr>
    </table>            
</ItemTemplate>
</asp:Repeater>   
Run Code Online (Sandbox Code Playgroud)

这是我的代码隐藏:

protected void RepeaterDeleteitemcommand(object source, RepeaterCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Click":
               Label1.Text = "ok !";
               break;
    }
}  
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我收到如下错误:无效的回发或回调参数.使用配置或页面中的<%@ Page EnableEventValidation ="true"%>启用事件验证.出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件.如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证.谢谢您的帮助

c# asp.net repeater itemcommand

5
推荐指数
1
解决办法
2万
查看次数

Repeater中的按钮不会触发ItemCommand

为什么Repeater中的按钮不会触发Repeater的ItemCommand事件?有没有办法强迫它这样做?ViewState已启用.

在下面的代码中,btnApprove和btnDelete是有问题的按钮:

<asp:Repeater runat="server" ID="rpt1" onitemdatabound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand" >
    <ItemTemplate>
        <table width="100%" style="margin-bottom:6px;">
            <tr>
                <td>
                    <asp:CheckBox ID="chkSelected" runat="server" Text=" " TextAlign="Right"/> Select
                    <asp:Button ID="btnApprove" runat="server" Width="80px" Text="Approve" />
                    <asp:Button ID="btnDelete" runat="server" Width="80px" Text="Delete" />
                </td>                                                                   
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="lblCommentStatus" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
        <table width="100%" style="margin-top:6px;">
            <tr>
                <td><asp:Label ID="lblAuthorName" runat="server" Text="Author: " Width="60px"></asp:Label></td>
                <td><asp:TextBox ID="txtAuthorName" runat="server" Width="250px"></asp:TextBox></td>
                <td style="padding-left: 30px;"><asp:Label ID="lblAuthorLocation" runat="server" Text="Location: " Width="70px"></asp:Label></td>
                <td><asp:TextBox ID="txtAuthorLocation" runat="server" Width="250px"></asp:TextBox></td>
            </tr>
        </table>
        Title: <asp:TextBox ID="txtTitle" runat="server" Width="640px" Enabled="False"></asp:TextBox>
        Body: …
Run Code Online (Sandbox Code Playgroud)

c# asp.net repeater button itemcommand

3
推荐指数
2
解决办法
2万
查看次数

在Repeater或GridView中第一次单击时,ItemCommand不会触发

这让我疯狂了2天 - 希望有人见过这个.

我有这个问题,在转发器或网格视图中第一次单击控件无法触发ItemCommand事件,所有后续点击工作.控件被加载到Base.aspx上的占位符中,就像这样

    private void LoadUserControl()
    {
        string controlPath = LastLoadedControl;

        if (!string.IsNullOrEmpty(controlPath))
        {
            ph.Controls.Clear();

            UserControl uc = (UserControl)LoadControl(controlPath);
            ph.Controls.Add(uc);
        }

    }
Run Code Online (Sandbox Code Playgroud)

我想知道这是否是一个viewstate问题,以及在哪些页面事件中我应该使用DataBind().我在不同的页面事件中尝试了数据绑定,在页面和控件上启用了视图状态而没有结果的变化.

这是GridView模板,但我对转发器控件也有相同的行为,所以我不相信它的控制就是这个问题.

<ItemTemplate>
       <asp:RadioButton ID="rbEnable" GroupName="MyGroup" runat="server" Text="Enabled" Checked="<%# ((EducateMe.BaseTypes.AbstractLink)Container.DataItem).IsActive == true %>" />
       <asp:RadioButton ID="rbDisable" runat="server" GroupName="MyGroup" Text="Disabled" Checked="<%# ((EducateMe.BaseTypes.AbstractLink)Container.DataItem).IsActive != true %>" />
       <asp:Button ID="btnEnable" runat="server" CommandArgument="<% # Container.DataItemIndex %>" CommandName="Enable" ToolTip="Enable" Text="Save" />
       <asp:Button ID="btnDisable" runat="server" CommandArgument="<% # Container.DataItemIndex %>" Visible="false" CommandName="Disable" ToolTip="Disable" Text="Disable" />
    </ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

一些可能相关的进一步信息:

我注意到的是在usercontrol的Page_Load事件中,我正在重新绑定控件.这可能是控制状态被重写的原因,但是如果我在ascx中向此区域添加if(!IsPostback),则此代码部分根本不像在aspx页面上那样触发.这将是我认为重新控制控制的正确部分.

c# asp.net itemcommand

2
推荐指数
1
解决办法
6577
查看次数

如何从转发器内部触发按钮事件?

我已经完成了我的研究但是找不到在VB中执行以下操作的有效方法:

  • 每个按钮都应该触发相同的事件.
  • 按钮事件保存每个转发器项目,因此每个事件都不是唯一的.

我知道我可以使用ItemCommand选项,但无法使其按预期工作.

ASP.NET

内置中继器项目

<asp:Button ID="btnSave" RunAt="Server"/>
Run Code Online (Sandbox Code Playgroud)

VB.NET

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    sqlConn.Open()
        For Each Item As RepeaterItem In rpt.Items
        ...
        Next
    sqlConn.Close()
End Sub
Run Code Online (Sandbox Code Playgroud)

vb.net asp.net repeater onclick itemcommand

2
推荐指数
2
解决办法
2万
查看次数