【问题标题】:how to know the TOOL TIP of button clicked within the datalist如何知道在数据列表中单击的按钮的工具提示
【发布时间】:2011-03-17 04:16:30
【问题描述】:

我需要代码示例。我尝试了 selectedindexchange 但它没有注册任何索引更改要使用什么?

它的 c# vs08 asp.net sql server

代码文件是

.cs 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {





    }
    protected void Button1_Click(object sender, EventArgs e)
    {//not this
        ///Label3.Text = "clicked clicked clicked";


    }
    protected void Button1_Click1(object sender, EventArgs e)
    {

        Label5.Text = "the tool tip of the button clicked is! HELP!!!";


        //here code please how to which button is clicked?
        //there are many records so?
        //even if i try to use the button id directly
        //it does not appear
        //to vs the button does not exist outside the datalist control
        //help

    }
}

源文件

    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:test1 %>" 
        DeleteCommand="DELETE FROM [1] WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [1] ([ID], [NAME]) VALUES (@ID, @NAME)" 
        SelectCommand="SELECT * FROM [1]" 
        UpdateCommand="UPDATE [1] SET [NAME] = @NAME WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Decimal" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="NAME" Type="String" />
            <asp:Parameter Name="ID" Type="Decimal" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ID" Type="Decimal" />
            <asp:Parameter Name="NAME" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
<br />
    <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:DataList ID="DataList2" runat="server" DataKeyField="ID" 
        DataSourceID="SqlDataSource3">
        <ItemTemplate>
            ID:
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
            <br />
            NAME:
            <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
            -<asp:Label ID="Label2" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
            &nbsp;
            <br />
            <br />
            <br />
            &nbsp;<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>' 
                ToolTip='<%# Eval("NAME") %>'></asp:Label>
            <br />
            here extra information/ description is binded to tool tip.<br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                Text='<%# Eval("ID") %>' ToolTip='<%# Eval("NAME") %>' />
            <br />
            when clicked, the text of the button is displayed in the label. but many records 
            so button belonging to which record clicked?<br />
            <br />
            <br />
            <hr />
            <br />
            <br />
        </ItemTemplate>
    </asp:DataList>
    <br />

 





编辑

<asp:DataList ID="DataList2" runat="server" DataKeyField="ID" 
        DataSourceID="SqlDataSource3">
        <ItemTemplate>
            ID:
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
            <br />
            NAME:
            <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
            -<asp:Label ID="Label2" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
            &nbsp;
            <br />
            <br />
            <br />
            &nbsp;<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>' 
                ToolTip='<%# Eval("NAME") %>'></asp:Label>
            <br />
            here extra information/ description is binded to tool tip.<br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                Text='<%# Eval("ID") %>' ToolTip='<%# Eval("NAME") %>' />
            <br />
            when clicked, the text of the button is displayed in the label. <br />
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" CommandArgument='<%# Eval("NAME") %>' 
                CommandName="Explain" Text='<%# Eval("ID") %>' />
            <asp:TextBox ID="TextBox1" runat="server">First Record</asp:TextBox>
            <br />
            when clicked takes argument from button and the text in the text box, displayed. 
            (record 1)<br />
            <br />
            <br />
            <br />
            <asp:Button ID="Button3" runat="server" CommandArgument='<%# Eval("NAME") %>' 
                CommandName="Explain" Text='<%# Eval("ID") %>' />
            //<br />
            when clicked does the same as above
            <br />
            <hr />
            <br />
            <br />
        </ItemTemplate>
    </asp:DataList>

后面的代码

protected void DataList2_ItemCommand(object sender, DataListCommandEventArgs e) { // 行中所有设置了 CommandName 属性的按钮都可以导致该事件处理程序执行。 // 使用 CommandName 参数来确定单击了哪个按钮并采取适当的操作 开关(e.CommandName) {

        case "Explain":
            // update your label using the command argument rather that the button's ToolTip
            Label5.Text = e.CommandArgument.ToString();

            TextBox TextBox1 = e.Item.FindControl("TextBox1") as TextBox;

            Label6.Text = TextBox1.Text;

            break;



        default:
            Label5.Text="ERROR";
            break;
    }
}

错误:- 我忘了放 OnItemCommand="MyDataList_ItemCommand" 在 datalist 源代码中 ...

【问题讨论】:

    标签: c# asp.net visual-studio-2008 datalist


    【解决方案1】:

    你可以这样做:

    protected void Button1_Click1(object sender, EventArgs e)
    {
        Label5.Text = (sender as Button).ToolTip;
    }
    

    此外,如果您知道要使用该行中的其他控件,则可以使用 DataList.ItemCommand 事件而不是 Button.Click 事件。以下是您可以如何执行此操作的示例:

    ASP 标记:

            <asp:Label ID="MyLabel" runat="server" />
            <asp:DataList ID="MyDataList" runat="server" OnItemCommand="MyDataList_ItemCommand">
                <ItemTemplate>
                    <!-- Suppose you had some input controls that you needed to work with as well -->
                    <asp:TextBox ID="txtInput1" runat="server" />
                    <asp:TextBox ID="txtInput2" runat="server" />
                    <asp:Button ID="btnMyCommand" runat="server" CommandName="MyCommand" CommandArgument='<%# Eval("NAME") %>' Text='<%# "Execute My Command on ID:" + Eval("ID") %>' ToolTip='<%# string.Format("This will execute the \"My Command\" command on {0}.", Eval("NAME")) %>' />
                    <!-- just some examples of other buttons on the same row that execute different commands -->
                    <asp:Button ID="btnDoSomethingCrazy" runat="server" CommandName="Do Something Crazy!" Text="Do Something Crazy!" />
                    <asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
                </ItemTemplate>
            </asp:DataList>
    

    代码隐藏:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Page.IsCallback)
            {
                // some example data
                MyDataList.DataSource = new[] {
                    new { ID = 1, NAME = "ABCD" },
                    new { ID = 2, NAME = "BCDE" },
                    new { ID = 3, NAME = "CDEF" },
                };
                MyDataList.DataBind();
            }
        }
    
        protected void MyDataList_ItemCommand(object sender, DataListCommandEventArgs e)
        {
            // all of the buttons within the row can cause this event handler to execute.
            // Use the CommandName argument (populated by the CommandName property of the button that was clicked) in order to determine which button was clicked and take the appropriate action
            switch (e.CommandName)
            {
                case "Edit":
                    // ...
                    break;
                case "Update":
                    // ...
                    break;
                case "Cancel":
                    // ...
                    break;
                case "Delete":
                    // ...
                    break;
                case "MyCommand":
                    // update your label using the command argument rather that the button's ToolTip
                    MyLabel.Text = e.CommandArgument.ToString();
    
                    TextBox txtInput1 = e.Item.FindControl("txtInput1") as TextBox;
                    TextBox txtInput2 = e.Item.FindControl("txtInput2") as TextBox;
    
                    string value1 = txtInput1.Text;
                    string value2 = txtInput2.Text;
    
                    // do something with the input values
                    break;
                case "Do Something Crazy!":
                    // ...
                    break;
            }
        }
    

    【讨论】:

    • 对于其他新蜜蜂,也可以通过将设计视图中的 datalist 的 itemcommand 属性设置为名称为 datalist1_blahblah... 的方法来完成以下操作。注意:-该方法将出现在.cs 文件中的下拉列表
    • 在“ // 行中所有设置了 CommandName 属性的按钮都可能导致执行此事件处理程序时可能有一个错误。” 即使是那些没有设置此属性的按钮,如上面问题“button1 click”的编辑代码导致datalist的click事件和itemcommand事件执行!..这是为什么?
    • 嗯,你是对的。 DataList.ItemCommand 事件 (msdn.microsoft.com/en-us/library/…) 的文档只是说它“在单击 DataList 控件中的任何按钮时发生”,并且它“通常在具有自定义 CommandName 值的按钮控件时使用”。我将更新示例中的 cmets 以使其更准确。至于为什么......控件设计者可能只是没有看到将事件限制为设置了 CommandName 的按钮的理由。如果 CommandName 为 null,您可以选择忽略它。
    【解决方案2】:

    您可以尝试投射发件人:

    protected void Button1_Click1(object sender, EventArgs e)
    {
        Button myButton = (Button)sender;
        Label5.Text = myButton.ToolTip;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多