如何知道在数据列表中单击的按钮的工具提示

时间:2010-07-16 21:00:52

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

我需要代码示例please.i尝试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源代码中 ...

2 个答案:

答案 0 :(得分:2)

你可以这样做:

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;
        }
    }

答案 1 :(得分:1)

您可以尝试投射发件人:

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