【问题标题】:Respond to Button Events in DataList响应 DataList 中的按钮事件
【发布时间】:2010-08-11 07:45:14
【问题描述】:

经过一些帮助和审查代码后,我得到了这个工作没有任何错误。但是,当我按下按钮时,没有任何反应,文本框没有更新。

我还想知道如何访问 DataList 中的数据,以便我可以在“DataList1_ItemCommand”函数中对其进行操作。

<p>   
 <asp:TextBox ID="NameTextBox" runat="server" CssClass="textEntry" 
                TextMode="SingleLine" Rows="0" Height="20px" Width="250px" Enabled="False"></asp:TextBox>
    <asp:DataList 
         ID="DataList1" 
         runat="server" 
         RepeatColumns="1" CellPadding="4" ForeColor="#333333" 
        GridLines="Both" Height="132px" Width="427px">
        <HeaderTemplate>
            Data
        </HeaderTemplate>
        <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "ref") %>
             <%# DataBinder.Eval(Container.DataItem, "name") %>
             <%# DataBinder.Eval(Container.DataItem, "city") %>
             <%# DataBinder.Eval(Container.DataItem, "ip") %>
             <%# DataBinder.Eval(Container.DataItem, "timestamp") %>
             <asp:Button ID="manage" runat="server" CommandName="manageWiki" Text="Granska"  Visible="True" />
        </ItemTemplate>
        <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle Font-Bold="true" Font-Names="Arial" BackColor="#5D7B9D" 
            ForeColor="White" />
        <ItemStyle Font-Names="Arial" Font-Size="Small" BackColor="#F7F6F3" 
            ForeColor="#333333" />
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    </asp:DataList>
</p>

以及后面的代码:

protected void Page_Load(object sender, EventArgs e)
{

        if (!IsPostBack)
        {
             //getSuggestions fill the DataList with data
            getSuggestions("SELECT [ref], [city], [name], [timestamp], [ip] FROM [table1] ORDER BY timestamp");
        }

}

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "manageWiki")
    { 
        //Just update the TextBox
        NameTextBox.Text = "ref that is inside the Datalist1";
    }
}

【问题讨论】:

    标签: c# asp.net c#-4.0 datalist


    【解决方案1】:

    我觉得你的PageLoad 有点古怪。您在初始加载时执行DataList1.DataBind();,没有分配给DataList1 进行绑定。

    getSuggestions 也每次都进行绑定。假设您没有在 DataList 控件(看起来您没有)或父控件中禁用 ViewState,则无需每次都绑定。

    通常,如果您修改通过客户端脚本提供的数据并将其发回并且它与 ViewState 的预期不匹配,则会发生此错误...您确定这就是全部代码?

    编辑:我建议尽可能多地注释掉代码,以便执行您遇到问题的功能。您发布的许多代码可能只是使问题变得模糊不清。将其归结为一个非常基本的代码子集,然后发布代码和重新创建的步骤,因为您拥有它,我没有看到任何问题。您是否正在编写任何 javascript 代码来修改将回发的任何数据?

    【讨论】:

    • 好的,我明白了您的意思“通常,如果您修改通过客户端脚本提供的数据并将其发回,并且它与 ViewState 的内容不匹配,则会发生此错误期待”并且它有效。我将编辑上面的代码。
    • @Mikael 你的问题解决了吗?如果此答案对您有所帮助,请不要忘记将其设置为已接受的答案,甚至投赞成票:)
    • 部分解决了,但我意识到我问的问题是错误的,所以我更新了它,也许我应该开始一个新问题?
    • @Mikael 好吧,按钮没有触发,因为您的DataList 中没有连接ItemCommand 事件。应该有一个像OnItemCommand="DataList1_ItemCommand" 这样的属性。至于访问DataList 中的数据,您不应该直接访问它。如果你想从中获取一些数据,你应该在前面加载它,以便将它传递给你 ItemCommand 事件并且不需要去寻找它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多